Skip to content

Commit 4ea42eb

Browse files
committed
Fix Timestamp.from_datetime returning wrong value for pre-epoch times
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().
1 parent f980636 commit 4ea42eb

File tree

1 file changed

+1
-1
lines changed

1 file changed

+1
-1
lines changed

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)

0 commit comments

Comments
 (0)