diff options
| author | 2020-01-30 21:12:31 +0000 | |
|---|---|---|
| committer | 2020-01-30 21:20:56 +0000 | |
| commit | d0d5cc4d0866d44eed652221940035d855296253 (patch) | |
| tree | 1683eb3aaf56b457835fb0229379530a7013280d /src/utils/datetime/parse.py | |
| parent | replace "/" in channel logfile names with "," (diff) | |
| signature | ||
split utils.datetime out in to .parse and .format
Diffstat (limited to 'src/utils/datetime/parse.py')
| -rw-r--r-- | src/utils/datetime/parse.py | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/src/utils/datetime/parse.py b/src/utils/datetime/parse.py new file mode 100644 index 00000000..8e372f8c --- /dev/null +++ b/src/utils/datetime/parse.py @@ -0,0 +1,25 @@ +import re, typing +import datetime as _datetime +import dateutil.parser +from .common import * + +def iso8601(s: str) -> _datetime.datetime: + return dateutil.parser.parse(s) + +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 |
