Python pytz 时区函数返回关闭 9 分钟的时区 [英] Python pytz timezone function returns a timezone that is off by 9 minutes

查看:26
本文介绍了Python pytz 时区函数返回关闭 9 分钟的时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于某种原因,我还没有弄清楚,从以下代码:

<预><代码>>>>从 pytz 导入时区>>>时区('美国/芝加哥')

我明白了:

我想什么时候我应该得到:

...因为我不认为我的时区与 UTC 相差 6 小时 9 分钟.

我已经查看了 pytz 的源代码,但我承认我一直无法弄清楚出了什么问题.

我已将其他值传递给 timezone() 函数,它返回的值似乎是正确的.但出于某种原因,与我的时区相关的信息不正确.

最后,我旁边立方体中的同事确认该函数在他的机器上返回了正确的时区信息.

有谁知道为什么我的时区('America/Chicago')会偏离 9 分钟?我正在运行使用 pip 安装的 pytz2015.7 版本.谢谢!

解决方案

除非您的本地时区具有固定的 UTC 偏移量,否则在不提供特定日期/时间的情况下谈论其特定值是毫无意义的.

如果您提供时间,例如当前时间,那么您将看到 pytz 产生预期的 UTC 偏移量:

<预><代码>>>>从日期时间导入日期时间>>>进口pytz>>>datetime.now(pytz.timezone('America/Chicago')).strftime('%Z%z')'CST-0600'

如果您不提供特定的日期/时间,则 pytz 可能会从给定时区的可用 utc 偏移集中返回一个任意的 utc 偏移.最近的 pytz 版本返回对应于最早时间的 utc 偏移量(通常为 LMT),但您不应该依赖它.您和您的朋友可能使用不同的 pytz 版本,这可能解释了结果的差异.

For some reason which I haven't been able to figure out yet, from the the following code:

>>> from pytz import timezone
>>> timezone('America/Chicago')

I get:

<DstTzInfo 'America/Chicago' LMT-1 day, 18:09:00 STD>

When, I assume, I should get:

<DstTzInfo 'America/Chicago' LMT-1 day, 18:00:00 STD>

...since I don't think that my timezone is 6 hours and 9 minutes away from UTC.

I have looked at the source code for pytz but I will admit that I haven't exactly been able to figure out what is going wrong.

I have passed other values to the timezone() function, and the values it returns appear to be correct. For some reason though, the information relevant to my timezone is not correct.

Finally, my co-worker in the cube next to me has confirmed that the function returns the correct timezone info on his machine.

Does anyone have any idea why my timezone ('America/Chicago') would be off by 9 minutes? I am running version 2015.7 of pytz installed using pip. Thank you!

解决方案

Unless your local timezone has a fixed UTC offset then it is pointless to talk about its specific value without providing a specific date/time.

If you provide the time e.g., the current time then you'll see that pytz produces the expected UTC offset:

>>> from datetime import datetime
>>> import pytz
>>> datetime.now(pytz.timezone('America/Chicago')).strftime('%Z%z')
'CST-0600'

See

If you don't provide a specific date/time then pytz may return an arbitrary utc offset from the set of available utc offsets for the given timezone. The recent pytz versions return utc offsets that correspond to the earliest time (LMT as a rule) but you should not rely on it. You and your friend may use different pytz versions that may explain the difference in results.

这篇关于Python pytz 时区函数返回关闭 9 分钟的时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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