gpt4 book ai didi

python - 无法在python中使用posix风格的时区解析解析时间字符串

转载 作者:太空狗 更新时间:2023-10-30 00:17:38 25 4
gpt4 key购买 nike

我有一个这样的时间字符串:2013-08-22 16:56:19 Etc/GMT

我需要将它解析为日期时间对象。我被 posix 风格的时区挂断了,我无法让 Python 本地理解它。

这里有一些尝试和他们的失败。我首先包括时区剥离版本以显示解析在其他方面是正确的。

datetime.strptime

>>> datetime.datetime.strptime("2013-08-22 16:56:19 UTC", "%Y-%m-%d %H:%M:%S %Z")
datetime.datetime(2013, 8, 22, 16, 56, 19)
>>> datetime.datetime.strptime("2013-08-22 16:56:19 Etc/GMT", "%Y-%m-%d %H:%M:%S %Z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '2013-08-22 16:56:19 Etc/GMT' does not match format '%Y-%m-%d %H:%M:%S %Z'

time.strptime

>>> time.strptime("2013-08-22 16:56:19 UTC", "%Y-%m-%d %H:%M:%S %Z")
time.struct_time(tm_year=2013, tm_mon=8, tm_mday=22, tm_hour=16, tm_min=56, tm_sec=19, tm_wday=3, tm_yday=234, tm_isdst=0)
>>> time.strptime("2013-08-22 16:56:19 Etc/GMT", "%Y-%m-%d %H:%M:%S %Z")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 454, in _strptime_time
return _strptime(data_string, format)[0]
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/_strptime.py", line 325, in _strptime
(data_string, format))
ValueError: time data '2013-08-22 16:56:19 Etc/GMT' does not match format '%Y-%m-%d %H:%M:%S %Z'

dateutil.parser

>>> dateutil.parser.parse("2013-08-22 16:56:19")
datetime.datetime(2013, 8, 22, 16, 56, 19)
>>> dateutil.parser.parse("2013-08-22 16:56:19 Etc/GMT")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.1-py2.7.egg/dateutil/parser.py", line 720, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.1-py2.7.egg/dateutil/parser.py", line 310, in parse
raise ValueError("unknown string format")
ValueError: unknown string format

尝试过的可能的解决方案/途径

tzinfos

好像dateutil's tzinfos argument应该是完美的,但它也不起作用......或者我误读了跟踪并做错了什么。 ( I used this as an example )

>>> dateutil.parser.parse("2013-08-22 16:56:19 Etc/GMT", tzinfos={ 'Etc/GMT': pytz.timezone('UTC') })
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.1-py2.7.egg/dateutil/parser.py", line 720, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.1-py2.7.egg/dateutil/parser.py", line 310, in parse
raise ValueError("unknown string format")
ValueError: unknown string format

tzinfos,第二轮

好像this stackoverflow answer正在逐步使用我可能需要的 tzinfos。我尝试了上面的简化版本(其中 value=offset seconds)。仍然失败。

>>> dateutil.parser.parse("2013-08-22 16:56:19 Etc/GMT", tzinfos={ 'Etc/GMT': 0 })
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.1-py2.7.egg/dateutil/parser.py", line 720, in parse
return DEFAULTPARSER.parse(timestr, **kwargs)
File "/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/python_dateutil-2.1-py2.7.egg/dateutil/parser.py", line 310, in parse
raise ValueError("unknown string format")
ValueError: unknown string format

做错了

我总是可以使用正则表达式或字符串匹配之类的东西来查找和更改时区,但感觉不对

红鲱鱼

posix ETC/xxx 时区的常见问题是 they have reversed signs .这是一个 UTC(“无偏移量”)posix 时区,我发现许多与“etc”有关的问题都与这种反向偏移量有关。

最佳答案

这个怎么样:

  • 通过strptime()将不带时区部分的日期字符串解析为datetime对象
  • 将时区字符串解析为pytz时区
  • 通过replace()更新日期时间对象上的tzinfo

from datetime import datetime
import pytz

date_string = "2013-08-22 16:56:19"
tz_string = "Etc/GMT"


dt = datetime.strptime(date_string, "%Y-%m-%d %H:%M:%S")
dt = dt.replace(tzinfo=pytz.timezone('Etc/GMT'))
print dt

打印:

2013-08-22 16:56:19+00:00

这实际上正确地理解和反转了 POSIX 时区格式的符号,例如:

dt = dt.replace(tzinfo=pytz.timezone('Etc/GMT-1'))
print dt # prints 2013-08-22 16:56:19+01:00

关于python - 无法在python中使用posix风格的时区解析解析时间字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18388788/

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