Fix NaiveDateTime.diff/3 over-counting incomplete units#15583
Merged
josevalim merged 1 commit intoJul 10, 2026
Merged
Conversation
`NaiveDateTime.diff/3` truncated each operand to the requested unit and only then subtracted, i.e. it computed `floor(t1) - floor(t2)` instead of `floor(t1 - t2)`. When the subtrahend's sub-second fraction exceeded the minuend's, this rounded the result *up*, contradicting the documented guarantee that "Fractional results are [...] truncated" and the example that it "rounds incomplete days to zero". For instance an elapsed span of 86399.5s (less than one day) returned `1` for `:day`, and a 0.5s span returned `1` for `:second`. The sibling `DateTime.diff/3`, which carries the identical doc line, returned `0` for the same wall-clock instants because it computes the difference at the `:microsecond` base first and truncates once via `System.convert_time_unit/3`. Align `NaiveDateTime.diff/3` with `DateTime.diff/3` so both agree on identical instants. All existing doctests are preserved. Assisted-by: Claude Code:claude-opus-4-8
Member
|
💚 💙 💜 💛 ❤️ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
NaiveDateTime.diff/3truncated each operand to the requested unit and only then subtracted, i.e. it computedfloor(t1) - floor(t2)instead offloor(t1 - t2). When the subtrahend's sub-second fraction exceeded the minuend's, this rounded the result up, contradicting the documented guarantee that "Fractional results are [...] truncated" and the example that it "rounds incomplete days to zero".For instance an elapsed span of 86399.5s (less than one day) returned
1for:day, and a 0.5s span returned1for:second. The siblingDateTime.diff/3, which carries the identical doc line, returned0for the same wall-clock instants because it computes the difference at the:microsecondbase first and truncates once viaSystem.convert_time_unit/3.Align
NaiveDateTime.diff/3withDateTime.diff/3so both agree on identical instants. All existing doctests are preserved.Assisted-by: Claude Code:claude-opus-4-8