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中的日期时间?
基础教程推荐
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 包装空间模型 2022-01-01
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
