Set tz when reading DateTime

This commit is contained in:
RunasSudo 2025-04-21 23:19:04 +10:00
parent ef2580feba
commit da65539d16
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -14,7 +14,7 @@
# You should have received a copy of the GNU Affero General Public License # You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from datetime import datetime from datetime import datetime, timezone
from decimal import Decimal from decimal import Decimal
import io import io
import struct import struct
@ -115,6 +115,12 @@ class CslaBinaryReader:
shift += 7 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): def read_decimal(self):
length = self.read_int32() length = self.read_int32()
if length != 4: if length != 4:
@ -176,10 +182,7 @@ class CslaBinaryReader:
return self.read_decimal() return self.read_decimal()
if known_type == CslaKnownTypes.DateTime.value: 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 return self.read_datetime()
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)
if known_type == CslaKnownTypes.String.value: if known_type == CslaKnownTypes.String.value:
return self.read_string() return self.read_string()