-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetimeparse.py
executable file
·96 lines (75 loc) · 2.14 KB
/
datetimeparse.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
#!/usr/bin/env python
# -*- coding: utf-8 vi:noet
# Parse and pretty-print functions for datetime or timedelta
# TODO try to use isodate or dateutil
__author__ = "Jérôme Carretero <cJ@zougloub.eu>"
__license__ = "MIT"
import re, datetime
def timedelta_str(x, precision="m"):
"""
:return: string corresponding to a time delta
:param x: timedelta, native or in seconds (float)
"""
if isinstance(x, datetime.timedelta):
ts = x.total_seconds()
elif isinstance(x, float):
ts = int(round(x))
else:
raise NotImplementedError()
h = ts//3600
m = ts//60 - h*60
return "%2d:%02d" % (h, m)
def inonull(i):
"""
:return: a dict containing integer values, only if they existed
"""
return dict((key, int(value)) for (key, value) in i.items() if value is not None)
def parse_date(spec):
"""
Return a timestamp from some kind of date/time spec
Something more than datetime.datetime.strptime(ts, "%Y-%m-%d")
"""
spec = spec.lower()
try:
sp = tuple(spec.split())
out = datetime.datetime.now()
if spec != 'now':
while True:
m = re.search(r'(?P<year>\d{4})?-?(?P<month>\d{2})-(?P<day>\d{2})', spec)
if m is not None:
g = inonull(m.groupdict())
out = out.replace(**g)
break
found = False
for i in range(1, 8):
day = out - datetime.timedelta(days=i)
sh = day.strftime('%a').lower()
lo = day.strftime('%A').lower()
if sh in sp or lo in sp:
out = day
found = True
break
if found:
break
if 'yesterday' in spec:
out -= datetime.timedelta(days=1)
break
if 'today' in spec or 'this' in spec or 1:
break
while True:
m = re.search(r'(?P<hour>\d{2}):(?P<minute>\d{2}):?(?P<second>\d{2})?', spec)
if m is not None:
g = inonull(m.groupdict())
out = out.replace(**g)
break
if 'afternoon' in spec:
out = out.replace(hour=13, minute=00, second=0)
break
if 'morning' in spec:
out = out.replace(hour=9, minute=30, second=0)
break
out = out.replace(hour=0, minute=0, second=0, microsecond=0)
break
return out
except Exception as e:
raise ValueError("Spec not understood: %s (%s)" % (spec, e))