Why %z Is Not Supported By Python's Strptime?
>>> datetime.strptime('2014-02-13 11:55:00 -0800', '%Y-%m-%d %H:%M:%S %z') Traceback (most recent call last): File '', line 1, in File '/Sy
Solution 1:
Until Python 3.2, Python's datetime
module had no timezone()
object. It supported 3rd-party libraries providing timezones by providing a datetime.tzinfo()
abstract base class, but no timezone object was included. Without a timezone object, no support for parsing timezone offsets either.
As of Python 3.2, z
is supported, because that version (and up) added a datetime.timezone()
type:
>>>import datetime>>>datetime.datetime.strptime('2014-02-13 11:55:00 -0800', '%Y-%m-%d %H:%M:%S %z')
datetime.datetime(2014, 2, 13, 11, 55, tzinfo=datetime.timezone(datetime.timedelta(-1, 57600)))
>>>_.tzinfo
datetime.timezone(datetime.timedelta(-1, 57600))
Solution 2:
Here is a fix for python 2.7
Instead of using:
datetime.strptime(t,'%Y-%m-%dT%H:%M %z')
use the timedelta
to account for the timezone, like this:
from datetime import datetime,timedelta
defdt_parse(t):
ret = datetime.strptime(t[0:16],'%Y-%m-%dT%H:%M')
if t[18]=='+':
ret+=timedelta(hours=int(t[19:22]),minutes=int(t[23:]))
elif t[18]=='-':
ret-=timedelta(hours=int(t[19:22]),minutes=int(t[23:]))
return ret
Post a Comment for "Why %z Is Not Supported By Python's Strptime?"