Parse UTC offset from string to datetime in Google App Engine Python runtime
-
07-07-2021 - |
Question
I want to parse a POST parameter like 2012-10-12T16:55+0300 to a datetime
type in order to store it in ndb.DateTimeProperty()
of my model.
Thus, what I have is:
from datetime import datetime
.....
my_date = datetime.strptime(self.request.get('my_date'), '%Y-%m-%dT%H:%M%z')
This call generates the following error:
ERROR 2012-10-12 14:03:08,376 webapp2.py:1553] 'z' is a bad directive in format '%Y-%m-%dT%H:%M%z'
Any ideas on how to solve this problem? I have read various posts but none of them helped me on this.
Thanks a lot in advance!
Solution
It seems someone filed a bug for the issue you see, which was rejected (see here). In short, %z is not always supported. Quoting from the bug report:
"Only the directives specified in the documentation [of time.strftime()] are supported. Because strftime() is implemented per platform it can sometimes offer more directives than those listed. But strptime() is independent of any platform and thus does not necessarily support all directives available that are not documented as supported."