{{ message }}
Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes#662
Merged
methane merged 2 commits intoJun 2, 2026
Merged
Conversation
int() truncates towards zero, so for pre-epoch datetimes with non-zero microseconds the seconds component gets the wrong value. For example, datetime(1969, 12, 31, 23, 59, 59, 500000, UTC) has timestamp -0.5, but int(-0.5) == 0, producing Timestamp(0, 500000000) (+0.5s after epoch) instead of Timestamp(-1, 500000000) (-0.5s before epoch). Use floor division (// 1) instead, consistent with from_unix().
methane
approved these changes
Feb 24, 2026
Member
|
@bysiber would you add a testcase for the fix to test_timestamp.py? |
Sorry, something went wrong.
Contributor
There was a problem hiding this comment.
Pull request overview
Fixes incorrect Timestamp.from_datetime() results for pre-epoch datetimes with fractional seconds by ensuring the seconds component is computed via flooring (consistent with Timestamp.from_unix()), avoiding int()’s truncation-toward-zero behavior.
Changes:
- Update
Timestamp.from_datetime()to computesecondsusingint(dt.timestamp() // 1)instead ofint(dt.timestamp()).
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Sorry, something went wrong.
Hide details
View details
methane
merged commit
98d2c79
into
msgpack:main
Jun 2, 2026
26 checks passed
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
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.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.
Timestamp.from_datetime()usesint(dt.timestamp())to compute the seconds component, butint()truncates towards zero. For pre-epoch datetimes with non-zero microseconds, this produces the wrong value:The sign of the time gets flipped.
from_unix()already handles this correctly using floor division (int(unix_sec // 1)). This change makesfrom_datetime()consistent.