make utils.from_pretty_time() format much stricter
This commit is contained in:
parent
786117b998
commit
007bb78d30
1 changed files with 11 additions and 12 deletions
|
@ -63,7 +63,8 @@ def time_unit(seconds: int) -> typing.Tuple[int, str]:
|
||||||
unit = "%ss" % unit # pluralise the unit
|
unit = "%ss" % unit # pluralise the unit
|
||||||
return (since, unit)
|
return (since, unit)
|
||||||
|
|
||||||
REGEX_PRETTYTIME = re.compile("\d+[wdhms]", re.I)
|
REGEX_PRETTYTIME = re.compile(
|
||||||
|
r"(?:(\d+)w)?(?:(\d+)d)?(?:(\d+)h)?(?:(\d+)m)?(?:(\d+)s)?", re.I)
|
||||||
|
|
||||||
SECONDS_MINUTES = 60
|
SECONDS_MINUTES = 60
|
||||||
SECONDS_HOURS = SECONDS_MINUTES*60
|
SECONDS_HOURS = SECONDS_MINUTES*60
|
||||||
|
@ -72,17 +73,15 @@ SECONDS_WEEKS = SECONDS_DAYS*7
|
||||||
|
|
||||||
def from_pretty_time(pretty_time: str) -> typing.Optional[int]:
|
def from_pretty_time(pretty_time: str) -> typing.Optional[int]:
|
||||||
seconds = 0
|
seconds = 0
|
||||||
for match in re.findall(REGEX_PRETTYTIME, pretty_time):
|
|
||||||
number, unit = int(match[:-1]), match[-1].lower()
|
match = re.match(REGEX_PRETTYTIME, pretty_time)
|
||||||
if unit == "m":
|
if match:
|
||||||
number = number*SECONDS_MINUTES
|
seconds += int(match.group(1) or 0)*SECONDS_WEEKS
|
||||||
elif unit == "h":
|
seconds += int(match.group(2) or 0)*SECONDS_DAYS
|
||||||
number = number*SECONDS_HOURS
|
seconds += int(match.group(3) or 0)*SECONDS_HOURS
|
||||||
elif unit == "d":
|
seconds += int(match.group(4) or 0)*SECONDS_MINUTES
|
||||||
number = number*SECONDS_DAYS
|
seconds += int(match.group(5) or 0)
|
||||||
elif unit == "w":
|
|
||||||
number = number*SECONDS_WEEKS
|
|
||||||
seconds += number
|
|
||||||
if seconds > 0:
|
if seconds > 0:
|
||||||
return seconds
|
return seconds
|
||||||
return None
|
return None
|
||||||
|
|
Loading…
Reference in a new issue