Fix Timestamp.from_datetime returning wrong value for pre-epoch datetimes (#662)

This commit is contained in:
Kadir Can Ozden 2026-06-02 15:41:52 +03:00 committed by GitHub
parent b83a0aa62b
commit 98d2c7907d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 8 additions and 1 deletions

View file

@ -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)

View file

@ -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)