How to convert a timezone aware string to datetime in Python without dateutil?(python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?)
问题描述
我必须将像 "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.
这篇关于python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:python - 如何在没有dateutil的情况下将时区感知字符串转换为Python中的日期时间?
基础教程推荐
- Python 中是否有任何支持将长字符串转储为块文字或折叠块的 yaml 库? 2022-01-01
- matplotlib 设置 yaxis 标签大小 2022-01-01
- 对多索引数据帧的列进行排序 2022-01-01
- Python,确定字符串是否应转换为 Int 或 Float 2022-01-01
- 在 Python 中将货币解析为数字 2022-01-01
- 比较两个文本文件以找出差异并将它们输出到新的文本文件 2022-01-01
- 在 Django Admin 中使用内联 OneToOneField 2022-01-01
- Kivy 使用 opencv.调整图像大小 2022-01-01
- 究竟什么是“容器"?在蟒蛇?(以及所有的 python 容器类型是什么?) 2022-01-01
- kivy 应用程序中的一个简单网页作为小部件 2022-01-01
