469,646 Members | 1,334 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

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 9404
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Able | last post: by
sunbin
4 posts views Thread by sunbin | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.