-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathDateInterval.py
64 lines (49 loc) · 1.43 KB
/
DateInterval.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
"""DateInterval.py
Convert interval strings (in the form of 1w2d, etc) to seconds, and back again.
Is not exactly about months or years (leap years in particular).
Accepts (y)ear, (b)month, (w)eek, (d)ay, (h)our, (m)inute, (s)econd.
Exports only timeEncode and timeDecode functions.
"""
__all__ = ['timeEncode', 'timeDecode']
import re
from operator import itemgetter
second = 1
minute = second*60
hour = minute*60
day = hour*24
week = day*7
month = day*30
year = day*365
timeValues = {
'y': year,
'b': month,
'w': week,
'd': day,
'h': hour,
'm': minute,
's': second,
}
timeOrdered = sorted(timeValues.items(), key=itemgetter(1), reverse=True)
def timeEncode(seconds):
"""Encode a number of seconds (representing a time interval).
Encode the number into a form like 2d1h3s.
"""
s = []
for char, amount in timeOrdered:
if seconds >= amount:
i, seconds = divmod(seconds, amount)
s.append(f'{i}{char}')
return ''.join(s)
_timeRE = re.compile(r'[0-9]+[a-zA-Z]')
def timeDecode(s):
"""Decode a number in the format 1h4d3m (1 hour, 3 days, 3 minutes).
Decode the format into a number of seconds.
"""
time = 0
for match in _timeRE.findall(s):
char = match[-1].lower()
try:
time += int(match[:-1]) * timeValues[char]
except KeyError:
raise ValueError(f'Invalid unit of time: {char}') from None
return time