无法使用python中的posix样式时区解析解析时间字符串

时间:2013-08-22 19:00:37

标签: python datetime timezone posix

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

我需要将它解析为datetime对象。我已经陷入了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时区,我发现处理“等”的许多问题都与这个反向偏移有关。

1 个答案:

答案 0 :(得分:1)

这个怎么样:

  • 通过datetime
  • 将没有时区部分的日期字符串解析为strptime()对象
  • 将时区字符串解析为pytz timezone
  • 通过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