From b81195eb448f0a33b4fb86fe868533da8f6abd79 Mon Sep 17 00:00:00 2001 From: Georg K Date: Wed, 5 Apr 2023 13:02:25 +0300 Subject: [PATCH] fix: _Timestamp.to_datetime works with negative ts --- src/betterproto/__init__.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/betterproto/__init__.py b/src/betterproto/__init__.py index d1b0b99..5739b1f 100644 --- a/src/betterproto/__init__.py +++ b/src/betterproto/__init__.py @@ -1578,7 +1578,11 @@ class _Timestamp(Timestamp): def to_datetime(self) -> datetime: ts = self.seconds + (self.nanos / 1e9) - return datetime.fromtimestamp(ts, tz=timezone.utc) + + if ts < 0: + return datetime(1970, 1, 1) + timedelta(seconds=ts) + else: + return datetime.fromtimestamp(ts, tz=timezone.utc) @staticmethod def timestamp_to_json(dt: datetime) -> str: