Python dateutil反转了时区偏移符号? [英] Timezone offset sign reversed by Python dateutil?

查看:166
本文介绍了Python dateutil反转了时区偏移符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



显然这个功能是一个已知结果不仅是dateutil,还有其他解析功能。但是,除非应用预处理黑客,否则这将导致不正确的日期时间结果:

  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无关。其他软件也是这样。从 tz数据库

 #我们在区域名称和输出缩写中使用POSIX样式的符号,
#即使这与许多人预期的相反。
#POSIX在格林威治以西有积极的迹象,但很多人预计格林威治以东的
#正面迹象。例如,TZ ='Etc / GMT + 4'使用
#缩写GMT + 4,对应于UT
#(即格林威治以西)后4小时即使许多人都会期待到
#意味着在UT前4小时(即格林威治以东)。

几乎所有地方都使用tz数据库



示例:

  $ TZ = Etc / GMT-8日期+%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'


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 错误:GMT + 1被解析为GMT-1和@Lennart Regebro的回复:


解析GTM + 1这样实际上是一部分的Posix规范。
这是一个功能,而不是一个错误。


查看,它们在...之间定义了.opengroup.org / onlinepubs / 9699919799 / basedefs / V1_chap08.html#tag_08rel =nofollow> TZ glibc使用类似的定义



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

Does anyone know why python's dateutil reverses the sign of the GMT offset when it parses the datetime field?

Apparently this feature is a known outcome of not only dateutil but also other parsing functions. But this results in an INCORRECT datetime result unless a pre-processing hack is applied:

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

解决方案

It seems dateutil uses POSIX-style signs here. It is not related to Python. Other software does it too. From 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.

Example:

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

You probably expect a different timezone:

>>> 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'

Note: PST, not GMT.

Though dateutil uses POSIX-style signs even for the PST timezone abbreviation:

>>> 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'

datetime.strptime() in Python 3 interprets it "correctly":

$ 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'

Notice the sign.

Despite the confusion due to POSIX-style signs; dateutil behavior is unlikely to change. See dateutil bug: "GMT+1" is parsed as "GMT-1" and @Lennart Regebro's reply:

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

See how TZ environment variable is defined in the POSIX specification, glibc uses similar definition.

It is not clear why dateutil uses POSIX TZ-like syntax to interpret the timezone info in a time string. The syntax is not exactly the same e.g., POSIX syntax requires a semicolon: hh[:mm[:ss]] in the utc offset that is not present in your input.

这篇关于Python dateutil反转了时区偏移符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆