initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import sys
|
||||
import re
|
||||
import math
|
||||
import datetime
|
||||
|
||||
|
||||
def parse_duration(text: str) -> datetime.timedelta:
|
||||
re_duration = re.compile(r'(\d+(?:\.\d+)?(?:sec|min|hour|hr|h|m|s))')
|
||||
re_digits = re.compile(r'([\d.]+)')
|
||||
time_scales = {
|
||||
's': 1,
|
||||
'sec': 1,
|
||||
'm': 60,
|
||||
'min': 60,
|
||||
'h': 3600,
|
||||
'hr': 3600,
|
||||
}
|
||||
|
||||
parts = re_duration.findall(text)
|
||||
if parts:
|
||||
total_secs = 0
|
||||
for it in parts:
|
||||
if m := re_digits.search(it):
|
||||
_, end = m.span()
|
||||
scale = it[end:]
|
||||
total_secs += math.ceil(float(m.group(1)) * time_scales[scale])
|
||||
return datetime.timedelta(seconds=total_secs)
|
||||
|
||||
parts = [float(it) for it in text.split(':')]
|
||||
if len(parts) == 1:
|
||||
return datetime.timedelta(seconds=parts[0])
|
||||
if len(parts) == 2:
|
||||
return datetime.timedelta(minutes=parts[0], seconds=parts[1])
|
||||
if len(parts) == 3:
|
||||
return datetime.timedelta(hours=parts[0], minutes=parts[1], seconds=parts[2])
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
parsed = parse_duration(sys.argv[1])
|
||||
seconds = int(parsed.total_seconds())
|
||||
Reference in New Issue
Block a user