-
Notifications
You must be signed in to change notification settings - Fork 0
/
itime.py
212 lines (177 loc) · 7.02 KB
/
itime.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
#!/usr/bin/env python
import sys, time, operator, datetime
""""
$ pip2 install pytz tzlocal
"""
try:
import pytz
timezones = pytz.all_timezones
except:
print("ERROR: `pytz` package not exists!")
sys.exit(0)
try:
import tzlocal
default_timezone = tzlocal.get_localzone().zone
except:
print("WARN: `tzlocal` package not exists! ")
default_timezone = "Asia/Shanghai"
class Datetime:
_datetime = None
def __init__(self, *args, **kwargs):
if "_datetime" in kwargs and isinstance(kwargs["_datetime"], datetime.datetime):
if isinstance(kwargs["_datetime"].tzinfo, datetime.tzinfo):
self._datetime = kwargs["_datetime"]
else:
raise ValueError("tzinfo error.")
elif len(args) > 0 and isinstance(args[0], datetime.datetime):
if isinstance(args[0].tzinfo, datetime.tzinfo):
self._datetime = args[0]
else:
raise ValueError("tzinfo error.")
else:
_datetime = datetime.datetime(*args)
if not isinstance(_datetime.tzinfo, datetime.tzinfo):
if "timezone" in kwargs and kwargs["timezone"] in timezones:
timezone = kwargs["timezone"]
else:
timezone = default_timezone
tz = pytz.timezone(timezone)
_datetime = tz.localize(_datetime)
self._datetime = _datetime
@classmethod
def now(cls, timezone=None, *args, **kwargs):
return cls.from_timestamp(time.time(), timezone=timezone)
def tzinfo(self):
return self._datetime.tzinfo
def utcoffset(self):
return self._datetime.utcoffset().total_seconds()
@classmethod
def from_datetime(cls, _datetime, timezone=None, *args, **kwargs):
assert( isinstance(_datetime, datetime.datetime) )
if not isinstance(_datetime.tzinfo, datetime.tzinfo):
if timezone not in timezones:
timezone = default_timezone
tz = pytz.timezone(timezone)
_datetime = tz.localize(_datetime)
return Datetime(_datetime=_datetime)
def to_datetime(self):
return self._datetime
@classmethod
def from_timestamp(cls, timestamp, timezone=None, *args, **kwargs):
if timezone not in timezones:
timezone = default_timezone
tz = pytz.timezone(timezone)
_datetime = datetime.datetime.fromtimestamp(timestamp, tz)
return Datetime(_datetime=_datetime)
def to_timestamp(self):
return float(self.to_string("%s.%f"))
def to_utc_timestamp(self):
return self.to_timestamp() + self.utcoffset()
@classmethod
def from_timestruct(cls, time_struct, timezone=None):
# time_struct = time.localtime()
# time_tuple = (time_struct.tm_year, time_struct.tm_mon, time_struct.tm_mday,
# time_struct.tm_hour, time_struct.tm_min, time_struct.tm_sec,
# time_struct.tm_wday, time_struct.tm_yday, time_struct.tm_isdst)
return cls.from_timestamp(time.mktime(time_struct), timezone=timezone)
def to_timestruct(self):
return self._datetime.timetuple()
def to_isoformat(self):
return self._datetime.isoformat()
@classmethod
def from_string(cls, fmt, date_str, timezone=None):
d = datetime.datetime.strptime(fmt, date_str)
return cls.from_datetime(d, timezone=timezone)
def to_string(self, *args, **kwargs):
if len(args) > 0:
return self._datetime.strftime(args[0])
else:
return self.to_isoformat()
def __lt__(self, other):
return operator.lt(self.to_timestamp(), other.to_timestamp())
def __le__(self, other):
return operator.le(self.to_timestamp(), other.to_timestamp())
def __eq__(self, other):
return operator.eq(self.to_timestamp(), other.to_timestamp())
def __ne__(self, other):
return operator.ne(self.to_timestamp(), other.to_timestamp())
def __ge__(self, other):
return operator.ge(self.to_timestamp(), other.to_timestamp())
def __gt__(self, other):
return operator.gt(self.to_timestamp(), other.to_timestamp())
def __add__(self, other):
return operator.add(self.to_timestamp(), other.to_timestamp())
def __sub__(self, other):
# "%.15f" % float_num
return operator.sub(self.to_timestamp(), other.to_timestamp())
def __mul__(self, other):
return operator.mul(self.to_timestamp(), other.to_timestamp())
def __floordiv__(self, other):
return operator.floordiv(self.to_timestamp(), other.to_timestamp())
def __div__(self, other):
return operator.div(self.to_timestamp(), other.to_timestamp())
def __truediv__(self, other):
return operator.truediv(self.to_timestamp(), other.to_timestamp())
def __mod__(self, other):
return operator.mod(self.to_timestamp(), other.to_timestamp())
def __divmod___(self, other):
return divmod(self.to_timestamp(), other.to_timestamp())
def __pow__(self, other):
return operator.pow(self.to_timestamp(), other.to_timestamp())
def __and__(self, other):
return operator.and_(self.to_timestamp(), other.to_timestamp())
def __or__(self, other):
return operator.or_(self.to_timestamp(), other.to_timestamp())
def __str__(self):
return self.to_string()
def __repr__(self):
return "<Datetime datetime=%s sec=%f>" % (self.to_string(), self.to_timestamp())
class Duration:
btime = None
def __init__(self, btime=None, *args, **kwargs):
if isinstance(btime, Datetime):
self.btime = btime
else:
self.btime = Datetime.now()
def elapsed(self, *args, **kwargs):
etime = Datetime.now()
return etime - self.btime
def now(timezone=None, *args, **kwargs):
return Datetime.now(timezone=timezone)
def strftime(fmt, _datetime, timezone=None):
if isinstance(_datetime, Datetime):
return _datetime.to_string(fmt)
elif isinstance(_datetime, datetime.datetime):
dt = Datetime.from_datetime(_datetime, timezone=timezone)
return dt.to_string(fmt)
else:
raise ValueError("Ooops ...")
def strptime(fmt, _date_str, timezone=None):
return Datetime.from_string(fmt, _date_str, timezone=timezone)
def test_datetime_now():
b = Datetime.now()
time.sleep(1)
e = Datetime.now()
print( repr(b) )
print( repr(e) )
print(e-b)
def test_init_datetime():
dt0 = datetime.datetime(2016, 11, 13, 0, 0, 0, 0)
print(dt0)
dt = Datetime(2016, 11, 13, timezone="Asia/Shanghai")
dt1 = Datetime(2016, 11, 13, timezone="Asia/Tokyo")
dt2 = Datetime(2016, 11, 13, 0, 0, 0, 0 timezone="UTC")
dt3 = Datetime(2016, 11, 13, 0, 0, 0, 0 timezone="US/Hawaii")
dt4 = Datetime(2016, 11, 13, 0, 0, 0, 0 timezone="America/Los_Angeles")
print(dt)
print(dt1)
print(dt2)
print(dt3)
print(dt4)
def test():
test_init_datetime()
test_datetime_now()
def main():
test()
if __name__ == '__main__':
main()