Skip to content

Instantly share code, notes, and snippets.

@7h3rAm
Last active May 10, 2017 15:14
Show Gist options
  • Save 7h3rAm/a53eac1eea4da887cab9 to your computer and use it in GitHub Desktop.
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
#!/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)
@edendekker
Copy link

edendekker commented May 10, 2017

Thanks for the share! I created a similar gist but it's not as good as yours. Thank you 👍

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