FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes by bysiber · Pull Request #662 · msgpack/msgpack-python · GitHub

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) All 1 file type selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
2 changes: 1 addition & 1 deletion msgpack/ext.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,4 @@ def from_datetime(dt):

:rtype: Timestamp
"""
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 1000)
return Timestamp(seconds=int(dt.timestamp() // 1), nanoseconds=dt.microsecond * 1000)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

@copilot apply changes based on this feedback

7 changes: 7 additions & 0 deletions test/test_timestamp.py
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
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def test_timestamp_datetime():

assert Timestamp.from_datetime(ts).to_datetime() == ts

# Regression test: pre-epoch fractional seconds must floor toward -inf.
pre_epoch = datetime.datetime(1969, 12, 31, 23, 59, 59, 500000, tzinfo=utc)
ts_pre_epoch = Timestamp.from_datetime(pre_epoch)
assert ts_pre_epoch.seconds == -1
assert ts_pre_epoch.nanoseconds == 500000000
assert ts_pre_epoch.to_datetime() == pre_epoch


def test_unpack_datetime():
t = Timestamp(42, 14)
Expand Down
Loading

Back | FazBrowse Home | New Git URL