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 
10  """!
11  Implementation of DateTime serializer, using IsoTime as a serialized standard.
12  """
13 
14 
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 
52  """!
53  Implementation of DateTime serializer, using Unix-epoch based timestamp with millisecond precision as serialized format.
54  """
55 
56 
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.fromtimestamp(0).astimezone(datetime.timezone.utc)
91  datetime_to_serialize = datetime_to_serialize.astimezone(datetime.timezone.utc)
92  ts = (datetime_to_serialize - epoch).total_seconds() * 1000.0
93  ts = math.floor(ts)
94  return ts
95 
96 
98  """!
99  Implementation of DateTime serializer, using Unix-epoch based timestamp with microsecond precision as serialized format.
100  """
101 
102 
103  @staticmethod
104  def deserialize(integer):
105  """!
106  Unserializes the unix timestamp with microsecond precision to DateTime object.
107  Always returns UTC timezone DateTime.
108 
109  Alias for unserialize()
110 
111  @param integer unix timestamp integer (microseconds)
112  @returns DateTime with UTC timezone
113  """
114  return __class__.unserialize(integer)
115 
116  @staticmethod
117  def unserialize(integer):
118  """!
119  Unserializes the unix timestamp with microsecond precision to DateTime object.
120  Always returns UTC timezone DateTime.
121 
122  @param integer unix timestamp integer (microseconds)
123  @returns DateTime with UTC timezone
124  """
125  return datetime.datetime.fromtimestamp(integer / 1000000.0).astimezone(datetime.timezone.utc)
126 
127  @staticmethod
128  def serialize(datetime_to_serialize):
129  """!
130  Serializes the DateTime to Unix timestamp integer.
131  Always returns UTC timezone integer.
132 
133  @param datetime_to_serialize DateTime to serialize to unix timestamp integer
134  @returns unix timestamp integer (microseconds)
135  """
136  epoch = datetime.datetime.fromtimestamp(0).astimezone(datetime.timezone.utc)
137  datetime_to_serialize = datetime_to_serialize.astimezone(datetime.timezone.utc)
138  ts = (datetime_to_serialize - epoch).total_seconds() * 1000000.0
139  ts = math.floor(ts)
140  return ts
141 
142 
144  """!
145  Implementation of DateTime serializer, using Unix-epoch based timestamp with second precision as serialized format.
146  """
147 
148 
149  @staticmethod
150  def deserialize(integer):
151  """!
152  Unserializes the unix timestamp with second precision to DateTime object.
153  Always returns UTC timezone DateTime.
154 
155  Alias for unserialize()
156 
157  @param integer unix timestamp integer (seconds)
158  @returns DateTime with UTC timezone
159  """
160  return __class__.unserialize(integer)
161 
162  @staticmethod
163  def unserialize(integer):
164  """!
165  Unserializes the unix timestamp with second precision to DateTime object.
166  Always returns UTC timezone DateTime.
167 
168  @param integer unix timestamp integer (seconds)
169  @returns DateTime with UTC timezone
170  """
171  return datetime.datetime.fromtimestamp(integer / 1.0).astimezone(datetime.timezone.utc)
172 
173  @staticmethod
174  def serialize(datetime_to_serialize):
175  """!
176  Serializes the DateTime to Unix timestamp integer with seconds precision.
177  Always returns UTC timezone integer.
178 
179  @param datetime_to_serialize DateTime to serialize to unix timestamp integer
180  @returns unix timestamp integer (seconds)
181  """
182  epoch = datetime.datetime.fromtimestamp(0).astimezone(datetime.timezone.utc)
183  datetime_to_serialize = datetime_to_serialize.astimezone(datetime.timezone.utc)
184  ts = (datetime_to_serialize - epoch).total_seconds() * 1.0
185  ts = math.floor(ts)
186  return ts
Implementation of DateTime serializer, using IsoTime as a serialized standard.
def serialize(datetime_to_serialize)
Serializes the DateTime to ISO string.
def serialize(datetime_to_serialize)
Serializes the DateTime to Unix timestamp integer.
Implementation of DateTime serializer, using Unix-epoch based timestamp with millisecond precision as...
def unserialize(integer)
Unserializes the unix timestamp with microsecond precision to DateTime object.
def serialize(datetime_to_serialize)
Serializes the DateTime to Unix timestamp integer with millisecond precision.
Implementation of DateTime serializer, using Unix-epoch based timestamp with second precision as seri...
def unserialize(integer)
Unserializes the unix timestamp with second precision to DateTime object.
def unserialize(integer)
Unserializes the unix timestamp with millisecond precision to DateTime object.
def serialize(datetime_to_serialize)
Serializes the DateTime to Unix timestamp integer with seconds precision.
Implementation of DateTime serializer, using Unix-epoch based timestamp with microsecond precision as...
def unserialize(serialized_string)
Unserializes the ISO string to DateTime object.