From da65539d1610678fc33cdd0409bdb28cb1e91ad5 Mon Sep 17 00:00:00 2001 From: RunasSudo Date: Mon, 21 Apr 2025 23:19:04 +1000 Subject: [PATCH] Set tz when reading DateTime --- csla_binary/binary_reader.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/csla_binary/binary_reader.py b/csla_binary/binary_reader.py index fb06364..de57333 100644 --- a/csla_binary/binary_reader.py +++ b/csla_binary/binary_reader.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . -from datetime import datetime +from datetime import datetime, timezone from decimal import Decimal import io import struct @@ -115,6 +115,12 @@ class CslaBinaryReader: shift += 7 + def read_datetime(self): + timestamp = self.read_int64() # Number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 + timestamp_unix = timestamp - 621355968000000000 # Subtract unix epoch in .NET ticks - https://gist.github.com/kristopherjohnson/397d0f74213a0087f1a1 + timestamp_unix /= 10000000 # Convert ticks to seconds (10^9 / 100) + return datetime.fromtimestamp(timestamp_unix, timezone.utc) + def read_decimal(self): length = self.read_int32() if length != 4: @@ -176,10 +182,7 @@ class CslaBinaryReader: return self.read_decimal() if known_type == CslaKnownTypes.DateTime.value: - timestamp = self.read_int64() # Number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000 - timestamp_unix = timestamp - 621355968000000000 # Subtract unix epoch in .NET ticks - https://gist.github.com/kristopherjohnson/397d0f74213a0087f1a1 - timestamp_unix /= 10000000 # Convert ticks to seconds (10^9 / 100) - return datetime.fromtimestamp(timestamp_unix) + return self.read_datetime() if known_type == CslaKnownTypes.String.value: return self.read_string()