Skip to content

Instantly share code, notes, and snippets.

@BenTheHokie
Created July 8, 2012 03:57
Show Gist options
  • Save BenTheHokie/3069238 to your computer and use it in GitHub Desktop.
Save BenTheHokie/3069238 to your computer and use it in GitHub Desktop.
A hour minute second converter for seconds to just about whatever you want
def secsToHMS(totsecs):
def plVal(num):
if abs(num)==1:
return ''
else:
return 's'
secs = totsecs%60
totmins = (totsecs - secs)/60
hrs = (totmins - totmins%60)/60
mins = totmins - hrs*60
strhrs = str(hrs)
strmins = '0'*(mins<10)+str(mins)
strsecs = '0'*(secs<10)+str(secs)
return {'hm':'%s:%s' % (strhrs,strmins),'hms':'%s:%s:%s' % (strhrs,strmins,strsecs),'hours':hrs,'totalhours':hrs,'totalmins':totmins,'mins':mins,'shorthms':'%s hr%s %s min%s %s sec%s' % (strhrs,plVal(hrs),strmins,plVal(mins),strsecs,plVal(secs)),'longhms':'%s hour%s %s minute%s %s sec%s' % (strhrs,plVal(hrs),strmins,plVal(mins),strsecs,plVal(secs)),'shorthm':'%s hr%s %s min%s' % (strhrs,plVal(hrs),strmins,plVal(mins)),'longhm':'%s hour%s %s minute%s' % (strhrs,plVal(hrs),strmins,plVal(mins))}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment