且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?

更新时间:2022-11-04 10:47:49

从 Python 3.7 开始,datetime.datetime.fromisoformat() 可以处理您的格式:

As of Python 3.7, datetime.datetime.fromisoformat() can handle your format:

>>> import datetime
>>> datetime.datetime.fromisoformat('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))

在较旧的 Python 版本中,您无法做到这一点,除非没有大量艰苦的手动时区定义.

In older Python versions you can't, not without a whole lot of painstaking manual timezone defining.

Python 不包含时区数据库,因为它会很快过时.相反,Python 依赖于可以具有更快发布周期的外部库来为您提供正确配置的时区.

Python does not include a timezone database, because it would be outdated too quickly. Instead, Python relies on external libraries, which can have a far faster release cycle, to provide properly configured timezones for you.

作为副作用,这意味着时区解析也需要是一个外部库.如果 dateutil 对你来说太重了,请使用 iso8601 相反,它会很好地解析您的特定格式:

As a side-effect, this means that timezone parsing also needs to be an external library. If dateutil is too heavy-weight for you, use iso8601 instead, it'll parse your specific format just fine:

>>> import iso8601
>>> iso8601.parse_date('2012-11-01T04:16:13-04:00')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=<FixedOffset '-04:00'>)

iso8601 高达 4KB 小.比较 python-dateutil 的 148KB.

iso8601 is a whopping 4KB small. Compare that tot python-dateutil's 148KB.

从 Python 3.2 开始,Python 可以处理简单的基于偏移的时区,并且 %z 将解析 -hhmm+hhmm 时间戳中的时区偏移量.这意味着对于 ISO 8601 时间戳,您必须删除时区中的 ::

As of Python 3.2 Python can handle simple offset-based timezones, and %z will parse -hhmm and +hhmm timezone offsets in a timestamp. That means that for a ISO 8601 timestamp you'd have to remove the : in the timezone:

>>> from datetime import datetime
>>> iso_ts = '2012-11-01T04:16:13-04:00'
>>> datetime.strptime(''.join(iso_ts.rsplit(':', 1)), '%Y-%m-%dT%H:%M:%S%z')
datetime.datetime(2012, 11, 1, 4, 16, 13, tzinfo=datetime.timezone(datetime.timedelta(-1, 72000)))

Python 问题 15873 中正在跟踪缺乏正确的 ISO 8601 解析.

The lack of proper ISO 8601 parsing is being tracked in Python issue 15873.