mirror of
https://github.com/msgpack/msgpack-python.git
synced 2026-06-19 06:02:09 +00:00
Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes (#662)
This commit is contained in:
parent
b83a0aa62b
commit
98d2c7907d
2 changed files with 8 additions and 1 deletions
|
|
@ -167,4 +167,4 @@ class Timestamp:
|
|||
|
||||
:rtype: Timestamp
|
||||
"""
|
||||
return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 1000)
|
||||
return Timestamp(seconds=int(dt.timestamp() // 1), nanoseconds=dt.microsecond * 1000)
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue