简体   繁体   English

如何比较python中的两个时区?

[英]How to compare two timezones in python?

Example

 import pytz
 b=pytz.timezone('Europe/Rome')
 c=pytz.timezone('Europe/Berlin')

These two timezones have different names but represent the same thing, however 然而,这两个时区有不同的名称,但代表相同的东西

  • b==c returns false b == c返回false
  • b.zone is different than c.zone b.zone与c.zone不同

Is there any way to see that b is in reality equal to c? 有没有办法看到b实际上等于c?

The concrete problem is that I have to convert the timezone of a pandas data frame, but only if this zone is different than let's say c. 具体的问题是我必须转换pandas数据帧的时区,但只有当这个区域不同于让我们说c时。 The original timezone might be b and in this case I do not want to convert as it would be a lost of time to convert b into c (since they represent the same time zones at the end....) 原始时区可能是b,在这种情况下我不想转换,因为将b转换为c会丢失时间(因为它们代表最后的相同时区....)

Thanks for any help. 谢谢你的帮助。

Update : changed 'CET' into 'Europe/Rome' to make sure that the timezones are the same in the example, using the feedback from an answer 更新 :将'CET'更改为'Europe / Rome'以确保示例中的时区相同,使用答案的反馈

They do not represent the same thing. 它们并不代表同一件事。

  • "CET" is always UTC+01:00 "CET" 始终为 UTC + 01:00

  • "Europe/Berlin" alternates between CET (UTC+01:00) in the winter, and CEST (UTC+02:00) in the summer. "Europe/Berlin"在冬季的CET(UTC + 01:00)和夏季的CEST(UTC + 02:00)之间交替。

See also: 也可以看看:

With regards to the edit, Europe/Rome is a distinct time zone. 关于编辑, Europe/Rome是一个独特的时区。 It is not the same as Europe/Berlin , nor Europe/Zurich , nor Europe/Amsterdam . 它与Europe/BerlinEurope/ZurichEurope/Amsterdam At least not for their entire histories. 至少不是因为他们的整个历史。

If you compare their definitions (using the links in the prior paragraph), you'll see that these each aligned to the "EU" rule for CET/CEST at some point in their past. 如果你比较它们的定义(使用前一段中的链接),你会发现它们在过去的某个时刻都与CET / CEST的“EU”规则一致。 Rome and Berlin since 1980, Zurich since 1981, and Amsterdam since 1977. Before those dates, they differed significantly. 自1980年以来的罗马和柏林,自1981年以来的苏黎世,以及自1977年以来的阿姆斯特丹。 Other time zones have different rules as well. 其他时区也有不同的规则。

If you're interested in the history of these zones, I suggest reading through the europe file in the TZ data. 如果您对这些区域的历史感兴趣,我建议您阅读TZ数据中的欧洲文件 The comments alone are quite interesting. 仅这些评论非常有趣。

On the other hand, if you are only working with modern dates, where all zones are following the same rules and offsets, then you could consider these substitutable - at least as long as they don't change in the future. 另一方面,如果您只处理现代日期,其中所有区域遵循相同的规则和抵消,那么您可以考虑这些可替代 - 至少只要它们未来不会改变。

Also, there are some time zones that are just aliases and are completely interchangeable. 此外, 还有一些时区中只是别名,是完全可以互换的。 In the TZ data, they're called "links". 在TZ数据中,它们被称为“链接”。 For example, you can see here that Europe/Vatican and Europe/San_Marino are both linked to Europe/Rome , and are therefore equivalent. 例如,你可以在这里看到Europe/VaticanEurope/San_Marino都与Europe/Rome相连,因此是等价的。

It's kind of ghetto, but I could compare the offsets of both timezones against a given a timestamp. 这是一种贫民窟,但我可以将两个时区的偏移与给定的时间戳进行比较。

from datetime import datetime
today = datetime.today()
b.utcoffset(today) == c.utcoffset(today)

If the only reason for not wanting to convert is because of inefficiency, I would question whether this is really necessary. 如果不想转换的唯一原因是效率低下,我会质疑这是否真的有必要。 There is a good blog post by Wes McKinney on vectorized datetime conversion http://wesmckinney.com/blog/?p=506 . Wes McKinney在矢量化日期时间转换http://wesmckinney.com/blog/?p=506上有一篇很好的博客文章。 As an example, for a series with 1e6 rows 例如,对于具有1e6行的系列

In [1]: from pandas import *
In [2]: import numpy as np
In [3]: rng = date_range('3/6/2012', periods=1000000, freq='s', tz='US/Eastern')
In [4]: ts = Series(np.random.randn(len(rng)),rng)
In [5]: %timeit ts.tz_convert('utc')
100 loops, best of 3: 2.17 ms per loop

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM