-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathutime.py
346 lines (289 loc) · 12.4 KB
/
utime.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
#======================================================================
#
# utime.py - time related functions
#
# Created by skywind on 2018/08/02
# Last Modified: 2018/08/02 15:55:48
#
#======================================================================
from __future__ import print_function
import sys
import time
import datetime
#----------------------------------------------------------------------
# python 2/3 compatible
#----------------------------------------------------------------------
if sys.version_info[0] >= 3:
long = int
unicode = str
xrange = range
#----------------------------------------------------------------------
# timezone
#----------------------------------------------------------------------
class timezone(datetime.tzinfo):
"""Backport of datetime.timezone.
Notes
-----
Backport of datetime.timezone for Python 2.7, from Python 3.6
documentation (https://tinyurl.com/z4cegu9), copyright Python Software
Foundation (https://docs.python.org/3/license.html)
"""
__slots__ = '_offset', '_name'
# Sentinel value to disallow None
_Omitted = object()
def __new__(cls, offset, name=_Omitted):
if not isinstance(offset, datetime.timedelta):
raise TypeError("offset must be a timedelta")
if name is cls._Omitted:
if not offset:
return cls.utc
name = None
elif not isinstance(name, str):
raise TypeError("name must be a string")
if not cls._minoffset <= offset <= cls._maxoffset:
raise ValueError("offset must be a timedelta "
"strictly between -timedelta(hours=24) and "
"timedelta(hours=24).")
if (offset.microseconds != 0 or offset.seconds % 60 != 0):
raise ValueError("offset must be a timedelta "
"representing a whole number of minutes")
return cls._create(offset, name)
@classmethod
def _create(cls, offset, name=None):
self = datetime.tzinfo.__new__(cls)
self._offset = offset
self._name = name
return self
def __getinitargs__(self):
"""pickle support"""
if self._name is None:
return (self._offset,)
return (self._offset, self._name)
def __eq__(self, other):
if not isinstance(other, timezone):
return False
return self._offset == other._offset
def __lt__(self, other):
raise TypeError("'<' not supported between instances of"
" 'datetime.timezone' and 'datetime.timezone'")
def __hash__(self):
return hash(self._offset)
def __repr__(self):
if self is self.utc:
return '%s.%s.utc' % (self.__class__.__module__,
self.__class__.__name__)
if self._name is None:
return "%s.%s(%r)" % (self.__class__.__module__,
self.__class__.__name__,
self._offset)
return "%s.%s(%r, %r)" % (self.__class__.__module__,
self.__class__.__name__,
self._offset, self._name)
def __str__(self):
return self.tzname(None)
def utcoffset(self, dt):
if isinstance(dt, datetime.datetime) or dt is None:
return self._offset
raise TypeError("utcoffset() argument must be a datetime instance"
" or None")
def tzname(self, dt):
if isinstance(dt, datetime.datetime) or dt is None:
if self._name is None:
return self._name_from_offset(self._offset)
return self._name
raise TypeError("tzname() argument must be a datetime instance"
" or None")
def dst(self, dt):
if isinstance(dt, datetime.datetime) or dt is None:
return None
raise TypeError("dst() argument must be a datetime instance"
" or None")
def fromutc(self, dt):
if isinstance(dt, datetime.datetime):
if dt.tzinfo is not self:
raise ValueError("fromutc: dt.tzinfo "
"is not self")
return dt + self._offset
raise TypeError("fromutc() argument must be a datetime instance"
" or None")
_maxoffset = datetime.timedelta(hours=23, minutes=59)
_minoffset = -_maxoffset
@staticmethod
def _name_from_offset(delta):
if not delta:
return 'UTC'
if delta < datetime.timedelta(0):
sign = '-'
delta = -delta
else:
sign = '+'
hours, rest = divmod(delta.total_seconds(), 3600)
hours = int(hours)
minutes = rest // datetime.timedelta(minutes=1).total_seconds()
minutes = int(minutes)
return 'UTC{}{:02d}:{:02d}'.format(sign, hours, minutes)
timezone.utc = timezone._create(datetime.timedelta(0))
timezone.min = timezone._create(timezone._minoffset)
timezone.max = timezone._create(timezone._maxoffset)
timezone.cst = timezone._create(datetime.timedelta(hours = 8))
_EPOCH = datetime.datetime(1970, 1, 1, tzinfo=timezone.utc)
if sys.version_info[0] < 3:
datetime.timezone = timezone
#----------------------------------------------------------------------
# Tools
#----------------------------------------------------------------------
DATETIME_FMT = '%Y-%m-%d %H:%M:%S'
#----------------------------------------------------------------------
# 1551884065 to datetime.dateime(2019, 3, 6, 22, 54, 25)
#----------------------------------------------------------------------
def timestamp_to_datetime (ts, tz = None):
return datetime.datetime.fromtimestamp(ts, tz)
#----------------------------------------------------------------------
# datetime.datetime(2019, 3, 6, 22, 54, 25) -> 1551884065
#----------------------------------------------------------------------
def datetime_to_timestamp (dt):
if hasattr(dt, 'timestamp'):
return dt.timestamp()
epoch = datetime.datetime.fromtimestamp(0, dt.tzinfo)
return (dt - epoch).total_seconds()
#----------------------------------------------------------------------
# datetime.datetime(2019, 3, 6, 22, 54, 25) -> '2019-03-06 22:54:25'
#----------------------------------------------------------------------
def datetime_to_string (dt, fmt = None):
return dt.strftime(fmt and fmt or DATETIME_FMT)
#----------------------------------------------------------------------
# '2019-03-06 22:54:25' -> datetime.datetime(2019, 3, 6, 22, 54, 25)
#----------------------------------------------------------------------
def string_to_datetime (text, tz = None, fmt = None):
dt = datetime.datetime.strptime(text, fmt and fmt or DATETIME_FMT)
if tz:
if hasattr(tz, 'localize'):
# in case, we have pytz
dt = tz.localize(dt)
else:
dt = dt.replace(tzinfo = tz)
return dt
#----------------------------------------------------------------------
# 1551884065 -> '2019-03-06 22:54:25'
#----------------------------------------------------------------------
def timestamp_to_string (ts, tz = None):
return datetime_to_string(timestamp_to_datetime(ts, tz))
#----------------------------------------------------------------------
# '2019-03-06 22:54:25' -> 1551884065
#----------------------------------------------------------------------
def string_to_timestamp (text, tz = None):
return datetime_to_timestamp(string_to_datetime(text, tz))
#----------------------------------------------------------------------
# 1551884065 -> '2019-03-06T22:54:25.000Z'
#----------------------------------------------------------------------
def timestamp_to_iso (ts):
dt = timestamp_to_datetime(ts, timezone.utc)
return dt.strftime('%Y-%m-%dT%H:%M:%S.%fZ')
#----------------------------------------------------------------------
# '2019-03-06T22:54:25.000Z' -> 1551884065
#----------------------------------------------------------------------
def iso_to_timestamp (iso):
iso = iso.strip()
if iso[-1:].upper() != 'Z':
raise ValueError('require an ISO 8601 UTC format')
if iso[10:11].upper() != 'T':
raise ValueError('require an ISO 8601 UTC format')
if '.' in iso:
if len(iso) >= 28:
if iso[19:20] == '.':
iso = iso[:26] + 'Z'
else:
raise ValueError('bad iso 8601 format')
dt = datetime.datetime.strptime(iso[:-1], '%Y-%m-%dT%H:%M:%S.%f')
else:
if len(iso) == 17:
iso = iso[:16] + ':00Z'
elif len(iso) == 12:
iso = iso[:11] + '00:00:00Z'
dt = datetime.datetime.strptime(iso[:-1], '%Y-%m-%dT%H:%M:%S')
dt = dt.replace(tzinfo = timezone.utc)
return datetime_to_timestamp(dt)
#----------------------------------------------------------------------
# read timestamp from various format
#----------------------------------------------------------------------
def read_timestamp (timestamp, tz = None):
if isinstance(timestamp, datetime.datetime):
return datetime_to_timestamp(timestamp)
elif isinstance(timestamp, int) or isinstance(timestamp, long):
return timestamp
elif isinstance(timestamp, float):
return timestamp
elif len(timestamp) == 10 and timestamp.isdigit():
return int(timestamp)
elif len(timestamp) == 13 and timestamp.isdigit():
return float(timestamp) * 0.001
elif timestamp.find('.') == 10 and timestamp[:10].isdigit():
return float(timestamp)
elif timestamp[:1].isdigit() and timestamp[4:5] == '-':
if len(timestamp) == 19:
return string_to_timestamp(timestamp, tz)
elif len(timestamp) == 16:
return string_to_timestamp(timestamp + ':00', tz)
elif len(timestamp) == 10:
return string_to_timestamp(timestamp + ' 00:00:00', tz)
if timestamp[:1].isdigit() and timestamp[-1:] == 'Z':
return iso_to_timestamp(timestamp)
return timestamp
#----------------------------------------------------------------------
# compact string: YYYYMMDDHHMM
#----------------------------------------------------------------------
def compact_from_timestamp(ts, tz = None, seconds = False):
fmt1 = '%Y%m%d%H%M'
fmt2 = '%Y%m%d%H%M%S'
dt = timestamp_to_datetime(ts, tz)
return dt.strftime(seconds and fmt2 or fmt1)
#----------------------------------------------------------------------
# utc to local without tz
#----------------------------------------------------------------------
def utc_to_local(utc):
epoch = time.mktime(utc.timetuple())
offset = datetime.datetime.fromtimestamp(epoch) - \
datetime.datetime.utcfromtimestamp(epoch)
return utc + offset
#----------------------------------------------------------------------
# local to utc
#----------------------------------------------------------------------
def local_to_utc(local):
epoch = time.mktime(local.timetuple())
offset = datetime.datetime.fromtimestamp(epoch) - \
datetime.datetime.utcfromtimestamp(epoch)
return local - offset
#----------------------------------------------------------------------
# testing case
#----------------------------------------------------------------------
if __name__ == '__main__':
def test1():
ts = time.time()
uc = datetime.datetime.utcfromtimestamp(ts)
uc = timestamp_to_datetime(ts, timezone.utc)
print(ts)
print(uc.tzinfo)
print(datetime_to_timestamp(uc))
return 0
def test2():
ts = time.time()
print(ts)
text = timestamp_to_iso(ts)
print(timestamp_to_iso(ts))
print(iso_to_timestamp(text))
tt = '2018-08-31T09:45:56.0000000Z'
print(len(tt))
ts = iso_to_timestamp(tt)
dt = timestamp_to_datetime(ts, timezone.utc)
print(dt)
print(read_timestamp('2018-01-01 12:00:32'))
def test3():
dt = datetime.datetime.fromtimestamp(time.time())
print(dt)
utc = local_to_utc(dt)
print(utc)
print(utc_to_local(utc))
return 0
test3()