472,348 Members | 1,848 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 10160
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...
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 -...
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...
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...
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....
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...
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,...
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...
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...
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 ... ...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

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.