python: how to handle timestamps (ISO8601)

Viewed 4783

I have to deal in python with strings representing iso8601 timestamps.

My timestamps string are therefore in the following form:

timestamp = "2011-08-18T10:29:47+03:00"

Currently I'm converting them in python using:

timestamp = timestamp[:-6]
timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S")

But in this way I lose all the information about the time zone. I have seen many examples here on s-o about timestamps and python, unfortunately no one was preserving the timezone as well, or just recover the time zone delay using:

delay = timestamp[-6:]

I have also tried:

timestamp = "2011-08-18T10:29:47+03:00"
timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%dT%H:%M:%S%z")

but it returned

ValueError: 'z' is a bad directive in format '%Y-%m-%dT%H:%M:%S%z'

Can you give some insight?

2 Answers
Related