From e92f68e9f0bda0d6808b8cb29e95c01be1aa9b07 Mon Sep 17 00:00:00 2001 From: hakan Date: Thu, 18 Apr 2024 13:44:28 +0200 Subject: [PATCH 1/4] avoid using float to convert timestamp from and to datetime --- msgpack/ext.py | 6 ++++-- test/test_timestamp.py | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/msgpack/ext.py b/msgpack/ext.py index 02c2c430..c689a3a8 100644 --- a/msgpack/ext.py +++ b/msgpack/ext.py @@ -157,7 +157,9 @@ def to_datetime(self): :rtype: `datetime.datetime` """ utc = datetime.timezone.utc - return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta(seconds=self.to_unix()) + return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta( + seconds=self.seconds, microseconds=round(self.nanoseconds / 1e3) + ) @staticmethod def from_datetime(dt): @@ -165,4 +167,4 @@ def from_datetime(dt): :rtype: Timestamp """ - return Timestamp.from_unix(dt.timestamp()) + return Timestamp(seconds=int(dt.timestamp() // 1), nanoseconds=dt.microsecond * 10**3) diff --git a/test/test_timestamp.py b/test/test_timestamp.py index db5cc57a..9b3a2606 100644 --- a/test/test_timestamp.py +++ b/test/test_timestamp.py @@ -86,6 +86,24 @@ def test_timestamp_datetime(): utc = datetime.timezone.utc assert t.to_datetime() == datetime.datetime(1970, 1, 1, 0, 0, 42, 0, tzinfo=utc) + ts = datetime.datetime(2024, 4, 16, 8, 43, 9, 420317, tzinfo=utc) + ts2 = datetime.datetime(2024, 4, 16, 8, 43, 9, 420318, tzinfo=utc) + + assert Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1e3 + + ts3 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4256) + ts4 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4257) + assert ( + Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1e3 + ) + + assert Timestamp.from_datetime(ts).to_datetime() == ts + + t2 = Timestamp(1713256989, 420318123) + t3 = Timestamp(1713256989, 420318499) + t4 = Timestamp(1713256989, 420318501) + assert t2.to_datetime() == t3.to_datetime() != t4.to_datetime() + def test_unpack_datetime(): t = Timestamp(42, 14) From 12d9cc086fcada919f7e6152edc399fbcd2aef1c Mon Sep 17 00:00:00 2001 From: hakan Date: Fri, 19 Apr 2024 10:50:40 +0200 Subject: [PATCH 2/4] removed floor division in from_datetime --- msgpack/ext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/msgpack/ext.py b/msgpack/ext.py index c689a3a8..e5d7d222 100644 --- a/msgpack/ext.py +++ b/msgpack/ext.py @@ -167,4 +167,4 @@ def from_datetime(dt): :rtype: Timestamp """ - return Timestamp(seconds=int(dt.timestamp() // 1), nanoseconds=dt.microsecond * 10**3) + return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 10**3) From 64c0e6797ffbe29c8c533cdf58a17d9ea64e1ad6 Mon Sep 17 00:00:00 2001 From: hakan Date: Fri, 19 Apr 2024 13:33:28 +0200 Subject: [PATCH 3/4] removed rounding for sub-second precision in to_datetime --- msgpack/ext.py | 2 +- test/test_timestamp.py | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/msgpack/ext.py b/msgpack/ext.py index e5d7d222..7fdad86f 100644 --- a/msgpack/ext.py +++ b/msgpack/ext.py @@ -158,7 +158,7 @@ def to_datetime(self): """ utc = datetime.timezone.utc return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta( - seconds=self.seconds, microseconds=round(self.nanoseconds / 1e3) + seconds=self.seconds, microseconds=self.nanoseconds // 1e3 ) @staticmethod diff --git a/test/test_timestamp.py b/test/test_timestamp.py index 9b3a2606..ce786edb 100644 --- a/test/test_timestamp.py +++ b/test/test_timestamp.py @@ -99,11 +99,6 @@ def test_timestamp_datetime(): assert Timestamp.from_datetime(ts).to_datetime() == ts - t2 = Timestamp(1713256989, 420318123) - t3 = Timestamp(1713256989, 420318499) - t4 = Timestamp(1713256989, 420318501) - assert t2.to_datetime() == t3.to_datetime() != t4.to_datetime() - def test_unpack_datetime(): t = Timestamp(42, 14) From 34ab27319836654a369d54dac71ec6302fc5b73d Mon Sep 17 00:00:00 2001 From: hakan Date: Fri, 19 Apr 2024 17:42:18 +0200 Subject: [PATCH 4/4] 1e3/10**3 --> 1000 --- msgpack/ext.py | 4 ++-- test/test_timestamp.py | 6 ++++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/msgpack/ext.py b/msgpack/ext.py index 7fdad86f..3940fe0f 100644 --- a/msgpack/ext.py +++ b/msgpack/ext.py @@ -158,7 +158,7 @@ def to_datetime(self): """ utc = datetime.timezone.utc return datetime.datetime.fromtimestamp(0, utc) + datetime.timedelta( - seconds=self.seconds, microseconds=self.nanoseconds // 1e3 + seconds=self.seconds, microseconds=self.nanoseconds // 1000 ) @staticmethod @@ -167,4 +167,4 @@ def from_datetime(dt): :rtype: Timestamp """ - return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 10**3) + return Timestamp(seconds=int(dt.timestamp()), nanoseconds=dt.microsecond * 1000) diff --git a/test/test_timestamp.py b/test/test_timestamp.py index ce786edb..f9bc8353 100644 --- a/test/test_timestamp.py +++ b/test/test_timestamp.py @@ -89,12 +89,14 @@ def test_timestamp_datetime(): ts = datetime.datetime(2024, 4, 16, 8, 43, 9, 420317, tzinfo=utc) ts2 = datetime.datetime(2024, 4, 16, 8, 43, 9, 420318, tzinfo=utc) - assert Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1e3 + assert ( + Timestamp.from_datetime(ts2).nanoseconds - Timestamp.from_datetime(ts).nanoseconds == 1000 + ) ts3 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4256) ts4 = datetime.datetime(2024, 4, 16, 8, 43, 9, 4257) assert ( - Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1e3 + Timestamp.from_datetime(ts4).nanoseconds - Timestamp.from_datetime(ts3).nanoseconds == 1000 ) assert Timestamp.from_datetime(ts).to_datetime() == ts