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

Faber zannablu at libero.it
Fri Apr 28 10:51:19 EDT 2006


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



More information about the Python-list mailing list