1 |
817
|
aaronmk
|
# Date/time manipulation
|
2 |
|
|
|
3 |
|
|
import datetime
|
4 |
1040
|
aaronmk
|
import dateutil.tz
|
5 |
1365
|
aaronmk
|
import strings
|
6 |
817
|
aaronmk
|
import time
|
7 |
|
|
|
8 |
1040
|
aaronmk
|
utc = dateutil.tz.tzutc()
|
9 |
|
|
local = dateutil.tz.tzlocal()
|
10 |
817
|
aaronmk
|
|
11 |
1040
|
aaronmk
|
def naive2utc(datetime_):
|
12 |
|
|
assert datetime_.tzinfo == None
|
13 |
|
|
return datetime_.replace(tzinfo=utc)
|
14 |
817
|
aaronmk
|
|
15 |
1040
|
aaronmk
|
def aware2local(datetime_): return datetime_.astimezone(local)
|
16 |
|
|
|
17 |
|
|
def from_timestamp(utc_timestamp):
|
18 |
|
|
return naive2utc(datetime.datetime.utcfromtimestamp(utc_timestamp))
|
19 |
|
|
|
20 |
1041
|
aaronmk
|
def timestamp(datetime_):
|
21 |
|
|
datetime_ = aware2local(datetime_)
|
22 |
|
|
return int(time.mktime(datetime_.timetuple())) + datetime_.microsecond/1e6
|
23 |
1040
|
aaronmk
|
|
24 |
|
|
epoch = from_timestamp(0)
|
25 |
|
|
|
26 |
1263
|
aaronmk
|
def total_seconds(timedelta_): return timestamp(epoch + timedelta_)
|
27 |
|
|
|
28 |
1040
|
aaronmk
|
def now(): return datetime.datetime.now(utc)
|
29 |
|
|
|
30 |
817
|
aaronmk
|
def strftime(format, datetime_):
|
31 |
|
|
'''datetime.strftime() can't handle years before 1900'''
|
32 |
1040
|
aaronmk
|
return (datetime_.replace(year=epoch.year, day=1).strftime(format
|
33 |
845
|
aaronmk
|
.replace('%Y', '%%Y')
|
34 |
|
|
.replace('%d', '%%d')
|
35 |
|
|
)
|
36 |
|
|
.replace('%Y', '%04d' % datetime_.year)
|
37 |
|
|
.replace('%d', '%02d' % datetime_.day)
|
38 |
|
|
)
|
39 |
1040
|
aaronmk
|
|
40 |
1263
|
aaronmk
|
def strtotime(str_, default=epoch):
|
41 |
|
|
import dateutil.parser
|
42 |
|
|
return dateutil.parser.parse(str_, default=default)
|
43 |
1365
|
aaronmk
|
|
44 |
|
|
def could_be_year(str_): return str_.isdigit() and len(str_) == 4
|
45 |
|
|
|
46 |
|
|
def could_be_day(str_): return str_.isdigit() and len(str_) <= 2
|
47 |
|
|
|
48 |
|
|
def parse_date_range(str_, range_sep='-', part_sep=' '):
|
49 |
1370
|
aaronmk
|
default = (str_, None)
|
50 |
1365
|
aaronmk
|
# range_sep might be used as date part separator instead
|
51 |
1370
|
aaronmk
|
if str_.find(part_sep) < 0: return default
|
52 |
1365
|
aaronmk
|
|
53 |
|
|
start, sep, end = str_.partition(range_sep)
|
54 |
1370
|
aaronmk
|
if sep == '': return default # not a range
|
55 |
|
|
start, end = (strings.single_space(d).split(part_sep) for d in (start, end))
|
56 |
1365
|
aaronmk
|
|
57 |
|
|
# Has form M D1-D2 or M D1-D2 Y (not M1 Y1-M2 Y2 or M1 D1-M2 D2)
|
58 |
|
|
if len(start) == 2 and (len(end) == 1 or (
|
59 |
|
|
len(end) == 2 and could_be_day(start[-1]) and could_be_day(end[0])
|
60 |
|
|
and could_be_year(end[-1])
|
61 |
|
|
)):
|
62 |
|
|
end.insert(0, start[0]) # make end fully specified
|
63 |
|
|
ct_diff = len(end) - len(start)
|
64 |
|
|
# Has form D1-D2 M Y, M1 D1-M2 D2 Y, M1-M2 Y, etc.
|
65 |
|
|
if ct_diff > 0: start += end[-ct_diff:] # make start fully specified
|
66 |
|
|
# Other forms are invalid and will be left as-is
|
67 |
|
|
|
68 |
1370
|
aaronmk
|
return tuple(part_sep.join(d) for d in (start, end))
|