因此在Python 3中,您可以使用.isoformat()生成ISO 8601日期,但是您无法将isoformat()创建的字符串转换回datetime对象,因为Python自己的日期时间指令不能正确匹配.也就是说,%z = 0500而不是05:00(由.isoformat()生成).
例如:
>>> strDate = d.isoformat()
>>> strDate
'2015-02-04T20:55:08.914461+00:00'
>>> objDate = datetime.strptime(strDate,"%Y-%m-%dT%H:%M:%S.%f%z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python34\Lib\_strptime.py", line 500, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "C:\Python34\Lib\_strptime.py", line 337, in _strptime
(data_string, format))
ValueError: time data '2015-02-04T20:55:08.914461+00:00' does not match format '%Y-%m-%dT%H:%M:%S.%f%z'
Run Code Online (Sandbox Code Playgroud)
来自Python的strptime文档:(https://docs.python.org/2/library/datetime.html#strftime-strptime-behavior)
%z UTC格式+ HHMM或-HHMM中的UTC偏移量(如果对象是天真的,则为空字符串).(空),+ 0000,-0400,+ 1030
因此,简而言之,Python甚至不遵守自己的字符串格式化指令.
我知道日期时间在Python中已经很糟糕了,但这真的超出了不合理的范围,变成了愚蠢的土地.
告诉我这不是真的.