Skip to content

Conversation

@emancu
Copy link
Contributor

@emancu emancu commented Jan 10, 2026

In Elixir 1.14+, Timex.shift/2 upgrades datetime precision to microseconds due to changes in DateTime.add/4.

This caused date_and_offset_to_datetime/3 to return datetimes like ~U[2023-01-01 10:00:00.000000Z] instead of ~U[2023-01-01 10:00:00Z], breaking Ecto validations and equality checks.

Fix: truncate to :second precision after shift since input is always ~T[00:00:00] (second precision).

Also updates all dependencies to latest compatible versions.

Is it the same fix as the fork?

No. The fork preserves the original precision dynamically:

  # Fork approach - general purpose
  defp retain_precision(new_datetime, original_datetime) do
    precision = original_datetime.microsecond |> elem(1)
    %{new_datetime | microsecond: {elem(new_datetime.microsecond, 0), precision}}
  end

My fix hardcodes :second truncation:

  # Surgex approach - specific to this function
  NaiveDateTime.truncate(shifted, :second)

Ref: bitwalker/timex#731

⚠️ By mistake, the fix was merged with fixing the pipelines... see here

In Elixir 1.14+, Timex.shift/2 upgrades datetime precision to
microseconds due to changes in DateTime.add/4. This caused
date_and_offset_to_datetime/3 to return datetimes like
~U[2023-01-01 10:00:00.000000Z] instead of ~U[2023-01-01 10:00:00Z],
breaking Ecto validations and equality checks.

Fix: truncate to :second precision after shift since input is always
~T[00:00:00] (second precision).

Also updates all dependencies to latest compatible versions.

Ref: bitwalker/timex#731
@emancu emancu self-assigned this Jan 10, 2026
@emancu
Copy link
Contributor Author

emancu commented Jan 10, 2026

Waiting for #61

emancu and others added 2 commits January 15, 2026 15:32
…ncate(shifted, :second) call in the shift_datetime/2 function,

ensuring datetimes maintain {0, 0} microsecond precision even during DST transitions
@emancu emancu force-pushed the emancu/fix_precision_on_date_and_offset branch from 14509e2 to ee3be7e Compare January 15, 2026 19:16
@emancu emancu mentioned this pull request Jan 15, 2026
@emancu emancu merged commit 7cc038a into master Jan 15, 2026
6 checks passed
@emancu emancu deleted the emancu/fix_precision_on_date_and_offset branch January 15, 2026 23:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants