Python 从字符串中获取aware datetime对象
是否有一种方法可以仅使用标准库模块从Python中的字符串中获取datetime感知对象 我知道我可以使用Python 从字符串中获取aware datetime对象,python,datetime,Python,Datetime,是否有一种方法可以仅使用标准库模块从Python中的字符串中获取datetime感知对象 我知道我可以使用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
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>
mx.DateTime.ISO.ParseDateTimeUTC
做的是正确的-它应用指定的时区将时间调整为UTC。生成的UTC时间没有时区,因为它不再是本地时间
标准Python库不包含任何具体的时区类:
tzinfo是一个抽象的基类,这意味着这个类不应该
可以直接实例化。您需要派生一个具体的子类,并且
(至少)提供标准tzinfo方法的实现
您使用的datetime方法需要日期时间模块不存在
提供tzinfo的任何具体子类。
我一直很惊讶,他们至少没有包含UTC类,或者像dateutil的
tzoffset
这样的通用实现,我想你误解了文档。时区得到尊重,并用于将时间调整为UTC。生成的UTC时间没有时区,因为它不再是本地时间。Doh,现在有意义了,我没有查看ParseDateTimeUTC打印出来的实际datetime字符串。。。如果你加上你的评论作为回答,我会接受的。谢谢为了清楚起见,我正在寻找一个stdlib/mx.DateTime解析器,它类似于一个带有tzinfo字段集的DateTime对象
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.