diff --git a/swh/storage/postgresql/converters.py b/swh/storage/postgresql/converters.py --- a/swh/storage/postgresql/converters.py +++ b/swh/storage/postgresql/converters.py @@ -1,9 +1,10 @@ -# Copyright (C) 2015-2020 The Software Heritage developers +# Copyright (C) 2015-2021 The Software Heritage developers # See the AUTHORS file at the top-level directory of this distribution # License: GNU General Public License version 3, or any later version # See top-level LICENSE file for more information import datetime +import math from typing import Any, Dict, Optional import warnings @@ -106,7 +107,7 @@ return TimestampWithTimezone( timestamp=Timestamp( - seconds=int(date.timestamp()), microseconds=date.microsecond, + seconds=int(math.floor(date.timestamp())), microseconds=date.microsecond, ), offset=offset, negative_utc=neg_utc_offset, diff --git a/swh/storage/tests/test_postgresql_converters.py b/swh/storage/tests/test_postgresql_converters.py --- a/swh/storage/tests/test_postgresql_converters.py +++ b/swh/storage/tests/test_postgresql_converters.py @@ -72,6 +72,78 @@ "neg_utc_offset": False, }, ), + ( + TimestampWithTimezone( + timestamp=Timestamp(seconds=0, microseconds=0,), + offset=-120, + negative_utc=False, + ), + { + "timestamp": "1970-01-01T00:00:00+00:00", + "offset": -120, + "neg_utc_offset": False, + }, + ), + ( + TimestampWithTimezone( + timestamp=Timestamp(seconds=0, microseconds=1,), + offset=-120, + negative_utc=False, + ), + { + "timestamp": "1970-01-01T00:00:00.000001+00:00", + "offset": -120, + "neg_utc_offset": False, + }, + ), + ( + TimestampWithTimezone( + timestamp=Timestamp(seconds=-1, microseconds=0,), + offset=-120, + negative_utc=False, + ), + { + "timestamp": "1969-12-31T23:59:59+00:00", + "offset": -120, + "neg_utc_offset": False, + }, + ), + ( + TimestampWithTimezone( + timestamp=Timestamp(seconds=-1, microseconds=1,), + offset=-120, + negative_utc=False, + ), + { + "timestamp": "1969-12-31T23:59:59.000001+00:00", + "offset": -120, + "neg_utc_offset": False, + }, + ), + ( + TimestampWithTimezone( + timestamp=Timestamp(seconds=-3600, microseconds=0,), + offset=-120, + negative_utc=False, + ), + { + "timestamp": "1969-12-31T23:00:00+00:00", + "offset": -120, + "neg_utc_offset": False, + }, + ), + ( + TimestampWithTimezone( + timestamp=Timestamp(seconds=-3600, microseconds=1,), + offset=-120, + negative_utc=False, + ), + { + "timestamp": "1969-12-31T23:00:00.000001+00:00", + "offset": -120, + "neg_utc_offset": False, + }, + ), ], ) def test_date(model_date, db_date):