为什么python的strptime不支持%z? [英] Why %z is not supported by python's strptime?

查看:51
本文介绍了为什么python的strptime不支持%z?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

>>> 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'

我知道它不受支持,但不知道为什么.似乎并不难支持.而且,"与UTC的偏移量"不像时区缩写那样模棱两可.

I understand that it's not supported, but don't know why. Seems it's not hard to support that. And 'Offset from UTC' is not as ambiguous as timezone abbreviation.

推荐答案

直到Python 3.2,Python的 datetime 模块没有 timezone()对象.它通过提供 datetime.tzinfo来支持提供时区的第三方库.()抽象基类,但不包含时区对象.没有时区对象,也不支持解析时区偏移.

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.

从Python 3.2开始,支持 z ,因为该版本(及更高版本)添加了

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))

这篇关于为什么python的strptime不支持%z?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆