Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing time from epoch time to iso format in Python [duplicate]

Tags:

python

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.

like image 361
Sam Gomari Avatar asked Dec 16 '14 14:12

Sam Gomari


1 Answers

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'
like image 151
unutbu Avatar answered Nov 17 '22 09:11

unutbu