<p>改编自被拒绝的编辑:</p>
<p><strong>Python>;=3.7:</strong></p>
<pre><code>from datetime import datetime
d = "2019-12-25T23:59:59+00:00"
print(datetime.strptime(d, "%Y-%m-%dT%H:%M:%S%z"))
</code></pre>
<blockquote>
<p>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'.</p>
</blockquote>
<p><a href="https://docs.python.org/3.7/library/datetime.html" rel="nofollow noreferrer">https://docs.python.org/3.7/library/datetime.html</a></p>
<p><strong>Python<;=3.6</strong></p>
<p>没有内置方式,但最好的解决方案是:</p>
<pre><code>from datetime import datetime
d = "2019-12-25T23:59:59+00:00"
if ":" == d[-3]:
d = d[:-3]+d[-2:]
print(datetime.strptime(d, "%Y-%m-%dT%H:%M:%S%z"))
</code></pre>
<p>说明:
<a href="https://bugs.python.org/issue15873" rel="nofollow noreferrer">https://bugs.python.org/issue15873</a>
<a href="https://bugs.python.org/msg169952" rel="nofollow noreferrer">https://bugs.python.org/msg169952</a></p>