I wish to output:
"14:48:06.743174"
This is the closest I can get:
`"14:48:06"`
with:
t = time.time()
time.strftime("%H:%M:%S",time.gmtime(t))
According to the manual on time
there is no straight forward way to print microseconds (or seconds as float):
>>> time.strftime("%H:%M:%S.%f",time.gmtime(t))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Invalid format string
However datetime.datetime
format provides %f
which is defined as:
Microsecond as a decimal number [0,999999], zero-padded on the left
>>> import datetime
>>> datetime.datetime.now().strftime('%H:%M:%S.%f')
'14:07:11.286000'
Or when you have your value stored in t = time.time()
, you can use datetime.datetime.utcfromtimestam()
:
>>> datetime.datetime.utcfromtimestamp(t).strftime('%H:%M:%S.%f')
'12:08:32.463000'
I'm afraid that if you want to have more control over how microseconds get formatted (for example displaying only 3 places instead of 6) you will either have to crop the text (using [:-3]
):
>>> datetime.datetime.utcfromtimestamp(t).strftime('%H:%M:%S.%f')[:-3]
'12:08:32.463'
Or format it by hand:
>>> '.{:03}'.format(int(dt.microsecond/1000))
'.463'