How to use calendar.timegm() with fractional seconds

456 views Asked by At

I have a timestamp that I am converting into seconds since the epoch. I have been using calendar.timegm(timestamp) but have decided that I also need the fractional seconds as well. I have looked through the python documentation and wasn't able to find anything. Is there a method that does the same conversion but doesn't ignore fractional seconds?

Here's an example of my timestamp: Jan 04 2011 00:00:20.498

Desired output: 978134435.something

1

There are 1 answers

0
Pedro Lobito On

You can use %f:

from datetime import datetime
d = "Jan 04 2011 00:00:20.498"
x = datetime.timestamp(datetime.strptime(d, "%b %d %Y %H:%M:%S.%f").replace(microsecond=int(d.split('.')[1])))
print(x)
#1294099220.000498

%f: Microsecond as a decimal number, zero-padded on the left.