How to parse timezone with colon

Question:

Is there a way to parse timezone in “+00:00” format with datetime.strptime? For instance:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.strptime("12:34:56+0000", "%X%z")
datetime.datetime(1900, 1, 1, 12, 34, 56, tzinfo=datetime.timezone.utc)
>>> datetime.strptime("12:34:56+00:00", "%X%z")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:Python34lib_strptime.py", line 500, in _strptime_datetime
    tt, fraction = _strptime(data_string, format)
  File "C:Python34lib_strptime.py", line 337, in _strptime
    (data_string, format))
ValueError: time data '12:34:56+00:00' does not match format '%X%z'

Any ideas?

Asked By: dijxtra

||

Answers:

Currently, there is no cure for this, and here is and explanation: https://bugs.python.org/issue15873 more precisely, here: https://bugs.python.org/msg169952 .
But you can override this issue, this way:

from datetime import datetime
d = "2015-04-30T23:59:59+00:00"
if ":" == d[-3:-2]:
    d = d[:-3]+d[-2:]
print(datetime.strptime(d, "%Y-%m-%dT%H:%M:%S%z"))
Answered By: Srđan Popić

Another solution is to use the dateutil library:

from dateutil import parser

mystr = '2015-04-30T23:59:59+00:00'

x = parser.parse(mystr)

# 2015-04-30 23:59:59+00:00
Answered By: jpp

Adapted from a rejected edit:

Python >= 3.7:

from datetime import datetime
d = "2019-12-25T23:59:59+00:00"
print(datetime.strptime(d, "%Y-%m-%dT%H:%M:%S%z"))

Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, ‘+01:00:00’ will be parsed as an offset of one hour. In addition, providing ‘Z’ is identical to ‘+00:00’.

https://docs.python.org/3.7/library/datetime.html

Python <= 3.6

There is no built-in way, but the best solution is:

from datetime import datetime
d = "2019-12-25T23:59:59+00:00"
if ":" == d[-3]:
    d = d[:-3]+d[-2:]
print(datetime.strptime(d, "%Y-%m-%dT%H:%M:%S%z"))

Explanations:
https://bugs.python.org/issue15873
https://bugs.python.org/msg169952

Answered By: Brendano257

Old question, but I thought I’d add this.

From 3.7 onwards, you could use time.fromisoformat():

from datetime import time
iso_datetime_string = "2021-01-01T00:00:00+00:00"
print(time.fromisoformat(iso_datetime_string))

Off topic but might be useful to anyone else getting here: if you are trying to do the reverse than OP, there is also no native way to convert datetime object via .strftime() with a zulu offset of format "+00:00" (there is %z but that gives you "+0000"). The solution I found was datetime.isoformat(), and that one works also before 3.7

Answered By: MarcelD
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.