Fix Timestamp.from_datetime() precision for far-future datetimes#710
Open
gaoflow wants to merge 1 commit into
Open
Fix Timestamp.from_datetime() precision for far-future datetimes#710gaoflow wants to merge 1 commit into
gaoflow wants to merge 1 commit into
Conversation
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.
methane
reviewed
Jul 3, 2026
Comment on lines
161
to
162
| utc = datetime.timezone.utc | ||
| return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta( |
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.