By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,490 Members | 3,266 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,490 IT Pros & Developers. It's quick & easy.

Converting milliseconds to human time

P: n/a
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

Jan 6 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Harlin Seritt wrote:
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:
d = datetime.timedelta(milliseconds=418235000)
print d

4 days, 20:10:35

Jan 6 '06 #2

P: n/a
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
print '%d days %d hours %d minutes %d seconds' % prntime(1000000) 0 days 0 hours 16 minutes 40 seconds print '%d days %d hours %d minutes %d seconds' % prntime(10000000) 0 days 2 hours 46 minutes 40 seconds print '%d days %d hours %d minutes %d seconds' % prntime(100000000) 1 days 3 hours 46 minutes 40 seconds print '%d days %d hours %d minutes %d seconds' % prntime(1000000000) 11 days 13 hours 46 minutes 40 seconds print '%d days %d hours %d minutes %d seconds' % prntime(418235000) 4 days 20 hours 10 minutes 35 seconds


max

Jan 6 '06 #3

P: n/a
Thanks Dan, that would be perfect or close enough I should say. :-)

Regards,

Harlin Seritt

Jan 7 '06 #4

P: n/a

"Max Erickson" <ma*********@gmail.com> wrote in message
news:ma**************************************@pyth on.org...
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

Jan 7 '06 #5

P: n/a
"Harlin Seritt" <ha**********@yahoo.com> writes:
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))
Jan 7 '06 #6

P: n/a
Paul Rubin <http://ph****@NOSPAM.invalid> writes:
def dhms(m,t):
if not t: return (m,)
return rl(m//t[0], t[1:]) + (m % t[0],)
Editing error, sigh. Meant of course to say
return dhms(m//t[0], t[1:]) + (m % t[0],)

Jan 7 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.