Skip to content

Instantly share code, notes, and snippets.

@need12648430
Last active August 29, 2015 14:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save need12648430/034fb1edcc54e8aafd28 to your computer and use it in GitHub Desktop.
Save need12648430/034fb1edcc54e8aafd28 to your computer and use it in GitHub Desktop.
User friendly representation of times in 12 lines of Python.
def ago(elapsed):
o = []
for unit, size in [("yr",365*24*60*60),("mo",30*24*60*60),("wk",7*24*60*60),("d",24*60*60),("hr",60*60),("min",60),("sec",1)]:
if size > elapsed: continue
total = int(elapsed / size)
elapsed = elapsed % size
o.append(str(total) + unit)
return ", ".join(o[0:2]) + " ago"
# usage:
import time
ago(time.time() - post_timestamp) # returns, for example, "1yr, 2mo ago"
@rdelro
Copy link

rdelro commented Aug 19, 2015

awesome!

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