I encountered a problem with converting the airflow macros reference 'ts' into a datetime object. The problem is with the tz at the end of the string.
from datetime import datetime
timetoday = kwargs['ts']
t = datetime.strptime(timetoday, '%Y-%m-%dT%H:%M:%S%z')
The code didn't execute successfully, instead it throws an error message:
time data '2019-08-24T00:00:00+00:00' does not match format '%Y-%m-%dT%H:%M:%S%z'
If I used the same code with slightly different format, and try with 'ds' macros, and I managed to convert the string into datetime object.
from datetime import datetime
timetoday = kwargs['ds']
t = datetime.strptime(timetoday, '%Y-%m-%d')
Update: Reading the notes from http://strftime.org/ and understand that %z
is UTC offset in the form +HHMM or -HHMM (empty string if the the object is naive). This led me wondering whether the additional ':'
in between +00:00
(returned string of kwargs['ts']
is the root cause of the error. However, I'm still unsure how to parse it correctly, as there were no other options in strptime
.
Therefore, I change my macro to kwargs['ts_nodash']
that doesn't return the timezone, and proceed with my code for now. If anyone knows how to do it, I'm still interested to learn how that can be done correctly! Thanks!
timetoday = kwargs['ts_nodash']
t = datetime.strptime(timetoday, '%Y%m%dT%H%M%S')