gpt4 book ai didi

python - 如何用冒号解析时区

转载 作者:IT老高 更新时间:2023-10-28 20:47:59 26 4
gpt4 key购买 nike

有没有办法用 datetime.strptime 解析“+00:00”格式的时区?例如:

Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (In
tel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from datetime import datetime
>>> datetime.strptime("12:34:56+0000", "%X%z")
datetime.datetime(1900, 1, 1, 12, 34, 56, tzinfo=datetime.timezone.utc)
>>> datetime.strptime("12:34:56+00:00", "%X%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 '12:34:56+00:00' does not match format '%X%z'

有什么想法吗?

最佳答案

目前,对此没有治愈方法,这里有解释:https://bugs.python.org/issue15873更准确地说,这里是:https://bugs.python.org/msg169952 .但是你可以通过这种方式覆盖这个问题:

from datetime import datetime
d = "2015-04-30T23:59:59+00:00"
if ":" == d[-3:-2]:
d = d[:-3]+d[-2:]
print(datetime.strptime(d, "%Y-%m-%dT%H:%M:%S%z"))

关于python - 如何用冒号解析时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30999230/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com