473,326 Members | 2,680 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,326 software developers and data experts.

Converting milliseconds to human amount of time

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

Jan 7 '06 #1
2 10798
Max
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
Jan 7 '06 #2
Max wrote:
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.


You'd probably be helped by divmod:
help(divmod) Help on built-in function divmod in module __builtin__:

divmod(...)
divmod(x, y) -> (div, mod)

Return the tuple ((x-x%y)/y, x%y). Invariant: div*y + mod == x.
def humanize(milli): sec, milli = divmod(milli, 1000)
min, sec = divmod(sec, 60)
hour, min = divmod(min, 60)
day, hour = divmod(hour, 24)
week, day = divmod(day, 7)
print week, "weeks,", day, "days,", hour, "hours,", \
min, "minutes,", sec, "seconds, and", \
milli, "milliseconds"
return (week, day, hour, min, sec, milli)
humanize(1234567890) 2 weeks, 0 days, 6 hours, 56 minutes, 7 seconds, and 890 milliseconds
(2, 0, 6, 56, 7, 890) humanize(694861001.1) #Also works with floats!

1.0 weeks, 1.0 days, 1.0 hours, 1.0 minutes, 1.0 seconds, and
1.10000002384 milliseconds
(1.0, 1.0, 1.0, 1.0, 1.0, 1.1000000238418579)
Jan 9 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stefan Behnel | last post by:
Hi! The logging module nicely prepends each line with a formatted date. However, I'm not interested in the actual date but only in the number of milliseconds that passed since the start of the...
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...
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;...
3
by: Parvesh | last post by:
hi, I am using a webservice which Returns the Result in an XML string, The XML response i get i svery cumbersome to parse, But if i could convert it to the Corresponding Class using the...
6
by: Harlin Seritt | last post by:
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...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.