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

John Machin sjmachin at lexicon.net
Fri Apr 28 06:27:47 EDT 2006


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]




More information about the Python-list mailing list