Convert timestamp to rfc 3339 in Python [duplicate]
Asked Answered
S

3

6

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?

Swansdown answered 10/1, 2016 at 9:27 Comment(3)
I do not want to use now() as my timestamp is already there as a string, and that is why i do not want to use "now()"Swansdown
the added part: "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 from influxdb while i actually want the time in influxdb to be 2016-01-11T01:07:39." is unrelated to your question "convert timestamp to rfc 3339". Ask a new question about your issues with influxdb specifically.Cleanly
All timestamps in InfluxDB are UTC. There are no time zones nor time zone support.Rhinarium
Q
5
>>> print datetime.datetime.utcnow().strftime("%Y-%m-%dT%H:%M:%S.%f")[:-4]+"Z"
2016-01-10T10:27:45.89Z
Quadruped answered 10/1, 2016 at 10:28 Comment(0)
F
3

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

Fug answered 10/1, 2016 at 9:36 Comment(0)
P
3

There is a specialized python package for this:

https://pypi.python.org/pypi/strict-rfc3339

Puncture answered 10/1, 2016 at 9:40 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.