2

I am getting timestamps from a REST server which creates them using datetime.str(). This gives me a timestamp that looks like: '2018-02-21 10:40:35+00:00'. However, datetime.strptime only supports %z which requires the trailing timezone offset to be in the form +/-nnnn with no colon. The exact error looks like:

In[16]: str(dt)
Out[16]: '2018-02-21 10:40:35+00:00'
In[17]: datetime.strptime(str(dt), "%Y-%m-%d %H:%M:%S%z")
ValueError: time data '2018-02-21 10:40:35+00:00' does not match format '%Y-%m-%d %H:%M:%S%z'

Is there a strptime flag that will parse the trailing "+00:00" as-is, or do I need to resort to str.rpartition tricks before calling strptime?

PaulMcG
  • 62,419
  • 16
  • 94
  • 130
  • `strptime` documentation (https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior) mentions only one valid standard for the TZ offset: without the colon. – DYZ Feb 21 '18 at 17:35
  • 1
    It is annoying that the default `str()` for datetime emits the timezone with the colon, which has no `strptime`-parseable analog. – PaulMcG Feb 21 '18 at 17:38

0 Answers0