-
Notifications
You must be signed in to change notification settings - Fork 1k
/
datetime.py
877 lines (696 loc) · 21.3 KB
/
datetime.py
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
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
# datetime.py
import time as _tmod
_DBM = (0, 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334)
_DIM = (0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31)
_TIME_SPEC = ("auto", "hours", "minutes", "seconds", "milliseconds", "microseconds")
def _leap(y):
return y % 4 == 0 and (y % 100 != 0 or y % 400 == 0)
def _dby(y):
# year -> number of days before January 1st of year.
Y = y - 1
return Y * 365 + Y // 4 - Y // 100 + Y // 400
def _dim(y, m):
# year, month -> number of days in that month in that year.
if m == 2 and _leap(y):
return 29
return _DIM[m]
def _dbm(y, m):
# year, month -> number of days in year preceding first day of month.
return _DBM[m] + (m > 2 and _leap(y))
def _ymd2o(y, m, d):
# y, month, day -> ordinal, considering 01-Jan-0001 as day 1.
return _dby(y) + _dbm(y, m) + d
def _o2ymd(n):
# ordinal -> (year, month, day), considering 01-Jan-0001 as day 1.
n -= 1
n400, n = divmod(n, 146_097)
y = n400 * 400 + 1
n100, n = divmod(n, 36_524)
n4, n = divmod(n, 1_461)
n1, n = divmod(n, 365)
y += n100 * 100 + n4 * 4 + n1
if n1 == 4 or n100 == 4:
return y - 1, 12, 31
m = (n + 50) >> 5
prec = _dbm(y, m)
if prec > n:
m -= 1
prec -= _dim(y, m)
n -= prec
return y, m, n + 1
MINYEAR = 1
MAXYEAR = 9_999
class timedelta:
def __init__(
self, days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0
):
s = (((weeks * 7 + days) * 24 + hours) * 60 + minutes) * 60 + seconds
self._us = round((s * 1000 + milliseconds) * 1000 + microseconds)
def __repr__(self):
return "datetime.timedelta(microseconds={})".format(self._us)
def total_seconds(self):
return self._us / 1_000_000
@property
def days(self):
return self._tuple(2)[0]
@property
def seconds(self):
return self._tuple(3)[1]
@property
def microseconds(self):
return self._tuple(3)[2]
def __add__(self, other):
if isinstance(other, datetime):
return other.__add__(self)
else:
us = other._us
return timedelta(0, 0, self._us + us)
def __sub__(self, other):
return timedelta(0, 0, self._us - other._us)
def __neg__(self):
return timedelta(0, 0, -self._us)
def __pos__(self):
return self
def __abs__(self):
return -self if self._us < 0 else self
def __mul__(self, other):
return timedelta(0, 0, round(other * self._us))
__rmul__ = __mul__
def __truediv__(self, other):
if isinstance(other, timedelta):
return self._us / other._us
else:
return timedelta(0, 0, round(self._us / other))
def __floordiv__(self, other):
if isinstance(other, timedelta):
return self._us // other._us
else:
return timedelta(0, 0, int(self._us // other))
def __mod__(self, other):
return timedelta(0, 0, self._us % other._us)
def __divmod__(self, other):
q, r = divmod(self._us, other._us)
return q, timedelta(0, 0, r)
def __eq__(self, other):
return self._us == other._us
def __le__(self, other):
return self._us <= other._us
def __lt__(self, other):
return self._us < other._us
def __ge__(self, other):
return self._us >= other._us
def __gt__(self, other):
return self._us > other._us
def __bool__(self):
return self._us != 0
def __str__(self):
return self._format(0x40)
def __hash__(self):
if not hasattr(self, "_hash"):
self._hash = hash(self._us)
return self._hash
def isoformat(self):
return self._format(0)
def _format(self, spec=0):
if self._us >= 0:
td = self
g = ""
else:
td = -self
g = "-"
d, h, m, s, us = td._tuple(5)
ms, us = divmod(us, 1000)
r = ""
if spec & 0x40:
spec &= ~0x40
hr = str(h)
else:
hr = f"{h:02d}"
if spec & 0x20:
spec &= ~0x20
spec |= 0x10
r += "UTC"
if spec & 0x10:
spec &= ~0x10
if not g:
g = "+"
if d:
p = "s" if d > 1 else ""
r += f"{g}{d} day{p}, "
g = ""
if spec == 0:
spec = 5 if (ms or us) else 3
if spec >= 1 or h:
r += f"{g}{hr}"
if spec >= 2 or m:
r += f":{m:02d}"
if spec >= 3 or s:
r += f":{s:02d}"
if spec >= 4 or ms:
r += f".{ms:03d}"
if spec >= 5 or us:
r += f"{us:03d}"
return r
def tuple(self):
return self._tuple(5)
def _tuple(self, n):
d, us = divmod(self._us, 86_400_000_000)
if n == 2:
return d, us
s, us = divmod(us, 1_000_000)
if n == 3:
return d, s, us
h, s = divmod(s, 3600)
m, s = divmod(s, 60)
return d, h, m, s, us
timedelta.min = timedelta(days=-999_999_999)
timedelta.max = timedelta(days=999_999_999, hours=23, minutes=59, seconds=59, microseconds=999_999)
timedelta.resolution = timedelta(microseconds=1)
class tzinfo:
# abstract class
def tzname(self, dt):
raise NotImplementedError
def utcoffset(self, dt):
raise NotImplementedError
def dst(self, dt):
raise NotImplementedError
def fromutc(self, dt):
if dt._tz is not self:
raise ValueError
# See original datetime.py for an explanation of this algorithm.
dtoff = dt.utcoffset()
dtdst = dt.dst()
delta = dtoff - dtdst
if delta:
dt += delta
dtdst = dt.dst()
return dt + dtdst
def isoformat(self, dt):
return self.utcoffset(dt)._format(0x12)
class timezone(tzinfo):
def __init__(self, offset, name=None):
if not (abs(offset._us) < 86_400_000_000):
raise ValueError
self._offset = offset
self._name = name
def __repr__(self):
return "datetime.timezone({}, {})".format(repr(self._offset), repr(self._name))
def __eq__(self, other):
if isinstance(other, timezone):
return self._offset == other._offset
return NotImplemented
def __str__(self):
return self.tzname(None)
def __hash__(self):
if not hasattr(self, "_hash"):
self._hash = hash((self._offset, self._name))
return self._hash
def utcoffset(self, dt):
return self._offset
def dst(self, dt):
return None
def tzname(self, dt):
if self._name:
return self._name
return self._offset._format(0x22)
def fromutc(self, dt):
return dt + self._offset
timezone.utc = timezone(timedelta(0))
def _date(y, m, d):
if MINYEAR <= y <= MAXYEAR and 1 <= m <= 12 and 1 <= d <= _dim(y, m):
return _ymd2o(y, m, d)
elif y == 0 and m == 0 and 1 <= d <= 3_652_059:
return d
else:
raise ValueError
def _iso2d(s): # ISO -> date
if len(s) < 10 or s[4] != "-" or s[7] != "-":
raise ValueError
return int(s[0:4]), int(s[5:7]), int(s[8:10])
def _d2iso(o): # date -> ISO
return "%04d-%02d-%02d" % _o2ymd(o)
class date:
def __init__(self, year, month, day):
self._ord = _date(year, month, day)
@classmethod
def fromtimestamp(cls, ts):
return cls(*_tmod.localtime(ts)[:3])
@classmethod
def today(cls):
return cls(*_tmod.localtime()[:3])
@classmethod
def fromordinal(cls, n):
return cls(0, 0, n)
@classmethod
def fromisoformat(cls, s):
return cls(*_iso2d(s))
@property
def year(self):
return self.tuple()[0]
@property
def month(self):
return self.tuple()[1]
@property
def day(self):
return self.tuple()[2]
def toordinal(self):
return self._ord
def timetuple(self):
y, m, d = self.tuple()
yday = _dbm(y, m) + d
return (y, m, d, 0, 0, 0, self.weekday(), yday, -1)
def replace(self, year=None, month=None, day=None):
year_, month_, day_ = self.tuple()
if year is None:
year = year_
if month is None:
month = month_
if day is None:
day = day_
return date(year, month, day)
def __add__(self, other):
return date.fromordinal(self._ord + other.days)
def __sub__(self, other):
if isinstance(other, date):
return timedelta(days=self._ord - other._ord)
else:
return date.fromordinal(self._ord - other.days)
def __eq__(self, other):
if isinstance(other, date):
return self._ord == other._ord
else:
return False
def __le__(self, other):
return self._ord <= other._ord
def __lt__(self, other):
return self._ord < other._ord
def __ge__(self, other):
return self._ord >= other._ord
def __gt__(self, other):
return self._ord > other._ord
def weekday(self):
return (self._ord + 6) % 7
def isoweekday(self):
return self._ord % 7 or 7
def isoformat(self):
return _d2iso(self._ord)
def __repr__(self):
return "datetime.date(0, 0, {})".format(self._ord)
__str__ = isoformat
def __hash__(self):
if not hasattr(self, "_hash"):
self._hash = hash(self._ord)
return self._hash
def tuple(self):
return _o2ymd(self._ord)
date.min = date(MINYEAR, 1, 1)
date.max = date(MAXYEAR, 12, 31)
date.resolution = timedelta(days=1)
def _time(h, m, s, us, fold):
if (
0 <= h < 24
and 0 <= m < 60
and 0 <= s < 60
and 0 <= us < 1_000_000
and (fold == 0 or fold == 1)
) or (h == 0 and m == 0 and s == 0 and 0 < us < 86_400_000_000):
return timedelta(0, s, us, 0, m, h)
else:
raise ValueError
def _iso2t(s):
hour = 0
minute = 0
sec = 0
usec = 0
tz_sign = ""
tz_hour = 0
tz_minute = 0
tz_sec = 0
tz_usec = 0
l = len(s)
i = 0
if l < 2:
raise ValueError
i += 2
hour = int(s[i - 2 : i])
if l > i and s[i] == ":":
i += 3
if l - i < 0:
raise ValueError
minute = int(s[i - 2 : i])
if l > i and s[i] == ":":
i += 3
if l - i < 0:
raise ValueError
sec = int(s[i - 2 : i])
if l > i and s[i] == ".":
i += 4
if l - i < 0:
raise ValueError
usec = 1000 * int(s[i - 3 : i])
if l > i and s[i] != "+":
i += 3
if l - i < 0:
raise ValueError
usec += int(s[i - 3 : i])
if l > i:
if s[i] not in "+-":
raise ValueError
tz_sign = s[i]
i += 6
if l - i < 0:
raise ValueError
tz_hour = int(s[i - 5 : i - 3])
tz_minute = int(s[i - 2 : i])
if l > i and s[i] == ":":
i += 3
if l - i < 0:
raise ValueError
tz_sec = int(s[i - 2 : i])
if l > i and s[i] == ".":
i += 7
if l - i < 0:
raise ValueError
tz_usec = int(s[i - 6 : i])
if l != i:
raise ValueError
if tz_sign:
td = timedelta(hours=tz_hour, minutes=tz_minute, seconds=tz_sec, microseconds=tz_usec)
if tz_sign == "-":
td = -td
tz = timezone(td)
else:
tz = None
return hour, minute, sec, usec, tz
def _t2iso(td, timespec, dt, tz):
s = td._format(_TIME_SPEC.index(timespec))
if tz is not None:
s += tz.isoformat(dt)
return s
class time:
def __init__(self, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0):
self._td = _time(hour, minute, second, microsecond, fold)
self._tz = tzinfo
self._fd = fold
@classmethod
def fromisoformat(cls, s):
return cls(*_iso2t(s))
@property
def hour(self):
return self.tuple()[0]
@property
def minute(self):
return self.tuple()[1]
@property
def second(self):
return self.tuple()[2]
@property
def microsecond(self):
return self.tuple()[3]
@property
def tzinfo(self):
return self._tz
@property
def fold(self):
return self._fd
def replace(
self, hour=None, minute=None, second=None, microsecond=None, tzinfo=True, *, fold=None
):
h, m, s, us, tz, fl = self.tuple()
if hour is None:
hour = h
if minute is None:
minute = m
if second is None:
second = s
if microsecond is None:
microsecond = us
if tzinfo is True:
tzinfo = tz
if fold is None:
fold = fl
return time(hour, minute, second, microsecond, tzinfo, fold=fold)
def isoformat(self, timespec="auto"):
return _t2iso(self._td, timespec, None, self._tz)
def __repr__(self):
return "datetime.time(microsecond={}, tzinfo={}, fold={})".format(
self._td._us, repr(self._tz), self._fd
)
__str__ = isoformat
def __bool__(self):
return True
def __eq__(self, other):
if (self._tz == None) ^ (other._tz == None):
return False
return self._sub(other) == 0
def __le__(self, other):
return self._sub(other) <= 0
def __lt__(self, other):
return self._sub(other) < 0
def __ge__(self, other):
return self._sub(other) >= 0
def __gt__(self, other):
return self._sub(other) > 0
def _sub(self, other):
tz1 = self._tz
if (tz1 is None) ^ (other._tz is None):
raise TypeError
us1 = self._td._us
us2 = other._td._us
if tz1 is not None:
os1 = self.utcoffset()._us
os2 = other.utcoffset()._us
if os1 != os2:
us1 -= os1
us2 -= os2
return us1 - us2
def __hash__(self):
if not hasattr(self, "_hash"):
# fold doesn't make any difference
self._hash = hash((self._td, self._tz))
return self._hash
def utcoffset(self):
return None if self._tz is None else self._tz.utcoffset(None)
def dst(self):
return None if self._tz is None else self._tz.dst(None)
def tzname(self):
return None if self._tz is None else self._tz.tzname(None)
def tuple(self):
d, h, m, s, us = self._td.tuple()
return h, m, s, us, self._tz, self._fd
time.min = time(0)
time.max = time(23, 59, 59, 999_999)
time.resolution = timedelta.resolution
class datetime:
def __init__(
self, year, month, day, hour=0, minute=0, second=0, microsecond=0, tzinfo=None, *, fold=0
):
self._d = _date(year, month, day)
self._t = _time(hour, minute, second, microsecond, fold)
self._tz = tzinfo
self._fd = fold
@classmethod
def fromtimestamp(cls, ts, tz=None):
if isinstance(ts, float):
ts, us = divmod(round(ts * 1_000_000), 1_000_000)
else:
us = 0
if tz is None:
raise NotImplementedError
else:
dt = cls(*_tmod.gmtime(ts)[:6], microsecond=us, tzinfo=tz)
dt = tz.fromutc(dt)
return dt
@classmethod
def now(cls, tz=None):
return cls.fromtimestamp(_tmod.time(), tz)
@classmethod
def fromordinal(cls, n):
return cls(0, 0, n)
@classmethod
def fromisoformat(cls, s):
d = _iso2d(s)
if len(s) <= 12:
return cls(*d)
t = _iso2t(s[11:])
return cls(*(d + t))
@classmethod
def combine(cls, date, time, tzinfo=None):
return cls(
0, 0, date.toordinal(), 0, 0, 0, time._td._us, tzinfo or time._tz, fold=time._fd
)
@property
def year(self):
return _o2ymd(self._d)[0]
@property
def month(self):
return _o2ymd(self._d)[1]
@property
def day(self):
return _o2ymd(self._d)[2]
@property
def hour(self):
return self._t.tuple()[1]
@property
def minute(self):
return self._t.tuple()[2]
@property
def second(self):
return self._t.tuple()[3]
@property
def microsecond(self):
return self._t.tuple()[4]
@property
def tzinfo(self):
return self._tz
@property
def fold(self):
return self._fd
def __add__(self, other):
us = self._t._us + other._us
d, us = divmod(us, 86_400_000_000)
d += self._d
return datetime(0, 0, d, 0, 0, 0, us, self._tz)
def __sub__(self, other):
if isinstance(other, timedelta):
return self.__add__(-other)
elif isinstance(other, datetime):
d, us = self._sub(other)
return timedelta(d, 0, us)
else:
raise TypeError
def _sub(self, other):
# Subtract two datetime instances.
tz1 = self._tz
if (tz1 is None) ^ (other._tz is None):
raise TypeError
dt1 = self
dt2 = other
if tz1 is not None:
os1 = dt1.utcoffset()
os2 = dt2.utcoffset()
if os1 != os2:
dt1 -= os1
dt2 -= os2
D = dt1._d - dt2._d
us = dt1._t._us - dt2._t._us
d, us = divmod(us, 86_400_000_000)
return D + d, us
def __eq__(self, other):
if (self._tz == None) ^ (other._tz == None):
return False
return self._cmp(other) == 0
def __le__(self, other):
return self._cmp(other) <= 0
def __lt__(self, other):
return self._cmp(other) < 0
def __ge__(self, other):
return self._cmp(other) >= 0
def __gt__(self, other):
return self._cmp(other) > 0
def _cmp(self, other):
# Compare two datetime instances.
d, us = self._sub(other)
if d < 0:
return -1
if d > 0:
return 1
if us < 0:
return -1
if us > 0:
return 1
return 0
def date(self):
return date.fromordinal(self._d)
def time(self):
return time(microsecond=self._t._us, fold=self._fd)
def timetz(self):
return time(microsecond=self._t._us, tzinfo=self._tz, fold=self._fd)
def replace(
self,
year=None,
month=None,
day=None,
hour=None,
minute=None,
second=None,
microsecond=None,
tzinfo=True,
*,
fold=None,
):
Y, M, D, h, m, s, us, tz, fl = self.tuple()
if year is None:
year = Y
if month is None:
month = M
if day is None:
day = D
if hour is None:
hour = h
if minute is None:
minute = m
if second is None:
second = s
if microsecond is None:
microsecond = us
if tzinfo is True:
tzinfo = tz
if fold is None:
fold = fl
return datetime(year, month, day, hour, minute, second, microsecond, tzinfo, fold=fold)
def astimezone(self, tz=None):
if self._tz is tz:
return self
_tz = self._tz
if _tz is None:
raise NotImplementedError
else:
os = _tz.utcoffset(self)
utc = self - os
utc = utc.replace(tzinfo=tz)
return tz.fromutc(utc)
def utcoffset(self):
return None if self._tz is None else self._tz.utcoffset(self)
def dst(self):
return None if self._tz is None else self._tz.dst(self)
def tzname(self):
return None if self._tz is None else self._tz.tzname(self)
def timetuple(self):
if self._tz is None:
conv = _tmod.gmtime
epoch = datetime.EPOCH.replace(tzinfo=None)
else:
conv = _tmod.localtime
epoch = datetime.EPOCH
return conv(round((self - epoch).total_seconds()))
def toordinal(self):
return self._d
def timestamp(self):
if self._tz is None:
raise NotImplementedError
else:
return (self - datetime.EPOCH).total_seconds()
def weekday(self):
return (self._d + 6) % 7
def isoweekday(self):
return self._d % 7 or 7
def isoformat(self, sep="T", timespec="auto"):
return _d2iso(self._d) + sep + _t2iso(self._t, timespec, self, self._tz)
def __repr__(self):
Y, M, D, h, m, s, us, tz, fold = self.tuple()
tz = repr(tz)
return "datetime.datetime({}, {}, {}, {}, {}, {}, {}, {}, fold={})".format(
Y, M, D, h, m, s, us, tz, fold
)
def __str__(self):
return self.isoformat(" ")
def __hash__(self):
if not hasattr(self, "_hash"):
self._hash = hash((self._d, self._t, self._tz))
return self._hash
def tuple(self):
d = _o2ymd(self._d)
t = self._t.tuple()[1:]
return d + t + (self._tz, self._fd)
datetime.EPOCH = datetime(*_tmod.gmtime(0)[:6], tzinfo=timezone.utc)