Last active
December 20, 2015 15:49
-
-
Save ktravis/6157119 to your computer and use it in GitHub Desktop.
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
#!/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