pytz:为什么在时区之间转换时需要标准化?

发布于 2024-08-05 12:46:55 字数 880 浏览 2 评论 0原文

我正在阅读不太完整的 pytz 文档,并且我一直在理解其中的一部分。

时区之间的转换也需要特别注意。这还需要使用normalize方法来保证转换正确。

>>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899))
>>> utc_dt.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'
>>> au_tz = timezone('Australia/Sydney')
>>> au_dt = au_tz.normalize(utc_dt.astimezone(au_tz))
>>> au_dt.strftime(fmt)
'2006-03-27 08:34:59 EST+1100'
>>> utc_dt2 = utc.normalize(au_dt.astimezone(utc))
>>> utc_dt2.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'

我尝试了这个例子,没有使用标准化,结果是一样的。在我看来,这个例子并没有真正解释为什么我们在不同时区的datetime对象之间进行转换时必须使用normalize

请有人给我一个示例(如上面的示例),其中不使用标准化时结果会有所不同。

谢谢

I'm reading the not so complete pytz documentation and I'm stuck on understand one part of it.

Converting between timezones also needs special attention. This also needs to use the normalize method to ensure the conversion is correct.

>>> utc_dt = utc.localize(datetime.utcfromtimestamp(1143408899))
>>> utc_dt.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'
>>> au_tz = timezone('Australia/Sydney')
>>> au_dt = au_tz.normalize(utc_dt.astimezone(au_tz))
>>> au_dt.strftime(fmt)
'2006-03-27 08:34:59 EST+1100'
>>> utc_dt2 = utc.normalize(au_dt.astimezone(utc))
>>> utc_dt2.strftime(fmt)
'2006-03-26 21:34:59 UTC+0000'

I tried this very example without using normalize and it turned out just the same. In my opinion this example doesn't really explain why we have to use normalize when converting between datetime objects in different timezones.

Would someone please give me an example (like the one above) where the result differs when not using normalize.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

关于从前 2024-08-12 12:46:56

来自 pytz 文档:

此外,如果您对跨越 DST 边界的当地时间执行日期算术,则结果可能位于不正确的时区(即,从 2002-10-27 1:00 EST 减去 1 分钟,得到 2002-10- 27 0:59 EST 而不是正确的 2002-10-27 1:59 EDT)。提供了 normalize() 方法来纠正此问题。不幸的是,如果不修改 Python 日期时间实现,这些问题就无法解决。

From the pytz documentation:

In addition, if you perform date arithmetic on local times that cross DST boundaries, the results may be in an incorrect timezone (ie. subtract 1 minute from 2002-10-27 1:00 EST and you get 2002-10-27 0:59 EST instead of the correct 2002-10-27 1:59 EDT). A normalize() method is provided to correct this. Unfortunately these issues cannot be resolved without modifying the Python datetime implementation.

若水微香 2024-08-12 12:46:56

文档说,标准化被用作 DST 问题的解决方法:

此外,如果您对跨越 DST 边界的当地时间执行日期算术,则结果可能位于不正确的时区(即,从 2002-10-27 1:00 EST 减去 1 分钟,得到 2002-10- 27 0:59 EST 而不是正确的 2002-10-27 1:59 EDT)。提供了 normalize() 方法来纠正此问题。

因此它用于纠正一些涉及 DST 的边缘情况。如果您不使用 DST 时区(例如 UTC),则无需使用标准化。

如果您不使用它,在某些情况下您的转化可能会延迟一小时。

The docs say that normalize is used as a workaround for DST issues:

In addition, if you perform date arithmetic on local times that cross DST boundaries, the results may be in an incorrect timezone (ie. subtract 1 minute from 2002-10-27 1:00 EST and you get 2002-10-27 0:59 EST instead of the correct 2002-10-27 1:59 EDT). A normalize() method is provided to correct this.

So it's used to correct some edge cases involving DST. If you're not using DST timezones (e.g. UTC) then it's not necessary to use normalize.

If you don't use it your conversion could potentially be one hour off under certain circumstances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文