Skip to content

Commit 98d2c79

Browse files
authored
Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes (#662)
1 parent b83a0aa commit 98d2c79

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

msgpack/ext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,4 +167,4 @@ def from_datetime(dt):
167167
168168
:rtype: Timestamp
169169
"""
170-
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 1000)
170+
return Timestamp(seconds=int(dt.timestamp() // 1), nanoseconds=dt.microsecond * 1000)

test/test_timestamp.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@ def test_timestamp_datetime():
103103

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

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

107114
def test_unpack_datetime():
108115
t = Timestamp(42, 14)

0 commit comments

Comments
 (0)