Fix Timestamp.from_datetime() precision for far-future datetimes#710
Merged
methane merged 1 commit intoJul 5, 2026
Merged
Conversation
methane
reviewed
Jul 3, 2026
from_datetime() derived the whole-second part from the float datetime.timestamp(), which cannot hold microsecond precision far from the epoch. It rounded the seconds up while still taking the exact microsecond for the nanoseconds, so the result was one second in the future (and raised OverflowError near datetime.max). Use integer timedelta arithmetic, matching the Cython packer, so the pure-Python path agrees with the reference implementation.
35bf81d to
b367f92
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes msgpack.ext.Timestamp.from_datetime() so it computes the whole-second component using integer timedelta arithmetic instead of datetime.timestamp() (float), preventing off-by-one-second rounding and OverflowError for far-future datetimes while keeping naive-datetime “local time” semantics consistent with datetime.timestamp().
Changes:
- Switch
Timestamp.from_datetime()to derive seconds/nanoseconds fromdt - epochusing integer math (and handle naive datetimes viaastimezone()to preserve existing behavior). - Introduce a shared module-level UTC epoch constant and reuse it in
to_datetime(). - Add regression tests covering far-future round-trip precision, exact seconds/nanoseconds, packer agreement, and the prior
OverflowErrorcase.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
msgpack/ext.py |
Reworks Timestamp.from_datetime() to avoid float timestamp precision/overflow and reuses a module-level UTC epoch in datetime conversions. |
test/test_timestamp.py |
Adds regression tests to ensure correct far-future datetime round-trips and consistency with datetime packing. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Timestamp.from_datetime()computes the whole-second part from the floatdatetime.timestamp(). A float64 cannot hold microsecond precision for datetimes far from the epoch, sotimestamp()rounds the seconds up while the exactmicrosecondis still used for the nanoseconds. The result is aTimestampone second in the future, and neardatetime.maxit raisesOverflowError:The Cython packer already uses integer
timedeltaarithmetic and is correct. This makes the pure-Pythonfrom_datetime()do the same, so the two paths agree and the round-trip invariantfrom_datetime(d).to_datetime() == dholds for far-future datetimes. Naive datetimes keep their existing local-time interpretation (matchingdatetime.timestamp()).Follow-up to #662, which fixed the pre-epoch rounding direction in the same method.
Existing tests are unchanged; I added a regression test covering the far-future round-trip, the exact seconds/nanoseconds, agreement with packing the datetime directly, and the former
OverflowErrorcase. Full suite is green on both the C-extension and pure-Python paths.