Converting Time question

Nick Craig-Wood nick at craig-wood.com
Tue Mar 28 09:30:07 EST 2006


Math <mathweijzen at home.nl> wrote:
>  I measure a time at racing events.this tracktime is measures in the format 
>  hh:mm:ssDDD
>  where DDD = thousands of a second...like 17:14:11.769
>  This format is being saved as a number of micro seconds since 1970..
>  like 1,090516451769E+15
>  How do I convert from the micros seconds back to time format above?

This is probably what you want

>>> import datetime 
>>> t=datetime.datetime.fromtimestamp(1.090516451769E+15/1E6)
>>> t 
datetime.datetime(2004, 7, 22, 18, 14, 11, 769000)
>>> t.isoformat()
'2004-07-22T18:14:11.769000'
>>> t.isoformat(' ').split()[1]
'18:14:11.769000'

Note that this converts using the PCs time zone (hence it says 18:14
not 17:14).  You can pass your own timezone in (but it isn't easy!),
or you can use utcfromtimestamp()

>>> datetime.datetime.utcfromtimestamp(1.090516451769E+15/1E6).isoformat()[-15:]
'17:14:11.769000'

-- 
Nick Craig-Wood <nick at craig-wood.com> -- http://www.craig-wood.com/nick



More information about the Python-list mailing list