Last active
December 2, 2021 20:25
-
-
Save angrychimp/dc2762c92f1c70ef90d684ab80b27bf5 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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