blob: 51941d78f73372fdb7051e7aaff8092eba0c14b7 (
about) (
plain) (
blame)
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
|
import re, typing
import datetime as _datetime
import dateutil.parser
from .common import *
def iso8601(s: str) -> _datetime.datetime:
return dateutil.parser.parse(s)
def date_human(s: str) -> typing.Optional[_datetime.datetime]:
try:
return _datetime.datetime.strptime(s, DATE_HUMAN)
except ValueError:
return None
REGEX_PRETTYTIME = re.compile(
r"(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?", re.I)
def from_pretty_time(pretty_time: str) -> typing.Optional[int]:
seconds = 0
match = re.match(REGEX_PRETTYTIME, pretty_time)
if match:
seconds += int(match.group(1) or 0)*SECONDS_WEEKS
seconds += int(match.group(2) or 0)*SECONDS_DAYS
seconds += int(match.group(3) or 0)*SECONDS_HOURS
seconds += int(match.group(4) or 0)*SECONDS_MINUTES
seconds += int(match.group(5) or 0)
if seconds > 0:
return seconds
return None
|