wan*_*ghq 3 python timezone datetime strptime python-datetime
>>> datetime.strptime('2014-02-13 11:55:00 -0800', '%Y-%m-%d %H:%M:%S %z')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 317, in _strptime
(bad_directive, format))
ValueError: 'z' is a bad directive in format '%Y-%m-%d %H:%M:%S %z'
Run Code Online (Sandbox Code Playgroud)
在Python 3.2之前,Python的datetime模块没有timezone()对象.它通过提供datetime.tzinfo()抽象基类来支持提供时区的第三方库,但不包括时区对象.没有时区对象,也不支持解析时区偏移.
从Python 3.2开始,z支持,因为该版本(及以上)添加了一个datetime.timezone()类型:
>>> 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))
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
927 次 |
| 最近记录: |