Skip to content

DateTime.diff in days doesn't work with microsecond precision #14273

@MrYawe

Description

@MrYawe

Elixir and Erlang/OTP versions

Erlang/OTP 27 [erts-15.2] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [jit]

Elixir 1.18.1 (compiled with Erlang/OTP 27)

Operating system

macOS 14.7.2 (23H311)

Current behavior

These two scripts don't return the same result:

now = ~U[2025-01-04 10:00:00.000000Z]
in_almost_7_days = DateTime.add(now, 7, :day) |> DateTime.add(-1, :microsecond)
DateTime.diff(in_almost_7_days, now, :day)

# Result: 6
now = ~U[2025-02-14 13:33:13.296012Z]
in_almost_7_days = DateTime.add(now, 7, :day) |> DateTime.add(-1, :microsecond)
DateTime.diff(in_almost_7_days, now, :day)

# Result: 7

Expected behavior

I was expecting 6 in both cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions