9

I would like to change the time from epoch time to a format readable by Kml extensions (such as iso format of time).

There is plenty of help to change from epoch to formats like YYYYMMDDHHMMSS and other structs using tuples and mktime, but to .iso formation , i haven't been able to find it.

Sam Gomari
  • 733
  • 4
  • 13
  • 37

1 Answers1

24

utcfromtimestamp converts seconds since the epoch to the corresponding UTC datetime.datetime.

datetime.datetime objects have a isoformat method which returns the date as a string in ISO 8601 format.

In [6]: import datetime as DT

In [7]: seconds_since_epoch = 0

In [8]: DT.datetime.utcfromtimestamp(seconds_since_epoch)
Out[8]: datetime.datetime(1970, 1, 1, 0, 0)

In [9]: DT.datetime.utcfromtimestamp(seconds_since_epoch).isoformat()
Out[9]: '1970-01-01T00:00:00'
unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677