Skip to content

Instantly share code, notes, and snippets.

@vlad-bezden
Created February 21, 2018 15:05
Show Gist options
  • Select an option

  • Save vlad-bezden/0afe988fb5bd7f914bda5863b71897d6 to your computer and use it in GitHub Desktop.

Select an option

Save vlad-bezden/0afe988fb5bd7f914bda5863b71897d6 to your computer and use it in GitHub Desktop.
Calculates time from any time passed format H:M:S M:S S
from datetime import timedelta
from functools import reduce
from itertools import chain, repeat
from typing import List
def calc(times: List[str]) -> timedelta:
'''Calculates total time from the list of times'''
result = timedelta()
for t in times:
for hms in [t.split(':')]:
s, m, h, *_ = chain(hms[::-1], repeat('0', 3))
result += timedelta(hours=int(h), minutes=int(m), seconds=int(s))
return result
@vlad-bezden
Copy link
Copy Markdown
Author

Example of using it
times = [
'1:2:3',
'4:5',
'6'
]
total_time = calc(times) # 1:06:14

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment