Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: preserve datetime precision after Timex.shift/2 #741

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -16,6 +16,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).

- Updated `Timex.now/1` typespec to remove the `AmbiguousDateTime`
- Corrected pluralization rules for bg/cs/he/id/ro/ru
- Fixed `Timex.shift/2` to preserve the precision of the provided datetime

---

Expand Down
17 changes: 14 additions & 3 deletions lib/datetime/datetime.ex
Expand Up @@ -447,13 +447,17 @@ defimpl Timex.Protocol, for: DateTime do
err

%DateTime{} = datetime when shift != 0 ->
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hope you don't mind if I make further suggestions. How about rewriting the two arrow clauses, like so:

%DateTime{} = original ->
  if shift == 0 do
    original
  else
    original
    |> DateTime.add(shift, :microsecond, Timex.Timezone.Database)
    |> retain_precision(original)
  end

DateTime.add(datetime, shift, :microsecond, Timex.Timezone.Database)
datetime
|> DateTime.add(shift, :microsecond, Timex.Timezone.Database)
|> retain_precision(datetime)

%DateTime{} = datetime ->
datetime

{{ty, _, _}, %DateTime{} = orig} when ty in [:gap, :ambiguous] and shift != 0 ->
DateTime.add(orig, shift, :microsecond, Timex.Timezone.Database)
{{ty, _, _}, %DateTime{} = original} when ty in [:gap, :ambiguous] and shift != 0 ->
original
|> DateTime.add(shift, :microsecond, Timex.Timezone.Database)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe it's a good idea to create a function for adding microseconds while keeping the precision? Something like:

original |> add_with_precision_preserved(shift)

|> retain_precision(datetime)

{{ty, _a, _b} = amb, _} when ty in [:gap, :ambiguous] ->
amb
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would get rid of the case statement below as it seems completely unnecessary, but that may be outside of the scope of this PR. 🤷 😅

Expand All @@ -480,6 +484,13 @@ defimpl Timex.Protocol, for: DateTime do
err
end

defp retain_precision(
%DateTime{microsecond: {ms, _precision}} = new_datetime,
%DateTime{microsecond: {_ms, precision}} = _original_datetime
) do
%{new_datetime | microsecond: {ms, precision}}
end

defp logical_shift(datetime, []), do: datetime

defp logical_shift(datetime, shifts) do
Expand Down
40 changes: 40 additions & 0 deletions test/shift_test.exs
Expand Up @@ -229,4 +229,44 @@ defmodule ShiftTests do
expected = ~D[2017-12-31] |> Timex.to_datetime()
assert expected === date
end

describe "DateTime does not change precision" do
test "seconds" do
datetime = Timex.shift(~U[2023-04-13 08:00:00Z], minutes: 1)
expected = ~U[2023-04-13 08:01:00Z]
assert expected === datetime
end

test "milliseconds" do
datetime = Timex.shift(~U[2023-04-13 08:00:00.000Z], minutes: 1)
expected = ~U[2023-04-13 08:01:00.000Z]
assert expected === datetime
end

test "microseconds" do
datetime = Timex.shift(~U[2023-04-13 08:00:00.000000Z], minutes: 1)
expected = ~U[2023-04-13 08:01:00.000000Z]
assert expected === datetime
end
end

describe "NaiveDateTime does not change precision" do
test "seconds" do
datetime = Timex.shift(~N[2023-04-13 08:00:00Z], minutes: 1)
expected = ~N[2023-04-13 08:01:00Z]
assert expected === datetime
end

test "milliseconds" do
datetime = Timex.shift(~N[2023-04-13 08:00:00.000Z], minutes: 1)
expected = ~N[2023-04-13 08:01:00.000Z]
assert expected === datetime
end

test "microseconds" do
datetime = Timex.shift(~N[2023-04-13 08:00:00.000000Z], minutes: 1)
expected = ~N[2023-04-13 08:01:00.000000Z]
assert expected === datetime
end
end
end