Created
October 4, 2012 00:34
-
-
Save justinabrahms/3830804 to your computer and use it in GitHub Desktop.
pretty time formatting in 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 nice_time(delta): | |
""" | |
Gets time delta in seconds and returns a pretty string | |
representing it in the format of 1w2d9h16m44s | |
""" | |
weeks = 0 | |
days = 0 | |
hours = 0 | |
minutes = 0 | |
seconds = 0 | |
seconds_in_minute = 60 | |
seconds_in_hour = 60 * seconds_in_minute | |
seconds_in_day = 24 * seconds_in_hour | |
seconds_in_week = 7 * seconds_in_day | |
weeks = delta / seconds_in_week | |
if weeks != 0: | |
delta -= weeks * seconds_in_week | |
days = delta / seconds_in_day | |
if days != 0: | |
delta -= days * seconds_in_day | |
hours = delta / seconds_in_hour | |
if hours != 0: | |
delta -= hours * seconds_in_hour | |
minutes = delta / seconds_in_minute | |
if minutes != 0: | |
delta -= minutes * seconds_in_minute | |
seconds = delta | |
out = "" | |
if seconds: | |
out = "%ss" % seconds + out | |
if minutes: | |
out = "%sm" % minutes + out | |
if hours: | |
out = "%sh" % hours + out | |
if days: | |
out = "%sd" % days + out | |
if weeks: | |
out = "%sw" % weeks + out | |
if out == "": | |
return "just now" | |
return out |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment