Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Created March 4, 2020 19:30
Show Gist options
  • Save ahmed4end/36097512b0bfd8b129ec3df100e17d65 to your computer and use it in GitHub Desktop.
Save ahmed4end/36097512b0bfd8b129ec3df100e17d65 to your computer and use it in GitHub Desktop.
def format_duration(s):
dt = []
for b, w in [(60, 'second'), (60, 'minute'), (24, 'hour'), (365, 'day'), (s+1, 'year')]:
s, m = divmod(s, b)
if m: dt.append('%d %s%s' % (m, w, 's' * (m > 1)))
return ' and '.join(', '.join(dt[::-1]).rsplit(', ', 1)) or 'now'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment