Last active
August 29, 2015 14:27
-
-
Save need12648430/034fb1edcc54e8aafd28 to your computer and use it in GitHub Desktop.
User friendly representation of times in 12 lines of Python.
This file contains 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
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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
awesome!