Reputation: 1929
I'm having issues parsing "Mar 3 03:00:04 2019 GMT" with python's (datetime.datetime.strptime). I can't figure out what the issue is. At first I thought it was because the day was not zero padded, but according to the docs I found that is not the case.
import datetime
datetime.datetime.strptime("%b %d %H:%M:%S %Y %Z", "Mar 3 03:00:04 2019 GMT")
I've tried removing and adding whitespace to the format string. I've also tried going without the timezone specifier to no luck. The format I'm trying to parse comes from the ssl socket method getpeercert
.
ValueError Traceback (most recent call last)
<ipython-input-14-a9f4aa24cc39> in <module>
----> 1 datetime.datetime.strptime("%b %d %H:%M:%S %Y %Z", "Mar 3 03:00:04 2019 GMT")
/usr/lib/python3.7/_strptime.py in _strptime_datetime(cls, data_string, format)
575 """Return a class cls instance based on the input string and the
576 format string."""
--> 577 tt, fraction, gmtoff_fraction = _strptime(data_string, format)
578 tzname, gmtoff = tt[-2:]
579 args = tt[:6] + (fraction,)
/usr/lib/python3.7/_strptime.py in _strptime(data_string, format)
357 if not found:
358 raise ValueError("time data %r does not match format %r" %
--> 359 (data_string, format))
360 if len(data_string) != found.end():
361 raise ValueError("unconverted data remains: %s" %
ValueError: time data '%b %d %H:%M:%S %Y %Z' does not match format 'Mar 3 03:00:04 2019 GMT'
What is the correct format string I should be using?
Upvotes: 0
Views: 1829
Reputation: 127
datetime.datetime.strptime(date_string, format)
The exact syntax should be as above. Changing the order of arguments would work.
datetime.datetime.strptime("Mar 3 03:00:04 2019 GMT","%b %d %H:%M:%S %Y %Z")
datetime.datetime(2019, 3, 3, 3, 0, 4)
Upvotes: 1
Reputation: 1416
You need to flip the args, ie:
import datetime
datetime.datetime.strptime("Mar 3 03:00:04 2019 GMT", "%b %d %H:%M:%S %Y %Z")
Upvotes: 1