473,396 Members | 2,068 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Converting milliseconds to human time

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
6 26379
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
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
Thanks Dan, that would be perfect or close enough I should say. :-)

Regards,

Harlin Seritt

Jan 7 '06 #4

"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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Deepa | last post by:
I need a routine to convert milliseconds to a traditional dd:hh:mm:ss or four separate variables that I can display. I am not wanting to display a date but rather the interval between two...
0
by: pduncan | last post by:
Hi all, I need to update client machines with the correct time if it is inaccurate. The server I'm using will always have the correct time so I'll be using that as the source of correct time. ...
6
by: Jim Davis | last post by:
Before I reinvent the wheel I thought I'd ask: anybody got a code snippet that will convert the common ISO8601 date formats to a JS date? By "common" I mean at the least ones described in this...
7
by: Dana Shields | last post by:
I am attempting to upsize from access to SQL Server. I'm trying to convert my queries to SQL Server views; however, I'm having a lot of difficulty with the syntax differences. For instance, a...
26
by: Pravesh | last post by:
Hi: is there a way to get current system time in milliseconds... which functions and headers?? thanks pravesh
9
by: HL | last post by:
I am using VS 2005 Beta - C# Problem: The Timer fires a few milliseconds before the actual Due-Time Let's say a timer is created in the following manner: System.Threading.Timer m_timer = null;...
2
by: Harlin Seritt | last post by:
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...
3
by: NateM | last post by:
How do I convert any given date into a milliseconds value that represents the number of milliseconds that have passed since January 1, 1970 00:00:00.000 GMT? Is there an easy way to do this like...
1
by: Math | last post by:
Hello PythonPeople.. Can anybody help me out? How do I convert a time of day from milliseconds? For example: I got the following time in milliseconds: 1,090516451769E+15 And I want to print it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.