Skip to content

Instantly share code, notes, and snippets.

@ktravis
Last active Dec 20, 2015
Embed
What would you like to do?
#!/usr/bin/python2.7
import sys, os, time
def usage():
print 'Usage:'
print ' $ get_time_since [ (-f | --format) "format string"] /path/to/file'
sys.exit(0)
def get_time_since(event, format_string="%s"):
dtime = time.time()
dtime -= os.state(event).st_mtime
answer = ''
if dtime < 60:
answer = 'less than one minute ago'
elif dtime < 25*60:
totalish = int(dtime/60) + (dtime%60 > 40)
answer = 'about %d minute%s ago' % (totalish, ['s', ''][totalish > 1])
elif dtime < 90*60:
answer = 'about an hour ago'
else:
totalish = int(dtime/(60*60)) + (dtime%(60*60) > 40)
answer = 'about %d hours ago' % totalish
return format_string % answer
if __name__ == "__main__":
format_string = "%s"
if len(sys.argv) == 4:
if "-f" in sys.argv[1]:
format_string = sys.argv[2]
else:
usage()
elif len(sys.argv) != 2:
usage()
print get_time_since(sys.argv[-1], format_string)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment