Implement read for DateTime

This commit is contained in:
RunasSudo 2025-04-21 21:47:15 +10:00
parent 4b76aeabb9
commit 6980156e48
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A

View File

@ -14,6 +14,7 @@
# 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/>.
from datetime import datetime
from decimal import Decimal
import io
import struct
@ -126,6 +127,10 @@ class CslaBinaryReader:
# BinaryReader.ReadInt32
return struct.unpack('<i', self.stream.read(4))[0]
def read_int64(self):
# BinaryReader.ReadInt64
return struct.unpack('<q', self.stream.read(8))[0]
def read_object(self):
# CslaBinaryReader.ReadObject
known_type = self.stream.read(1)[0]
@ -171,7 +176,10 @@ class CslaBinaryReader:
return self.read_decimal()
if known_type == CslaKnownTypes.DateTime.value:
raise NotImplementedError()
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)
if known_type == CslaKnownTypes.String.value:
return self.read_string()