PYTZ 'America/Edmonton' 偏移错误 [英] PYTZ 'America/Edmonton' offset wrong

查看:45
本文介绍了PYTZ 'America/Edmonton' 偏移错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<块引用>

可能的重复:
pytz 的奇怪时区问题

这似乎是错误的:

<预><代码>>>>进口pytz>>>z1 = timezone('美国/埃德蒙顿')>>>z2 = timezone('美国/山区')>>>z1<DstTzInfo '美国/埃德蒙顿' LMT-1 天,16:26:00 STD>>>>z2<DstTzInfo 'US/Mountain' MST-1 天,17:00:00 STD>>>>pytz.VERSION'2012f'>>>

美国/埃德蒙顿"和美国/东部"应该是同一时区(标准时间 17:00:00).更何况 16:26:00 没有任何意义.

-- 更新--

以上在 Jon Skeet 的回答中是有道理的.但是,当我这样做时,事情变得很奇怪:

<预><代码>>>>d = datetime.now()>>>ddatetime.datetime(2012, 10, 9, 15, 21, 41, 644706)

我创建了一个天真的约会.由于美国/埃德蒙顿"我的时区,我尝试手动设置:

<预><代码>>>>d2 = d.replace(tzinfo=timezone('美国/埃德蒙顿'))>>>d2datetime.datetime(2012, 10, 9, 15, 21, 41, 644706, tzinfo=<DstTzInfo 'America/Edmonton' LMT-1 day, 16:26:00 STD>)

这不应该有任何改变,因为那是正确的 TZ.但是:

<预><代码>>>>d2.astimezone(timezone('美国/东部'))datetime.datetime(2012, 10, 9, 18, 55, 41, 644706, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

这应该给我 2 小时的偏移(美国/东部"和美国/埃德蒙顿"之间的差异)但它给了我 3 小时 26 分钟(即 2 小时 1 小时26 分钟 :D)

插入 timezone('US/Mountain')astimezone() 中产生正确的结果.使用America/Edmonton"创建有意识的日期时间也可以正常工作.

解决方案

pytz 的文档 明确指出直接从时区创建日期时间在所有情况下都不起作用,并指示您改为执行以下操作:

d2 = timezone('America/Edmonton').localize(d)

Possible Duplicate:
Weird timezone issue with pytz

This seems wrong:

>>> import pytz
>>> z1 = timezone('America/Edmonton')
>>> z2 = timezone('US/Mountain')
>>> z1
<DstTzInfo 'America/Edmonton' LMT-1 day, 16:26:00 STD>
>>> z2
<DstTzInfo 'US/Mountain' MST-1 day, 17:00:00 STD>
>>> pytz.VERSION
'2012f'
>>> 

'America/Edmonton' and 'US/Eastern' should be the same time zone (17:00:00 STD). Not to mention 16:26:00 doesn't make any sense.

-- Update --

The above makes sense in context of Jon Skeet's answer. However, things get strange when I do this:

>>> d = datetime.now()
>>> d
datetime.datetime(2012, 10, 9, 15, 21, 41, 644706)

I created a naive date. Since 'America/Edmonton' is my timezone, I try to set that manually:

>>> d2 = d.replace(tzinfo=timezone('America/Edmonton'))
>>> d2
datetime.datetime(2012, 10, 9, 15, 21, 41, 644706, tzinfo=<DstTzInfo 'America/Edmonton' LMT-1 day, 16:26:00 STD>)

This should not have change anything because that is the correct TZ. However:

>>> d2.astimezone(timezone('US/Eastern'))
datetime.datetime(2012, 10, 9, 18, 55, 41, 644706, tzinfo=<DstTzInfo 'US/Eastern' EDT-1 day, 20:00:00 DST>)

This should give me an offset of 2 hours (difference between 'US/Eastern' and 'America/Edmonton') but it gives me 3 hours 26 minutes (which is 2 hours plus one hour 26 minutes :D )

inserting timezone('US/Mountain') produces the correct result in astimezone(). Creating an aware datetime with 'America/Edmonton' will also work correctly.

解决方案

The documentation for pytz explicitly says that creating a datetime directly from a timezone won't work in all cases, and directs you to do the following instead:

d2 = timezone('America/Edmonton').localize(d)

这篇关于PYTZ 'America/Edmonton' 偏移错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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