python 日期时间模块支持 ISO8601 格式

ISO8601 format support in python datetime module

我在 python 中阅读了很多关于解析 ISO8601 的问题,但大多数问题都使用外部依赖项。 然后我碰到了这个问题,

How to parse an ISO 8601-formatted date?

说明python不支持iso8601格式,但答案是3岁

我需要在不使用任何外部依赖项的情况下解析这个日期,

from datetime import datetime
app_login = "1996-12-19T16:39:57+08:00"
parse_app_login = datetime.strptime(x,"%Y-%m-%dT%H:%M:%S%z")
print(parse_app_login)

我收到错误:

ValueError: time data '1996-12-19T16:39:57+08:00' does not match format '%Y-%m-%dT%H:%M:%S%z'

我想知道为什么python不支持iso8601格式?

请注意 Python 3.7 支持 ISO8601 UTC 偏移量格式,

From Docs,

Changed in version 3.7: When the %z directive is provided to the strptime() method, the UTC offsets can have a colon as a separator between hours, minutes and seconds. For example, '+01:00:00' will be parsed as an offset of one hour. In addition, providing 'Z' is identical to '+00:00'.

使用 Python 3.7.0(v3.7.0:1bf9cc5093,2018 年 6 月 27 日,04:06:47),

>>> from datetime import datetime
>>> x = "2018-10-18T16:39:57+08:00"
>>> y = datetime.strptime(x,"%Y-%m-%dT%H:%M:%S%z")
>>> print(y)

输出,

2018-10-18 16:39:57+08:00