我的 datetime.strptime 格式有什么问题?

Whats wrong with my datetime.strptime format?

有了这个功能:

timestamp = datetime.strptime(date_str, date_fmt)

我收到这个错误:

ValueError: time data 'Sun, 28 Oct 2018 07:33:13 -0400 (EDT)' does not match format '%a, %d %b %Y %H:%M:%S %z (%Z)'

Sun, 28 Oct 2018 07:33:13 -0400 (EDT)
%a, %d %b %Y %H:%M:%S %z (%Z)

looked over it a dozen times 我不知道我做错了什么。

我的Python版本:

Python 3.7.0 (default, Jul 23 2018, 20:24:19) 

除了您的当前时区,不支持 %Z 输入。只有 time.tzname tuple 加上 'UTC''GMT' 中的字符串才会被识别:

>>> from datetime import datetime
>>> import time
>>> time.tzname
('GMT', 'BST')
>>> sample = 'Sun, 28 Oct 2018 07:33:13 -0400 (EDT)'
>>> datetime.strptime(sample.replace('EDT', time.tzname[0]), '%a, %d %b %Y %H:%M:%S %z (%Z)')
datetime.datetime(2018, 10, 28, 7, 33, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000), 'GMT'))
>>> datetime.strptime(sample.replace('EDT', time.tzname[1]), '%a, %d %b %Y %H:%M:%S %z (%Z)')
datetime.datetime(2018, 10, 28, 7, 33, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000), 'BST'))

即使您在输入中使用 'known' 时区名称,该名称也会被忽略,除非在输入中也有时区偏移量 (%z);对于 %z 偏移量,它仅用于设置 datetime.timezone() instancename 参数,该参数由 %z 偏移量构建。

不幸的是,此行为没有明确记录,请参阅 Python 问题 #22377 and #22426。文档似乎 暗示 EST 将是 %Z 解析的可接受值,但文档的该部分仅显示 datetime.strftime() 字符串输出,不可接受 datetime.strptime() 字符串输入。

由于您在字符串中也有与 UTC 的偏移量(-0400 部分),只需从您的输入中删除时区名称,而不必费心去解析它:

>>> datetime.strptime(sample.rpartition(' ')[0], '%a, %d %b %Y %H:%M:%S %z')
datetime.datetime(2018, 10, 28, 7, 33, 13, tzinfo=datetime.timezone(datetime.timedelta(days=-1, seconds=72000)))

我在这里使用 str.rpartition() 来删除从最后一个 space 开始的所有内容,但这取决于您拥有哪种输入,如何最好地删除应用程序中的时区名称部分。没有它,就不需要 (%Z) 部分,你会得到一个正确的 datetime 对象。

调试 datetime.strptime() 问题时,您想 划分 问题。您可以将输入和模板部分一分为二以查看问题出在哪里,或者一个一个地尝试不同的组件。它可能是导致问题的单个指令,也可能是多个指令,因此我通常会单步删除指令,直到一个指令起作用。


对于那些对 datetime.strptime() 工作原理的细节感兴趣的人,您可以使用调试器进入调用作为实现 uses a pure Python module. For datetime.strptime(), the entry point is _strptime._strptime_datetime(), and the %Z parameter matching pattern is generated by this little loop, where self.locale_time.timezone is a 2-value tuple of frozensets with 3-letter strings, which was set by the LocaleTime.__calc_timezone() method