Skip to content

Instantly share code, notes, and snippets.

@angrychimp
Last active December 2, 2021 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save angrychimp/dc2762c92f1c70ef90d684ab80b27bf5 to your computer and use it in GitHub Desktop.
Save angrychimp/dc2762c92f1c70ef90d684ab80b27bf5 to your computer and use it in GitHub Desktop.
def duration(sec):
is_float = isinstance(sec, float)
parts = [
('days', int(sec / 86400)),
('hours', int((sec % 86400) / 3600)),
('minutes', int((sec % 3600) / 60)),
('seconds', float(sec % 60) if is_float else int(sec % 60))
]
for i in range(len(parts)):
if float(parts[i][1]) > 0:
break
return ":".join([*list(map(lambda x: f"{x[1]:02}{x[0][0]}", parts[i:-1])), f"{parts[-1][1]:06.3f}{parts[-1][0][0]}" if is_float else f"{parts[-1][1]:02}{parts[-1][0][0]}"]).lstrip('0')
print(duration(1))
print(duration(120.123532))
print(duration(1445))
print(duration(101445))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment