所以问题是00:00时区偏移. Looking into this there’s not an out of the box solution for Python
datetime.datetime.strptime("2016-04-01 17:29:25+00:00", '%Y-%m-%d %H:%M:%S %z')
Traceback (most recent call last):
File "", line 1, in
File "/usr/lib/python2.7/_strptime.py", line 324, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
一种创可贴解决方案是剥离时区,但感觉很糟糕.
datetime.datetime.strptime("2016-04-01 17:29:25+00:00".rstrip("+00:00"), '%Y-%m-%d %H:%M:%S')
datetime.datetime(2016, 4, 1, 17, 29, 25)
环顾四周看起来(如果可以使用第三方库)dateutil解决了此问题,因此使用datetime.strptime更好.
在命令行上
pip install python-dateutil
码
>>> import dateutil.parser
>>> dateutil.parser.parse("2016-04-01 17:29:25+00:00")
datetime.datetime(2016, 4, 1, 17, 29, 25, tzinfo=tzutc())