Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes#662
Open
bysiber wants to merge 1 commit intomsgpack:mainfrom
Open
Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes#662bysiber wants to merge 1 commit intomsgpack:mainfrom
bysiber wants to merge 1 commit intomsgpack:mainfrom
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().
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()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.