8

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

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

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

日期时间.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'

时间.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

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

关注信息

似乎dateutil 的 tzinfos 参数应该是完美的,但它也不起作用......或者我误读了跟踪并做错了什么。(我以此为例

>>> 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,第二轮

似乎这个 stackoverflow 答案正在接近我可能需要的 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 时区的常见问题是它们的符号相反。这是一个 UTC(“无偏移”)posix 时区,我发现处理“etc”的许多问题都与这个反向偏移有关。

4

1 回答 1

1

这个怎么样:

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

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
于 2013-08-22T19:30:20.817 回答