-
Notifications
You must be signed in to change notification settings - Fork 381
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -447,13 +447,17 @@ defimpl Timex.Protocol, for: DateTime do | |
err | ||
|
||
%DateTime{} = datetime when shift != 0 -> | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. 🤷 😅 |
||
|
@@ -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 | ||
|
There was a problem hiding this comment.
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: