Last active
May 10, 2017 15:14
-
-
Save 7h3rAm/a53eac1eea4da887cab9 to your computer and use it in GitHub Desktop.
Seconds to human readable text. Inspired from https://gist.github.com/erickpatrick/3039081
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): | |
units = dict({ | |
7*24*3600: "week", | |
24*3600: "day", | |
3600: "hour", | |
60: "minute", | |
1: "second" | |
}) | |
if secs <= 0: return "0 seconds" | |
s = list() | |
for divisor, name in sorted(units.iteritems(), 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
Thanks for the share! I created a similar gist but it's not as good as yours. Thank you 👍