Refactoring utils with adding several new ones

This commit is contained in:
Aleksei Chetaev 2023-02-20 00:41:16 +01:00 committed by Aleksey Chetaev
parent 5568cbd0bf
commit 4fd9d69701
10 changed files with 276 additions and 61 deletions

View file

@ -0,0 +1,27 @@
# There is place for date time utils functions
def parse_time(value: str) -> int:
"""Converts time interval in text form into time interval as number of seconds.
Args:
value: time interval as text.
Returns:
Number of seconds in the parsed time interval.
"""
value = value.lower()
for suffix in ["s", "sec"]:
if value.endswith(suffix):
return int(value[: -len(suffix)])
for suffix in ["m", "min"]:
if value.endswith(suffix):
return int(value[: -len(suffix)]) * 60
for suffix in ["h", "hr", "hour"]:
if value.endswith(suffix):
return int(value[: -len(suffix)]) * 60 * 60
raise ValueError(f"Unknown units in time value '{value}'")