编程语言
首页 > 编程语言> > Python dateutil反转的时区偏移标志?

Python dateutil反转的时区偏移标志?

作者:互联网

有谁知道为什么python的dateutil在解析datetime字段时会反转GMT偏移的符号?

显然,这个功能不仅仅是dateutil的known outcome,还有其他解析功能.但是,除非应用预处理黑客,否则会导致INCORRECT日期时间结果:

from dateutil import parser

jsDT = 'Fri Jan 02 2015 03:04:05.678910 GMT-0800'
python_datetime = parser.parse(jsDT)
print(python_datetime)
>>> 2015-01-02 03:04:05.678910+08:00

jsDT = 'Fri Jan 02 2015 03:04:05.678910 GMT-0800'
if '-' in jsDT:
    jsDT = jsDT.replace('-','+')
elif '+' in jsDT:
    jsDT = jsDT.replace('+','-')
python_datetime = parser.parse(jsDT)
print(python_datetime)
>>> 2015-01-02 03:04:05.678910-08:00

解决方法:

似乎dateutil在这里使用POSIX风格的标志.它与Python无关.其他软件也是如此.从the tz database开始:

# We use POSIX-style signs in the Zone names and the output abbreviations,
# even though this is the opposite of what many people expect.
# POSIX has positive signs west of Greenwich, but many people expect
# positive signs east of Greenwich.  For example, TZ='Etc/GMT+4' uses
# the abbreviation "GMT+4" and corresponds to 4 hours behind UT
# (i.e. west of Greenwich) even though many people would expect it to
# mean 4 hours ahead of UT (i.e. east of Greenwich).

The tz database is used almost everywhere.

例:

$TZ=Etc/GMT-8 date +%z
+0800

你可能期望一个不同的时区:

>>> from datetime import datetime
>>> import pytz
>>> pytz.timezone('America/Los_Angeles').localize(datetime(2015, 1, 2, 3, 4, 5, 678910), is_dst=None).strftime('%Y-%m-%d %H:%M:%S.%f %Z%z')
'2015-01-02 03:04:05.678910 PST-0800'

注意:PST,而不是GMT.

虽然dateutil使用POSIX风格的标志,即使PST时区缩写:

>>> from dateutil.parser import parse
>>> str(parse('2015-01-02 03:04:05.678910 PST-0800'))
'2015-01-02 03:04:05.678910+08:00'

Python 3中的datetime.strptime()将其“正确”解释为:

$TZ=America/Los_Angeles python3                                               
...
>>> from datetime import datetime
>>> str(datetime.strptime('2015-01-02 03:04:05.678910 PST-0800', '%Y-%m-%d %H:%M:%S.%f %Z%z'))
'2015-01-02 03:04:05.678910-08:00'

注意标志.

尽管由于POSIX风格的标志造成了混乱; dateutil行为不太可能改变.请参阅dateutil bug:“GMT+1” is parsed as “GMT-1”和@Lennart Regebro的回复:

Parsing GTM+1 this way is actually a part of the Posix specification.
This is therefore a feature, and not a bug.

看看TZ environment variable is defined in the POSIX specification,glibc如何使用similar definition.

目前尚不清楚为什么dateutil使用类似POSIX TZ的语法来解释时间字符串中的时区信息.语法不完全相同,例如,POSIX语法需要在输入中不存在的utc偏移量中使用分号:hh [:mm [:ss]].

标签:python,datetime,python-dateutil
来源: https://codeday.me/bug/20190612/1222308.html