Created
May 6, 2013 03:23
-
-
Save ecaldwell/5523207 to your computer and use it in GitHub Desktop.
Convert time in seconds to a pretty string summary in the format 1 h 5 minutes 10.3 seconds.
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/env python | |
def formatTime(s): | |
'''Converts seconds to a pretty string like 1 h 5 minutes 10.3 seconds.''' | |
hours, remainder = divmod(s, 3600) | |
minutes, seconds = divmod(remainder, 60) | |
timestring = str(seconds) + ' sec' | |
if minutes > 0: | |
timestring = str(int(minutes)) + ' min ' + timestring | |
if hours > 0: | |
timestring = str(int(hours)) + ' h ' + timestring | |
return timestring #1 h 5 min 10.3 sec |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment