Convert timestamp to rfc 3339 in Python

Vishal Puliani picture Vishal Puliani · Jan 10, 2016 · Viewed 7k times · Source

I am trying to convert the timestamp 1452454659 to its rfc3339 equivalent.

I am getting the output as:

2016-01-11T01:07:39+05:30

When I pass this to influxdb it returns a time of:

2016-01-10T19:37:39Z

while I actually want the time in influxdb to be:

2016-01-11T01:07:39

I have even tried to pass only 2016-01-11T01:07:39, leaving out +5:30, but then it gives me no result.

What mistake am I making?

Answer

Zohar81 picture Zohar81 · Jan 10, 2016

if your timestamp is from utc format, the following example may help you : (just replace the variable 'd' with your own timestamp)

import datetime
d = datetime.datetime.utcnow()
print d.isoformat("T") + "Z"


--> 2016-01-10T09:33:33.865129Z

I based my answer on the following link : https://docs.python.org/2/library/datetime.html