2

I have the string:

import datetime
time = "2016-02-01 19:14:54+02:00"

And trying to convert it to datetime obj:

result = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S%z")

It throws exception:

ValueError: time data '2016-02-01 19:14:54+02:00' does not match format '%Y-%m-%d %H:%M:%S%z'

Could you please help me what is wrong here?

3

3 Answers 3

5

Timezone offset %z should not have : between hours and minutes according to the python strptime specification.

>>>> datetime.datetime.strptime("2016-02-01 19:14:54+02:00", "%Y-%m-%d %H:%M:%S%z")
ValueError: time data '2016-02-01 19:14:54+02:00' does not match format '%Y-%m-%d %H:%M:%S%z'

>>> datetime.datetime.strptime("2016-02-01 19:14:54+0200", "%Y-%m-%d %H:%M:%S%z")
datetime.datetime(2016, 2, 1, 19, 14, 54, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))
Sign up to request clarification or add additional context in comments.

7 Comments

Don't blame me, blame the python or gnu guys who implemented strptime. docs.python.org/2/library/… And who mentioned the iso8601 standard, anyway?
You have Timezone offset should not have : between hours and minutes. which is incorrect, it might not be handled by strptime but it is perfectly valid
This question is about strptime. You're the only one talking about the iso standard.
My answer is to the question "what is wrong here?". But I agree with your point. dateutil is a better solution to the problem than fixing the string and then using strptime.
Thank you for the correction. I've edited my answer to clarify that this is a strptime thing.
|
2

Your date looks like it is in iso8601 format, you can use dateutil:

time = "2016-02-01 19:14:54+02:00"
from dateutil import parser

dte = parser.parse(time)

Output:

In [7]: from dateutil import parser
In [8]: time = "2016-02-01 19:14:54+02:00"
In [9]: dte = parser.parse(time)   
In [10]: dte
Out[10]: datetime.datetime(2016, 2, 1, 19, 14, 54, tzinfo=tzoffset(None, 7200))

In [11]: dte.utcoffset()
Out[11]: datetime.timedelta(0, 7200)

Comments

2

strptime will not parse iso8601 formatted datetime strings which have a : in the timezone (thanks @håken-lid), which is what your "2016-02-01 19:14:54+02:00" is an example of.

You can use either:

dateutil library like @padraic-cunningham suggests:

from dateutil import parser
time = "2016-02-01 19:14:54+02:00"
dte = parser.parse(time)   
dte
datetime.datetime(2016, 2, 1, 19, 14, 54, tzinfo=tzoffset(None, 7200))

dte.utcoffset()
datetime.timedelta(0, 7200)

or xml.utils

import xml.utils.iso8601
xml.utils.iso8601.parse("2004-04-09T21:39:00-08:00")

or iso8601

import iso8601
iso8601.parse_date("2007-01-25T12:00:00Z")
datetime.datetime(2007, 1, 25, 12, 0, tzinfo=<iso8601.Utc>)

2 Comments

. Both +HHMM and +HH:MM are valid in the iso standard, so it's not quite true that strptime will not parse the iso dateformat. It was @Padriac who suggested dateutil. But I fully agree that it is better to use a third party library over strptime to parse iso8601. There are several problems with python's strptime. For instance, it can't parse ISO week dates. And the features are not guaranteed to be the same across all operating systems, since python uses the whatever version of the C strptime function that's available.
Edited to reflect the distinction

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.