-
Notifications
You must be signed in to change notification settings - Fork 4
/
abstract_time.py
82 lines (61 loc) · 2.2 KB
/
abstract_time.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
import time
import calendar
class AbstractTime(object):
def __init__(self, string, timestamp):
self.str = string
self.timestamp = timestamp
def __repr__(self):
return self.str
def __hash__(self):
return self.timestamp
def __cmp__ (self, other):
assert not isinstance(other, str)
return self.timestamp.__cmp__(other.timestamp)
def __sub__(self, other):
assert not isinstance(other, str)
return self.timestamp - other.timestamp
def __eq__(self, other):
if other is None:
return False
assert not isinstance(other, str)
assert other is not None
if isinstance(other, int):
return self.timestamp == other
return self.timestamp == other.timestamp
def __neq__(self, other):
assert not isinstance(other, str)
assert other is not None
return self.timestamp != other.timestamp
def __lt__(self, other):
assert not isinstance(other, str)
assert other is not None
if isinstance(other, int):
return self.timestamp < other
return self.timestamp < other.timestamp
def __le__(self, other):
assert not isinstance(other, str)
assert other is not None
if isinstance(other, int):
return self.timestamp <= other
return self.timestamp <= other.timestamp
def __gt__(self, other):
assert not isinstance(other, str)
assert other is not None
if isinstance(other, int):
return self.timestamp > other
return self.timestamp > other.timestamp
def __ge__(self, other):
assert not isinstance(other, str)
assert other is not None
if isinstance(other, int):
return self.timestamp >= other
return self.timestamp >= other.timestamp
@staticmethod
def get_timestamp_from_hour(t):
return calendar.timegm(time.strptime(t, "%H:%M"))
@staticmethod
def get_timestamp_from_date_hour(d, t):
return calendar.timegm(time.strptime(d + " " + t, "%Y/%m/%d %H:%M"))
@staticmethod
def get_timestamp_from_date(d):
return calendar.timegm(time.strptime(d, "%Y/%m/%d"))