Formatting Time

Andrew Dalke dalke at dalkescientific.com
Thu Jun 2 17:03:49 EDT 2005


Ognjen Bezanov wrote:
> I have a float variable representing seconds, and i want to format it
> like this:
> 
> 0:00:00  (h:mm:ss)


>>> def format_secs(t):
...   m, s = divmod(t, 60)
...   h, m = divmod(m, 60)
...   return "%d:%02d:%02d" % (h, m, s)
... 
>>> format_secs(0)
'0:00:00'
>>> format_secs(1)
'0:00:01'
>>> format_secs(59)
'0:00:59'
>>> format_secs(60)
'0:01:00'
>>> format_secs(61)
'0:01:01'
>>> format_secs(3600)
'1:00:00'
>>> format_secs(3601)
'1:00:01'
>>> format_secs(3661)
'1:01:01'
>>> format_secs(3600*100+120+58)
'100:02:58'
>>> 

				Andrew
				dalke at dalkescientific.com




More information about the Python-list mailing list