473,473 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

time conversions [hh:mm:ss.ms <-> sec(.ms)

Hi,

I was looking at python & datetime and hoping that it would already
have
a method/func to translate time formats. I need to translate seconds to
hh:mm:ss.ms and vice versa and would like the ability to do some basic
arithmetic in these formats. I think that there just has to be a
package
or module out there that already does this with reasonable speed and
accuracy.

Sadly, i do indeed need both reasonable speed and accuracy since i will
be adding up huge masses of small events (around 10 to 60 ms
milliseconds
in length) to create cloud like textures for real time audio/video at
high sampling rates.

i googled for hh:mm:ss.ms + python and didn't find much...

best,

-kp---

[mac os x w/ python 2.4.1]

Apr 28 '06 #1
5 10519
On 28/04/2006 6:39 PM, kpp9c wrote:
Hi,

I was looking at python & datetime and hoping that it would already
have
a method/func to translate time formats. I need to translate seconds to
hh:mm:ss.ms and vice versa and would like the ability to do some basic
arithmetic in these formats.
Could you please be a little more specific? I guess that you have
seconds-and-a-fraction in a float eg 3723.456 seconds, which is
equivalent to 1 hour, 2 minutes and 3.456 seconds. How do you want that
represented? You say hh:mm:ss.ms which could be interpreted as a string
"01:02:03.456" -- but this format is not very useful for "basic
arithmetic". OTOH a tuple representation like (1, 2, 3.456) can
accommodate "arithmetic" of some sort or other more easily -- is that
what you had in mind? Next question: exactly what basic arithmetic
operations do you want to do in the hour-minute-second format, and why
do you want to do them in that format, and not the seconds-only format?
I think that there just has to be a
package
or module out there that already does this with reasonable speed and
accuracy.
Get the specification right first. Get the accuracy right second. Then
worry about the speed. The "arithmetic" of which you speak can't be so
mind-boggling that you can't write it in Python and test it yourself.
You may find the specification changes under the influence of the
implementation :-)

Sadly, i do indeed need both reasonable speed and accuracy since i will
be adding up huge masses of small events (around 10 to 60 ms
milliseconds
in length) to create cloud like textures for real time audio/video at
high sampling rates.
So why not keep it in seconds (or milliseconds)?

Have you considered an extension, using C or Pyrex?

i googled for hh:mm:ss.ms + python and didn't find much...

best,

-kp---

[mac os x w/ python 2.4.1]


Apr 28 '06 #2
kpp9c wrote:
Hi,

I was looking at python & datetime and hoping that it would already
have
a method/func to translate time formats. I need to translate seconds
to hh:mm:ss.ms and vice versa and would like the ability to do some
basic arithmetic in these formats. I think that there just has to be a
package
or module out there that already does this with reasonable speed and
accuracy.

Sadly, i do indeed need both reasonable speed and accuracy since i
will be adding up huge masses of small events (around 10 to 60 ms
milliseconds
in length) to create cloud like textures for real time audio/video at
high sampling rates.

i googled for hh:mm:ss.ms + python and didn't find much...

best,

-kp---

[mac os x w/ python 2.4.1]


Hmmm ... not difficult to do, but it's the "speed" bit that's tricky.
Here's quick and dirty implementation, but I wouldn't place any bets on
it being fast (or accurate enough for that matter!)

#!/bin/env python
# vim: set noet sw=4 ts=4:

import datetime

def secs2time(s):
ms = int((s - int(s)) * 1000000)
s = int(s)
# Get rid of this line if s will never exceed 86400
while s >= 24*60*60: s -= 24*60*60
h = s / (60*60)
s -= h*60*60
m = s / 60
s -= m*60
return datetime.time(h, m, s, ms)

def time2secs(d):
return d.hour*60*60 + d.minute*60 + d.second + \
(float(d.microsecond) / 1000000)

if __name__ == "__main__":
for i in (80000.123, 0.0, 5873, 12345.6789):
print "%12.6f -> %15s -> %12.6f" % (
i,
secs2time(i),
time2secs(secs2time(i))
)
assert i == time2secs(secs2time(i))
from timeit import Timer
timer = Timer(
setup="from __main__ import time2secs, secs2time",
stmt="time2secs(secs2time(12345.6789))")
count = 1000000
total = timer.timeit(count)
print "Time for %d invocations: %.10fs" % (count, total)
print "Time per invocation: %.10fs" % (total / count,)
And the output:

80000.123000 -> 22:13:20.123000 -> 80000.123000
0.000000 -> 00:00:00 -> 0.000000
5873.000000 -> 01:37:53 -> 5873.000000
12345.678900 -> 03:25:45.678900 -> 12345.678900
Time for 1000000 invocations: 9.3959178925s
Time per invocation: 0.0000093959s
Huh ... what d'ya know ... good thing I ain't a betting man!
Dave.

--

Apr 28 '06 #3
kpp9c wrote:
I was looking at python & datetime and hoping that it would already
have a method/func to translate time formats. I need to translate seconds
to hh:mm:ss.ms and vice versa and would like the ability to do some basic
arithmetic in these formats.
Have a look at datetime.timedelta:

from datetime import timedelta

seconds_value = 4237.63
td = timedelta(seconds=seconds_value)

print td # Shows 1:10:37.630000
print td.seconds # Shows 4237

other_td = td + timedelta(seconds=13)

print other_td # Shows 1:10:50.630000
print other_td.seconds # Shows 4250
I think that there just has to be a package or module out there that
already does this with reasonable speed and accuracy.


The accuracy seems perfect, don't know about speed - take some test :)

Regards

--
Faber
http://faberbox.com/
http://smarking.com/

We live in a society exquisitely dependent on science and technology,
in which hardly anyone knows anything about science and technology. -- Carl
Sagan
Apr 28 '06 #4
timedelta looks to be just the ticket! bravo, thank you... i guess this
must be pretty new to Python. Nice... more "batteries included"
stuff...

cheers,

-kevin--

Apr 29 '06 #5
kpp9c wrote:
timedelta looks to be just the ticket! bravo, thank you... i guess this
must be pretty new to Python.


Well, since Python 2.3 (July 2003):
http://www.python.org/doc/2.3.5/what...00000000000000

:-)

--
Faber
http://faberbox.com/
http://smarking.com/

Only wimps use tape backup: _real_ men just upload their important stuff on
ftp and let the rest of the world mirror it *-- Linus Torvalds
Apr 30 '06 #6

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

Similar topics

6
by: Able | last post by:
Dear friends I need to format seconds as hh:mm:ss. I see a lot of coding transforming seconds to hh:mm:ss. Somebody know a short way? Regards Able
4
by: Rich | last post by:
Now.ToShortTimeString returns 9:13 PM. I would like to get this in miliatry time with seconds included 21:13:45 (or something like that - just military time with seconds). How can this be...
14
by: Michael Barrido | last post by:
I have this for example: Dim iSeconds as int32 = 3600 '3600 seconds is one hour How do i convert it to "01:00:00" ? Please help. Thanks in advance!
1
by: Jason Chan | last post by:
DateTime mydate = new DateTime(2006,1,1,0,0,0); string testStr = mydate.ToString("hh:mm:ss"); //return 12:00:00 mydate = new DateTime(2006,1,1,1,0,0) testStr = mydate.ToString("hh:mm:ss");...
5
nirmalsingh
by: nirmalsingh | last post by:
i am getting date format in string as 30-11-2006 05:59:44 PM . i want to convert in the format of yyyy-mm-dd hh:mm:ss to sore it in mysql database. Help me with sample coding plz..
4
sunbin
by: sunbin | last post by:
hi, How can i get the FLV file or any type of video file's duration in HH:MM:SS or HH:MM:SS:FF format from milliosecond ? I already got duration in millisecond. PHP script needed ... Plz help...
1
by: unknown66 | last post by:
hello, I have a time value in unix time that I would like to convert into a time format of hh:mm:ss:ms. For example, I have this number, 1172234138451, that I would like to be in the hh:mm:ss:ms...
1
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i...
1
seshu
by: seshu | last post by:
Hi every body to morning my cousine has show his application and also his live db in that to sav the length of all the voice files he has saved in time format ie he took the datatype of time now i...
5
by: lofty | last post by:
Hi, I'm building a time report system for work and I want to show the total hours and minutes a project have taken but can't get it to work ... I have a database (MySQL) in one table I have...
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.