Created
June 11, 2021 11:50
-
-
Save Synthetica9/77c39e52a51ec7ed64f670014f0fbf89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import timedelta | |
import re | |
def parseTime(s): | |
m = re.match(r'^(\d+) *([a-z]+)$', s) | |
if m is None: | |
raise ValueError("couldn't parse time string") | |
units = 'seconds minutes hours days weeks'.split() | |
num = float(m[1]) | |
for unit in units: | |
if unit.startswith(m[2]): | |
break | |
else: | |
raise ValueError(f'Unknown unit {m[2]}') | |
return timedelta(**{unit: num}) | |
print(parseTime('1d')) | |
print(parseTime('5m')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment