Skip to content

Instantly share code, notes, and snippets.

@ecaldwell
Created May 6, 2013 03:23
Show Gist options
  • Save ecaldwell/5523207 to your computer and use it in GitHub Desktop.
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.
#!/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