Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert timestamp to rfc 3339 in Python [duplicate]

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?

like image 906
Vishal Puliani Avatar asked Jan 10 '16 09:01

Vishal Puliani


2 Answers

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

like image 68
Zohar81 Avatar answered Sep 30 '22 09:09

Zohar81


>>> print datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4]+"Z"
2016-01-10T10:27:45.89Z
like image 20
Rolf of Saxony Avatar answered Sep 30 '22 11:09

Rolf of Saxony