datetime.strptime奇怪的行为 [英] datetime.strptime strange behavior
问题描述
我在运行python 3.6.8的aws虚拟机上遇到以下错误,而在我的笔记本电脑上,它在python 3.6.1上正常工作
I'm getting the following error on aws virtual machine running python 3.6.8, while on my laptop it works fine with python 3.6.1
return datetime.strptime(date_str, self.date_format)
File "/usr/lib64/python3.6/_strptime.py", line 565, in _strptime_datetime
tt, fraction = _strptime(data_string, format)
File "/usr/lib64/python3.6/_strptime.py", line 362, in _strptime
(data_string, format))
ValueError: time data 'Fri, 23 Aug 2019 20:24:46 IDT' does not match format '%a, %d %b %Y %H:%M:%S %Z'
我使用的格式有什么问题?
What's wrong with the format that I'm using?
推荐答案
请参阅官方Python错误跟踪器上的此讨论。显然%Z
仅支持 UTC
, GMT
和本地时区(由 time.tzname
返回)。
See this discussion on the official Python bug tracker. Apparently %Z
only supports UTC
, GMT
and the local time zone (as returned by time.tzname
).
这是一个有趣的情况,官方Python文档具有误导性(说得很好):
It's an interesting case, the official Python docs are misleading (to put it nicely):
%Z
时区名称(如果对象是天真对象,则为空字符串)。(空),UTC,EST,CST
%Z Time zone name (empty string if the object is naive).(empty), UTC, EST, CST
这是错误的。 %Z
仅会识别 EST
, CST
等。 如果它们是操作系统的本地时区。
This is wrong. %Z
will only recognise EST
, CST
, etc. if they are the local timezone of the OS.
EDIT 嗯,在底部附近有一个注释文档中显示以下内容的页面
EDIT Well, there is a note near the bottom of the page in the docs that says
%Z
如果tzname(
)返回None
,%Z
替换为空字符串。否则,%Z
将被返回值替换,该值必须是字符串。
%Z
Iftzname(
) returnsNone
,%Z
is replaced by an empty string. Otherwise%Z
is replaced by the returned value, which must be a string.
仍然,但不够清晰。
要清楚一点:
您的本地计算机,因为它使用IDT作为其本地时区,而不是AWS远程计算机。
It works on your local machine because it uses IDT as its local timezone, which is not the case for the AWS remote machine.
这篇关于datetime.strptime奇怪的行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!