I have the following string "2017-03-30 08:25:00CET"
which I want to convert to a datetime tz-aware object.
According this SO question, from python 3.2 it can be done using only datetime
module. In addition, from the documentation, I see
%z | UTC offset in the form +HHMM or -HHMM (empty string if the object is naive). | (empty), +0000, -0400, +1030 %Z | Time zone name (empty string if the object is naive). | (empty), UTC, EST, CST
So I try the following
datetime.strptime(dep_dt, '%Y-%m-%d %H:%M:%S%Z')
I do not get any error, but then the object I get is not tz-aware
datetime.datetime(2017, 3, 30, 8, 25)
On the other hand, if I convert my string to "2017-03-30 08:25:00+0200"
and then convert it to an object with
datetime.strptime(dep_dt, '%Y-%m-%d %H:%M:%S%z')
I do get a tz-aware datetime:
datetime.datetime(2017, 3, 30, 8, 25, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))
Any ideas of why it works with %z
but not with %Z
? What am I doing wrong?