caellion-python-commons: caellion/pycommons/serializers/datetime_serializer.py Source File

caellion-python-commons

datetime_serializer.py
Go to the documentation of this file.
1 """!
2 This module provides various serializers for datetime.datetime object
3 """
4
5 import datetime
6 import math
7
8
9 class DateTimeIsoTimeSerializer:
10 """!
11 Implementation of DateTime serializer, using IsoTime as a serialized standard.
12 """
13
14 # Alias for unserialize
15 @staticmethod
16 def deserialize(serialized_string):
17 """!
18 Unserializes the ISO string to DateTime object.
19 Always returns UTC timezone DateTime.
20
21 Alias for unserialize()
22
23 @param serialized_string serialized datetime to unserialize
24 @returns DateTime with UTC timezone
25 """
26 return __class__.unserialize(serialized_string)
27
28 @staticmethod
29 def unserialize(serialized_string):
30 """!
31 Unserializes the ISO string to DateTime object.
32 Always returns UTC timezone DateTime.
33
34 @param serialized_string serialized datetime to unserialize
35 @returns DateTime with UTC timezone
36 """
37 return datetime.datetime.fromisoformat(serialized_string)
38
39 @staticmethod
40 def serialize(datetime_to_serialize):
41 """!
42 Serializes the DateTime to ISO string.
43 Always returns UTC timezone string.
44
45 @param datetime_to_serialize DateTime to serialize to UTC timezoned ISO string
46 @returns ISO String
47 """
48 return datetime_to_serialize.astimezone(datetime.timezone.utc).isoformat()
49
50
51 class DateTimeUnixMillisSerializer:
52 """!
53 Implementation of DateTime serializer, using Unix-epoch based timestamp with millisecond precision as serialized format.
54 """
55
56 # Alias for unserialize
57 @staticmethod
58 def deserialize(integer):
59 """!
60 Unserializes the unix timestamp with millisecond precision to DateTime object.
61 Always returns UTC timezone DateTime.
62
63 Alias for unserialize()
64
65 @param integer unix timestamp integer (milliseconds)
66 @returns DateTime with UTC timezone
67 """
68 return __class__.unserialize(integer)
69
70 @staticmethod
71 def unserialize(integer):
72 """!
73 Unserializes the unix timestamp with millisecond precision to DateTime object.
74 Always returns UTC timezone DateTime.
75
76 @param integer unix timestamp integer (milliseconds)
77 @returns DateTime with UTC timezone
78 """
79 return datetime.datetime.fromtimestamp(integer / 1000.0).astimezone(datetime.timezone.utc)
80
81 @staticmethod
82 def serialize(datetime_to_serialize):
83 """!
84 Serializes the DateTime to Unix timestamp integer with millisecond precision.
85 Always returns UTC timezone integer.
86
87 @param datetime_to_serialize DateTime to serialize to unix timestamp integer
88 @returns unix timestamp integer (milliseconds)
89 """
90 epoch = datetime.datetime(
91 year=1970, month=1, day=1, hour=0, minute=0, second=0, tzinfo=datetime.timezone.utc)
92 datetime_to_serialize = datetime_to_serialize.astimezone(
93 datetime.timezone.utc)
94 ts = (datetime_to_serialize - epoch).total_seconds() * 1000.0
95 ts = math.floor(ts)
96 return ts
97
98
99 class DateTimeUnixMicrosSerializer:
100 """!
101 Implementation of DateTime serializer, using Unix-epoch based timestamp with microsecond precision as serialized format.
102 """
103
104 # Alias for unserialize
105 @staticmethod
106 def deserialize(integer):
107 """!
108 Unserializes the unix timestamp with microsecond precision to DateTime object.
109 Always returns UTC timezone DateTime.
110
111 Alias for unserialize()
112
113 @param integer unix timestamp integer (microseconds)
114 @returns DateTime with UTC timezone
115 """
116 return __class__.unserialize(integer)
117
118 @staticmethod
119 def unserialize(integer):
120 """!
121 Unserializes the unix timestamp with microsecond precision to DateTime object.
122 Always returns UTC timezone DateTime.
123
124 @param integer unix timestamp integer (microseconds)
125 @returns DateTime with UTC timezone
126 """
127 return datetime.datetime.fromtimestamp(integer / 1000000.0).astimezone(datetime.timezone.utc)
128
129 @staticmethod
130 def serialize(datetime_to_serialize):
131 """!
132 Serializes the DateTime to Unix timestamp integer.
133 Always returns UTC timezone integer.
134
135 @param datetime_to_serialize DateTime to serialize to unix timestamp integer
136 @returns unix timestamp integer (microseconds)
137 """
138 epoch = datetime.datetime(
139 year=1970, month=1, day=1, hour=0, minute=0, second=0, tzinfo=datetime.timezone.utc)
140 datetime_to_serialize = datetime_to_serialize.astimezone(
141 datetime.timezone.utc)
142 ts = (datetime_to_serialize - epoch).total_seconds() * 1000000.0
143 ts = math.floor(ts)
144 return ts
145
146
147 class DateTimeUnixSerializer:
148 """!
149 Implementation of DateTime serializer, using Unix-epoch based timestamp with second precision as serialized format.
150 """
151
152 # Alias for unserialize
153 @staticmethod
154 def deserialize(integer):
155 """!
156 Unserializes the unix timestamp with second precision to DateTime object.
157 Always returns UTC timezone DateTime.
158
159 Alias for unserialize()
160
161 @param integer unix timestamp integer (seconds)
162 @returns DateTime with UTC timezone
163 """
164 return __class__.unserialize(integer)
165
166 @staticmethod
167 def unserialize(integer):
168 """!
169 Unserializes the unix timestamp with second precision to DateTime object.
170 Always returns UTC timezone DateTime.
171
172 @param integer unix timestamp integer (seconds)
173 @returns DateTime with UTC timezone
174 """
175 return datetime.datetime.fromtimestamp(integer / 1.0).astimezone(datetime.timezone.utc)
176
177 @staticmethod
178 def serialize(datetime_to_serialize):
179 """!
180 Serializes the DateTime to Unix timestamp integer with seconds precision.
181 Always returns UTC timezone integer.
182
183 @param datetime_to_serialize DateTime to serialize to unix timestamp integer
184 @returns unix timestamp integer (seconds)
185 """
186 epoch = datetime.datetime(
187 year=1970, month=1, day=1, hour=0, minute=0, second=0, tzinfo=datetime.timezone.utc)
188 datetime_to_serialize = datetime_to_serialize.astimezone(
189 datetime.timezone.utc)
190 ts = (datetime_to_serialize - epoch).total_seconds() * 1.0
191 ts = math.floor(ts)
192 return ts
caellion.pycommons.serializers.datetime_serializer.DateTimeIsoTimeSerializer
Implementation of DateTime serializer, using IsoTime as a serialized standard.
Definition: datetime_serializer.py:9
caellion.pycommons.serializers.datetime_serializer.DateTimeIsoTimeSerializer.deserialize
def deserialize(serialized_string)
Unserializes the ISO string to DateTime object.
Definition: datetime_serializer.py:16
caellion.pycommons.serializers.datetime_serializer.DateTimeIsoTimeSerializer.serialize
def serialize(datetime_to_serialize)
Serializes the DateTime to ISO string.
Definition: datetime_serializer.py:40
caellion.pycommons.serializers.datetime_serializer.DateTimeIsoTimeSerializer.unserialize
def unserialize(serialized_string)
Unserializes the ISO string to DateTime object.
Definition: datetime_serializer.py:29
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMicrosSerializer
Implementation of DateTime serializer, using Unix-epoch based timestamp with microsecond precision as...
Definition: datetime_serializer.py:99
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMicrosSerializer.serialize
def serialize(datetime_to_serialize)
Serializes the DateTime to Unix timestamp integer.
Definition: datetime_serializer.py:130
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMicrosSerializer.unserialize
def unserialize(integer)
Unserializes the unix timestamp with microsecond precision to DateTime object.
Definition: datetime_serializer.py:119
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMicrosSerializer.deserialize
def deserialize(integer)
Unserializes the unix timestamp with microsecond precision to DateTime object.
Definition: datetime_serializer.py:106
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMillisSerializer
Implementation of DateTime serializer, using Unix-epoch based timestamp with millisecond precision as...
Definition: datetime_serializer.py:51
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMillisSerializer.unserialize
def unserialize(integer)
Unserializes the unix timestamp with millisecond precision to DateTime object.
Definition: datetime_serializer.py:71
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMillisSerializer.deserialize
def deserialize(integer)
Unserializes the unix timestamp with millisecond precision to DateTime object.
Definition: datetime_serializer.py:58
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixMillisSerializer.serialize
def serialize(datetime_to_serialize)
Serializes the DateTime to Unix timestamp integer with millisecond precision.
Definition: datetime_serializer.py:82
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixSerializer
Implementation of DateTime serializer, using Unix-epoch based timestamp with second precision as seri...
Definition: datetime_serializer.py:147
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixSerializer.serialize
def serialize(datetime_to_serialize)
Serializes the DateTime to Unix timestamp integer with seconds precision.
Definition: datetime_serializer.py:178
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixSerializer.unserialize
def unserialize(integer)
Unserializes the unix timestamp with second precision to DateTime object.
Definition: datetime_serializer.py:167
caellion.pycommons.serializers.datetime_serializer.DateTimeUnixSerializer.deserialize
def deserialize(integer)
Unserializes the unix timestamp with second precision to DateTime object.
Definition: datetime_serializer.py:154