Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,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
- Fixed documentation formatting of `Timex.TimezoneInfo.create/6`
- Updated tzdata to fix issues with 2024b
- Fix deprecation: Module.eval_quoted/4 is deprecated. Use Code.eval_quoted/3 instead
Expand Down
17 changes: 14 additions & 3 deletions lib/datetime/datetime.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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)
|> retain_precision(datetime)

{{ty, _a, _b} = amb, _} when ty in [:gap, :ambiguous] ->
amb
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
Original file line number Diff line number Diff line change
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