Converting milliseconds to human time

H

Harlin Seritt

I would like to take milliseconds and convert it to a more
human-readable format like:

4 days 20 hours 10 minutes 35 seconds

Is there something in the time module that can do this? I havent been
able to find anything that would do it.

Thanks,

Harlin Seritt
 
D

Dan Bishop

Harlin said:
I would like to take milliseconds and convert it to a more
human-readable format like:

4 days 20 hours 10 minutes 35 seconds

Is there something in the time module that can do this? I havent been
able to find anything that would do it.

The datetime module has something like that:
4 days, 20:10:35
 
M

Max Erickson

the hard way(in that you have to do it yourself):

def prntime(ms):
s=ms/1000
m,s=divmod(s,60)
h,m=divmod(m,60)
d,h=divmod(h,24)
return d,h,m,s

max
 
R

rurpy

Max Erickson said:
the hard way(in that you have to do it yourself):

def prntime(ms):
s=ms/1000
m,s=divmod(s,60)
h,m=divmod(m,60)
d,h=divmod(h,24)
return d,h,m,s

Or abstracted...

def decd (n, base):
"""
Decompose numeric value 'n' into components k[0:n]
such that n = sum (k[N-i]*base[M-i]) for i=0...N
where N is the length of k and M is the length of
base.

Examples:

To convert 310255 seconds to [days, hours, minutes, seconds]:

decd (310255, [3600*24, 3600, 60, 1])
[3, 14, 10, 55]

To convert 86.182 hours to [days, hours, minutes, seconds]:

decd (86.182, [24, 1, 1./60, 1./3600])
[3.0, 14.0, 10.0, 55.0]

To convert 78 (decimal) to binary:

decd (78, [128, 64, 32, 16, 8, 4, 2, 1])
[0, 1, 0, 0, 1, 1, 1, 0]

To break a decimal number into digits:
decd (463, [1000, 100, 10, 1])
[0, 4, 6, 3]

"""
r = []
for b in base:
d, n = divmod (n, b)
r.append (d)
return r
 
H

Harlin Seritt

Thanks Dan, that would be perfect or close enough I should say. :)

Regards,

Harlin Seritt
 
P

Paul Rubin

Harlin Seritt said:
I would like to take milliseconds and convert it to a more
human-readable format like:

4 days 20 hours 10 minutes 35 seconds

# To iterate is human; to recurse, divine.
def dhms(m,t):
if not t: return (m,)
return rl(m//t[0], t[1:]) + (m % t[0],)

human_readable = '%d days %d hours %d minutes %d seconds'% \
dhms(msec//1000, (60, 60, 24))
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top