所以在Python3中,可以使用.isoformat()生成一个ISO8601日期,但是不能将由isoformat()创建的字符串转换回datetime对象,因为Python自己的datetime指令不匹配。即,%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 "", line 1, in
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'%z UTC offset in the form +HHMM or -HHMM (empty string if the the
object is naive). (empty), +0000, -0400, +1030
因此,简言之,Python甚至不遵守自己的字符串格式化指令。
我知道datetime在Python中已经很糟糕了,但这真的超出了不合理的范围,进入了一片愚蠢的土地。
告诉我这不是真的。