Repository navigation
Expand file tree
/
Copy pathdateobj.py
More file actions
59 lines (46 loc) · 1.8 KB
/
Copy pathdateobj.py
File metadata and controls
59 lines (46 loc) · 1.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
class Date(object):
datefmt = '{date.month}/{date.day}/{date.year}'
def __init__(self, year, month, day):
self.year = year
self.month = month
self.day = day
def __str__(self):
return self.datefmt.format(date=self)
def __repr__(self):
return '%s(%r, %r, %r)' % (self.__class__.__name__, self.year, self.month, self.day)
def __eq__(self, other):
if not isinstance(other, Date):
return False
return (self.year, self.month, self.day) == (other.year, other.month, other.day)
def __lt__(self, other):
if not isinstance(other, Date):
raise ValueError('Not comparing two dates')
return (self.year, self.month, self.day) < (other.year, other.month, other.day)
def __le__(self, other):
if not isinstance(other, Date):
raise ValueError('Not comparing two dates')
return (self.year, self.month, self.day) <= (other.year, other.month, other.day)
def __gt__(self, other):
if not isinstance(other, Date):
raise ValueError('Not comparing two dates')
return (self.year, self.month, self.day) > (other.year, other.month, other.day)
def __ge__(self, other):
if not isinstance(other, Date):
raise ValueError('Not comparing two dates')
return (self.year, self.month, self.day) => (other.year, other.month, other.day)
@classmethod
def today(cls):
import time
t = time.localtime()
self = cls.__new__(cls)
self.year = t.tm_year
self.month = t.tm_mon
self.day = t.tm_mday
return self
@classmethod
def from_timestamp(cls, unix):
import time
t = time.localtime(unix)
return Date(t.tm_year, t.tm_mon, t.tm_mday)
class ISODate(Date):
pass