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?

jfs
  • 399,953
  • 195
  • 994
  • 1,670
Bazaka Bazaka
  • 135
  • 11
  • there is no correct decision by this link. I'm using Python 3.4 – Bazaka Bazaka Feb 01 '16 at 17:59
  • related: [Convert timestamps with offset to datetime obj using strptime](http://stackoverflow.com/q/12281975/4279) – jfs Feb 01 '16 at 20:07
  • related Python issues: [No way to generate or parse timezone as produced by datetime.isoformat()](https://bugs.python.org/issue24954) and [datetime: add ability to parse RFC 3339 dates and time](https://bugs.python.org/issue15873) – jfs Feb 01 '16 at 20:12

3 Answers3

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)))
Håken Lid
  • 22,318
  • 9
  • 52
  • 67
  • 1
    Don't blame me, blame the python or gnu guys who implemented `strptime`. https://docs.python.org/2/library/datetime.html#strftime-and-strptime-behavior And who mentioned the iso8601 standard, anyway? – Håken Lid Feb 01 '16 at 18:10
  • 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 – Padraic Cunningham Feb 01 '16 at 18:11
  • 1
    This question is about `strptime`. You're the only one talking about the iso standard. – Håken Lid Feb 01 '16 at 18:12
  • 1
    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`. – Håken Lid Feb 01 '16 at 18:22
  • Both `+HH`, `+HHMM` and `+HH:MM` are valid in iso8601, by the way. `strptime` only accepts one of them, though. – Håken Lid Feb 01 '16 at 18:32
  • I did not say they weren't, my original comment related to saying that offsets could not contain `:`. – Padraic Cunningham Feb 01 '16 at 18:37
  • 1
    Thank you for the correction. I've edited my answer to clarify that this is a `strptime` thing. – Håken Lid Feb 01 '16 at 18:43
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)
Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
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>)
ben432rew
  • 1,872
  • 16
  • 30
  • . 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. – Håken Lid Feb 01 '16 at 18:41
  • Edited to reflect the distinction – ben432rew Feb 01 '16 at 18:45