-
-
Save edendekker/012587ffbd2e3a47f676ae9b07d2f721 to your computer and use it in GitHub Desktop.
Seconds to human readable text.
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
#!/usr/bin/env python | |
def sec_to_human(secs): | |
secs = end_time - start_time | |
units = dict({ | |
7*24*3600: "week", | |
24*3600: "day", | |
3600: "hour", | |
60: "minute", | |
1: "second" | |
}) | |
if secs <= 0: | |
return "0 seconds" | |
elif secs <= 1: | |
return str(secs)+" seconds" | |
s = list() | |
for divisor, name in sorted(units.items(), reverse=True): | |
quot = int(secs / divisor) | |
if quot: | |
if abs(quot) > 1: | |
s.append("%s %ss" % (quot, name)) | |
else: | |
s.append("%s %s" % (quot, name)) | |
secs -= quot * divisor | |
return " ".join(s) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment