Compare commits
4 Commits
aec7242e72
...
da65539d16
Author | SHA1 | Date | |
---|---|---|---|
da65539d16 | |||
ef2580feba | |||
6ded4cb568 | |||
6970250be9 |
@ -14,7 +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 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()
|
||||
|
@ -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
|
||||
@ -85,6 +86,10 @@ class CslaBinaryWriter:
|
||||
# BinaryWriter.WriteInt32
|
||||
self.stream.write(struct.pack('<i', value))
|
||||
|
||||
def write_int64(self, value):
|
||||
# BinaryWriter.WriteInt64
|
||||
self.stream.write(struct.pack('<q', value))
|
||||
|
||||
def write_string(self, value):
|
||||
# BinaryWriter.WriteString - "The string is prefixed with the length, encoded as an integer seven bits at a time."
|
||||
encoded_string = value.encode('utf-8')
|
||||
@ -94,12 +99,21 @@ class CslaBinaryWriter:
|
||||
|
||||
def write_object(self, value):
|
||||
# CslaBinaryWriter.Write(...)
|
||||
if value is None:
|
||||
return self.write_object_null()
|
||||
|
||||
if isinstance(value, bool):
|
||||
return self.write_object_bool(value)
|
||||
|
||||
if isinstance(value, int):
|
||||
return self.write_object_int32(value)
|
||||
|
||||
if isinstance(value, Decimal):
|
||||
return self.write_object_decimal(value)
|
||||
|
||||
if isinstance(value, datetime):
|
||||
return self.write_object_datetime(value)
|
||||
|
||||
if isinstance(value, str):
|
||||
return self.write_object_string(value)
|
||||
|
||||
@ -118,6 +132,15 @@ class CslaBinaryWriter:
|
||||
self.write_int32(len(value))
|
||||
self.stream.write(value)
|
||||
|
||||
def write_object_datetime(self, value):
|
||||
# CslaBinaryWriter.Write(DateTime)
|
||||
timestamp_unix = value.timestamp()
|
||||
timestamp = timestamp_unix * 10000000 # Convert seconds to ticks (10^9 / 100)
|
||||
timestamp += 621355968000000000 # Add unix epoch in .NET ticks - https://gist.github.com/kristopherjohnson/397d0f74213a0087f1a1
|
||||
timestamp = int(timestamp) # Number of 100-nanosecond intervals that have elapsed since January 1, 0001 at 00:00:00.000
|
||||
self.stream.write(bytes([CslaKnownTypes.DateTime.value]))
|
||||
self.write_int64(timestamp)
|
||||
|
||||
def write_object_decimal(self, value):
|
||||
# CslaBinaryWriter.Write(Decimal)
|
||||
self.stream.write(bytes([CslaKnownTypes.Decimal.value]))
|
||||
@ -131,6 +154,9 @@ class CslaBinaryWriter:
|
||||
self.stream.write(bytes([CslaKnownTypes.Int32.value]))
|
||||
self.write_int32(value)
|
||||
|
||||
def write_object_null(self):
|
||||
self.stream.write(bytes([CslaKnownTypes.Null.value]))
|
||||
|
||||
def write_object_string(self, value):
|
||||
# CslaBinaryWriter.Write(string)
|
||||
self.stream.write(bytes([CslaKnownTypes.String.value]))
|
||||
|
Loading…
x
Reference in New Issue
Block a user