-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathdatetime.py
More file actions
396 lines (310 loc) · 12.8 KB
/
Copy pathdatetime.py
File metadata and controls
396 lines (310 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import utime
def is_leap_year(year):
return not year % 4 if year % 100 else not year % 400
def get_day_in_month(year, month):
if month == 2:
return 29 if is_leap_year(year) else 28
else:
return 31 if month in [1, 3, 5, 7, 8, 10, 12] else 30
def validate_date(year, month, day):
if not (isinstance(year, int) and 1 <= year <= 9999):
raise ValueError("\"year\" is not valid.")
if not (isinstance(month, int) and 1 <= month <= 12):
raise ValueError("\"month\" is not valid.")
if not (isinstance(day, int) and 1 <= day <= get_day_in_month(year, month)):
raise ValueError("\"day\" is not valid.")
def validate_time(hour, minute, second):
if not (isinstance(hour, int) and 0 <= hour <= 23):
raise ValueError("\"hour\" is not valid.")
if not (isinstance(minute, int) and 0 <= minute <= 59):
raise ValueError("\"minute\" is not valid.")
if not (isinstance(second, int) and 0 <= second <= 59):
raise ValueError("\"second\" is not valid.")
class UTimeAdapter(object):
"""QuecPython platform `utime` module adapter"""
@staticmethod
def get_local_timetuple():
temp = list(utime.localtime())
temp[-2] = (temp[-2] + 1) % 7
return tuple(temp)
@staticmethod
def get_timestamp_from_timetuple(time_tuple):
return utime.mktime(time_tuple)
@staticmethod
def get_timetuple_from_timestamp(timestamp):
temp = list(utime.localtime(timestamp))
temp[-2] = (temp[-2] + 1) % 7
return tuple(temp)
@staticmethod
def get_local_timezone_offset():
return utime.getTimeZone()
@staticmethod
def set_local_timezone_offset(offset):
return utime.setTimeZone(offset) == 0
class TimeZone(object):
def __init__(self, offset=0, name="N/A"):
self.__offset = self.__validate_offset(offset)
self.__name = name
def __repr__(self):
return "{}(offset={}, name={})".format(type(self).__name__, repr(self.offset), repr(self.name))
def __str__(self):
return "UTC{:+02d}:00".format(self.offset)
@staticmethod
def __validate_offset(offset):
if not (isinstance(offset, int) and -12 <= offset <= 12):
raise ValueError("offset should be int type and between [-12, 12].")
return offset
@property
def offset(self):
return self.__offset
@property
def name(self):
return self.__name
UTC = TimeZone(offset=0, name="UTC")
class TimeDelta(object):
def __init__(self, days=0, seconds=0, hours=0, minutes=0, weeks=0):
seconds += (minutes * 60 + (hours % 24 * 3600))
self.__seconds = seconds % 86400
self.__days = days + weeks * 7 + hours // 24 + seconds // 86400
def __str__(self):
return "{} days, {} seconds".format(self.days, self.seconds)
def __add__(self, other):
if isinstance(other, type(self)):
return type(self)(
days=self.days + other.days,
seconds=self.seconds + other.seconds
)
elif isinstance(other, DateTime):
return other + self
else:
raise TypeError("unsupported operand type(s) for +: \"{}\" and \"{}\"".format(type(self), type(other)))
def __sub__(self, other):
if isinstance(other, type(self)):
return type(self)(
days=self.days - other.days,
seconds=self.seconds - other.seconds
)
else:
raise TypeError("unsupported operand type(s) for -: \"{}\" and \"{}\"".format(type(self), type(other)))
def __lt__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for <: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() < 0
def __le__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for <=: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() <= 0
def __gt__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for >: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() > 0
def __ge__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for >=: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() >= 0
def __eq__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for ==: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() == 0
def __ne__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for !=: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() != 0
@property
def days(self):
return self.__days
@property
def seconds(self):
return self.__seconds
def total_seconds(self):
return self.days * 86400 + self.seconds
class _Date(object):
def __init__(self, year, month=1, day=1):
validate_date(year, month, day)
self.__year = year
self.__month = month
self.__day = day
def __str__(self):
return "{:04d}-{:02d}-{:02d}".format(self.year, self.month, self.day)
@property
def year(self):
return self.__year
@property
def month(self):
return self.__month
@property
def day(self):
return self.__day
class _Time(object):
def __init__(self, hour=0, minute=0, second=0):
validate_time(hour, minute, second)
self.__hour = hour
self.__minute = minute
self.__second = second
def __str__(self):
return "{:02d}:{:02d}:{:02d}".format(self.hour, self.minute, self.second)
@property
def hour(self):
return self.__hour
@property
def minute(self):
return self.__minute
@property
def second(self):
return self.__second
class DateTime(object):
def __init__(self, year, month=1, day=1, hour=0, minute=0, second=0, weekday=None, yearday=None, tz=None):
self.__date = _Date(year, month, day)
self.__time = _Time(hour, minute, second)
self.__tz = tz
self.__weekday = weekday
self.__yearday = yearday
self.__timestamp = None
def __repr__(self):
return "{}({}, {}, {}, {}, {}, {}, tz={})".format(
type(self).__name__,
self.year, self.month, self.day,
self.hour, self.minute, self.second,
repr(self.tz)
)
def __str__(self):
return "{} {}".format(self.date, self.time) + (" {}".format(self.tz) if self.tz else "")
@property
def year(self):
return self.date.year
@property
def month(self):
return self.date.month
@property
def day(self):
return self.date.day
@property
def hour(self):
return self.time.hour
@property
def minute(self):
return self.time.minute
@property
def second(self):
return self.time.second
@property
def date(self):
return self.__date
@property
def time(self):
return self.__time
@property
def tz(self):
return self.__tz
def __get_year_and_weekday(self):
days = 0
for m in range(1, self.month):
if m == 2:
days += (29 if is_leap_year(self.year) else 28)
else:
days += (31 if m in [1, 3, 5, 7, 8, 10, 12] else 30)
self.__yearday = days + self.day
year = self.year - 1
self.__weekday = (year + year // 4 - year // 100 + year // 400 + self.__yearday) % 7
@property
def weekday(self):
if self.__weekday is None:
self.__get_year_and_weekday()
return self.__weekday
@property
def yearday(self):
if self.__yearday is None:
self.__get_year_and_weekday()
return self.__yearday
@property
def timetuple(self):
return self.year, self.month, self.day, self.hour, self.minute, self.second
@property
def timestamp(self):
if self.__timestamp is None:
self.__timestamp = UTimeAdapter.get_timestamp_from_timetuple(self.timetuple + (None, None))
return self.__timestamp
@classmethod
def from_timestamp(cls, timestamp, tz=None):
time_tuple = UTimeAdapter.get_timetuple_from_timestamp(timestamp)
return cls(*time_tuple, tz=tz)
@classmethod
def utc_now(cls):
return cls.now(tz=UTC)
@classmethod
def now(cls, tz=None):
if not isinstance(tz, (TimeZone, type(None))):
raise TypeError("\"tz\" should be TimeZone type or None.")
timetuple = UTimeAdapter.get_local_timetuple()
self = cls(
*timetuple,
tz=TimeZone(offset=UTimeAdapter.get_local_timezone_offset())
)
if tz:
self = self.astimezone(tz)
return self
def replace(self, year=None, month=None, day=None, hour=None,
minute=None, second=None, tz=None):
if year is None:
year = self.year
if month is None:
month = self.month
if day is None:
day = self.day
if hour is None:
hour = self.hour
if minute is None:
minute = self.minute
if second is None:
second = self.second
if tz is None:
tz = self.tz
return type(self)(year, month, day, hour, minute, second, tz=tz)
def astimezone(self, tz=None):
if not (isinstance(tz, TimeZone) and isinstance(self.tz, TimeZone)):
raise TypeError("can not convert without timezone information.")
return (self - TimeDelta(hours=self.tz.offset) + TimeDelta(hours=tz.offset)).replace(tz=tz)
def __sub__(self, other):
if isinstance(other, TimeDelta):
time_tuple = UTimeAdapter.get_timetuple_from_timestamp(self.timestamp - other.total_seconds())
return type(self)(
*time_tuple,
tz=self.tz
)
elif isinstance(other, type(self)):
seconds = self.timestamp - other.timestamp
if all((self.tz, other.tz)):
seconds -= (self.tz.offset - other.tz.offset) * 3600
return TimeDelta(seconds=seconds)
else:
raise TypeError("unsupported operand type(s) for -: \"{}\" and \"{}\"".format(type(self), type(other)))
def __add__(self, other):
if isinstance(other, TimeDelta):
time_tuple = UTimeAdapter.get_timetuple_from_timestamp(self.timestamp + other.total_seconds())
return type(self)(*time_tuple, tz=self.tz)
else:
raise TypeError("unsupported operand type(s) for +: \"{}\" and \"{}\"".format(type(self), type(other)))
def __lt__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for <: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() < 0
def __le__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for <=: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() <= 0
def __gt__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for >: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() > 0
def __ge__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for >=: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() >= 0
def __eq__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for ==: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() == 0
def __ne__(self, other):
if not isinstance(other, type(self)):
raise TypeError("unsupported operand type(s) for !=: \"{}\" and \"{}\"".format(type(self), type(other)))
return (self - other).total_seconds() != 0