有没有办法只使用标准库模块从Python中的字符串中获取日期时间感知对象?
我知道我可以使用dateutil.parser.parse
,但遗憾的是,将其作为依赖项添加到我的项目中并不是一个足够好的理由。我已经将mx.DateTime
模块作为依赖项,buuttttt:
>>> dateutil.parser.parse('2011-10-24T06:51:47-07:00')
datetime.datetime(2011, 10, 24, 6, 51, 47, tzinfo=tzoffset(None, -25200))
>>> mx.DateTime.ISO.ParseDateTimeUTC('2011-10-24T06:51:47-07:00')
<mx.DateTime.DateTime object for '2011-10-24 13:51:47.00' at 29c7e48>
ParseDateTimeUTC
未能检测到偏移量,即使在其文档中说明:
Returns a DateTime instance in UTC reflecting the given ISO
date. A time part is optional and must be delimited from the
date by a space or 'T'. Timezones are honored.
答案 0 :(得分:2)
mx.DateTime.ISO.ParseDateTimeUTC
正在做正确的事情 - 它正在应用指定的时区来调整UTC的时间。生成的UTC时间没有时区,因为它不再是本地时间。
标准Python库不包含任何具体时区类according to the documentation:
tzinfo是一个抽象的基础clase,意思是这个类不应该 直接实例化。您需要派生一个具体的子类,并且 (至少)提供标准tzinfo方法的实现 您使用的日期时间方法所需。 日期时间模块没有 提供tzinfo的任何具体子类。
我一直感到惊讶的是,他们至少没有包括UTC的类,或者像dateutil的tzoffset
这样的通用实现。