Implement write for DateTime
This commit is contained in:
parent
6ded4cb568
commit
ef2580feba
@ -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')
|
||||
@ -106,6 +111,9 @@ class CslaBinaryWriter:
|
||||
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)
|
||||
|
||||
@ -124,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]))
|
||||
|
Loading…
x
Reference in New Issue
Block a user