Repository navigation
Conversation
When the two datetimes are in different time zones (or on the same day), the Rust precise_diff moves each one to UTC by adjusting the hour, minute and second fields by hand. That adjustment only rolls over when a field goes strictly past 60 or 24, so a result of exactly 24:00 stays as hour 24, and a day pushed past either end of the month becomes day 0 or day 32 instead of moving into the neighbouring month. The pure-Python implementation subtracts the offset with datetime arithmetic and does not have this problem, so the two disagree, e.g. 2024-06-01 00:00 UTC to 2024-06-01 20:00 America/New_York is "1 day" in Python but "24 hours" with the extension. Do the shift on the total seconds of the day and carry any day change into the day, month and year.
This branch has not been deployed
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.
Pull Request Check List
When the two datetimes are in different time zones (or fall on the same day), the Rust
precise_diffmoves each one to UTC by adjusting the hour, minute and second fields by hand. That adjustment only carries when a field goes strictly past 60 or 24, and it never carries into the month or year. So:The pure-Python
precise_diffsubtracts the offset withdatetimearithmetic and does not have this problem, so the results depend on whether the extension is loaded:This change does the shift on the total seconds of the day and carries any day change into the day, month and year. Since UTC offsets are always less than a day, the date moves by at most one day. The two hand-written copies (one per datetime) are replaced by one
DateTimeInfo::shift_to_utcmethod.This is separate from #901 / #906. The example in #901 comes from
is_exact_type_ofon the second argument, which #953 changes. The new test uses stdlibdatetimeobjects withzoneinfo, so it does not depend on that change.Testing
test_precise_diff_timezone_crossing_a_date_boundaryintests/test_helpers.py. It covers the three cases above, in both argument orders.1 failed, 18 passed(assert 0 == 1,PreciseDiff(... days=0, hours=24 ...)).PENDULUM_EXTENSIONS=0, before and after:19 passed.19 passed.pytest -q testswith the extension:1871 passed, 3 skipped. WithPENDULUM_EXTENSIONS=0:1871 passed, 3 skipped.pendulum._pendulum.precise_diffagainstpendulum._helpers.precise_diff. It used 200k pairs per seed and three seeds, with stdlib aware, naive and date inputs over UTC, New York, Paris, Kolkata, Lord Howe, St Johns, Kathmandu and Chatham. Before: 59 mismatches for seed 1. After: 0 mismatches for every seed.cargo fmt --all -- --checkandcargo clippy --tests -- -D warningsare clean, and so areruff checkandruff format --check(0.14.11) on the test file.Not run: mypy, PyPy, the codspeed benchmarks, and other platforms (macOS arm64, CPython 3.13 only).
Note: this changes the
diff()/Intervalresults that the extension returns for datetimes in different time zones (or on the same day) when the UTC shift crosses a date boundary. They now match the pure-Python results.