I have the following line, "2017-03-30 08:25:00CET" , which I want to convert to a dtetime tz-aware object.
According to this SO question , from python 3.2 this can be done using only the datetime module. Also, 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 will try the following
datetime.strptime(dep_dt, '%Y-%m-%d %H:%M:%S%Z')
I don't get any errors, 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 get tat-aware datetime:
datetime.datetime(2017, 3, 30, 8, 25, tzinfo=datetime.timezone(datetime.timedelta(0, 7200)))
Any ideas on why it works with %z but not with %z ? What am I doing wrong?