/
Date.py
48 lines (36 loc) · 1.09 KB
/
Date.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
import functools
import datetime
from day_of import day_of
from calend_utils import as_days
@functools.total_ordering
class Date:
def __init__(self, year, month, day):
self._year = year
self._month = month
self._day = day
def __sub__(self, other):
d1 = datetime.date(*tuple(self))
d2 = datetime.date(*tuple(other))
return (d1 - d2).days
def day_of(self):
return day_of(self._day, self._month, self._year)
def sub(self, other):
return int(self) - int(other)
def __iter__(self):
yield from (self._year, self._month, self._day)
def __eq__(self, other):
return tuple(self) == tuple(other)
def __le__(self, other):
return tuple(self) < tuple(other)
def __str__(self):
return '%02d/%02d/%04d' % tuple(self)
def __int__(self):
return as_days(*tuple(self))
if __name__ == "__main__":
d1 = Date(1983, 1, 3)
d2 = Date(2017, 10, 26)
print(d1, tuple(d1), d1 > d2, min(d1,d2), d1-d2)
print(d1 - d2)
print(d1.day_of())
print(d1.sub(d2))
print(int(d1))