Converting milliseconds to human amount of time

Max rabkin at mweb[DOT]co[DOT]za
Sat Jan 7 08:27:03 EST 2006


Harlin Seritt wrote:
> How can I take a time given in milliseconds (I am doing this for an
> uptime script) and convert it to human-friendly time i.e. "4 days, 2
> hours, 25 minutes, 10 seonds."? Is there a function from the time
> module that can do this?
> 
> Thanks,
> 
> Harlin Seritt
> 

seconds = millis / 1000 # obviously

minutes = seconds / 60
seconds %= 60

hours = minutes / 60
minutes %= 60

days = hours / 24
hours %= 24

All this using integer division, of course. This is probably much more 
verbose than the tersest soln, but it works (or should do - I haven't 
tested it). It's not strictly accurate (from a scientific/UTC 
perspective, as some minutes have 59 or 61 seconds rather than 60, but 
it's probably the best you need.

--Max



More information about the Python-list mailing list