1

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

tb1212
  • 11
  • 1
  • Note: you used the tag: gps-time: note that GPS time count the exact number of seconds (from an "other epoch"), unix timestamp instead doesn't consider leap seconds. So you may edit your question about what of the two counts your are asking. – Giacomo Catenazzi Mar 12 '20 at 13:02

1 Answers1

0

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.

Pedro Lobito
  • 94,083
  • 31
  • 258
  • 268