0
2018-03-09T09:00:00.687294374+08

This is the date time data i've got from TRTH(Thomson Reuters Tick History) for HongKong. I want to convert it to epoch time but i am unable to do so because of the extra precision it has.I know only how to convert date time in seconds precision to epoch time which could be done by following code

import time
datetime = '15.07.2011 16:02:09'
pattern = '%d.%m.%Y %H:%M:%S'
epoch = int(time.mktime(time.strptime(datetime, pattern)))
print epoch

But i don't know how to convert the data i have. Please help.

EDIT 1:I got to know that hong kong time is UTC+8 so that +08 is there for it so i can remove that and then the string left to convert to epoch is

2018-03-09T09:00:00.687294374
abhihacker02
  • 51
  • 1
  • 9

1 Answers1

0

Datetime with nanoseconds is not possible, only microseconds. Perhaps you can try the following (with Python 3):

from datetime import datetime, timezone
s = '2018-03-09T09:00:00.687294374'  # your string
dt = datetime.strptime(s[:-3], '%Y-%m-%dT%H:%M:%S.%f')  # removing the nanoseconds
timestamp = dt.replace(tzinfo=timezone.utc).timestamp() # get POSIX timestamp 
print(timestamp)

1520586000.687294

Documentation: datetime.time & datetime.timestamp()

Please also check out:

1) Converting datetime.date to UTC timestamp in Python

2) Parsing datetime strings containing nanoseconds

And perhaps it might be possible with Python 3.7: PEP 564 -- Add new time functions with nanosecond resolution

PythonSherpa
  • 2,560
  • 3
  • 19
  • 40