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

2022-01-16 00:00:00 python datetime timezone rfc3339

问题描述

我必须将像 "2012-11-01T04:16:13-04:00" 这样的时区感知字符串转换为 Python datetime 对象.

I have to convert a timezone-aware string like "2012-11-01T04:16:13-04:00" to a Python datetime object.

我看到了具有解析功能的 dateutil 模块,但我真的不想使用它,因为它添加了依赖项.

I saw the dateutil module which has a parse function, but I don't really want to use it as it adds a dependency.

那我该怎么做呢?我尝试了类似以下的方法,但没有运气.

So how can I do it? I have tried something like the following, but with no luck.

datetime.datetime.strptime("2012-11-01T04:16:13-04:00", "%Y-%m-%dT%H:%M:%S%Z")


解决方案

从 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.

相关文章