How to compare two time zones in python?

Example

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

These two time zones have different names, but represent the same thing, however

  • b == c returns false
  • b.zone is different from c.zone

Is there a way to see that b is actually equal to c?

The specific problem is that I need to convert the timezone to a pandas data frame, but only if this zone is different from what we say c. The initial time zone may be b, in which case I do not want to convert, since that would be a lost time for converting b to c (since they represent the same time zones at the end ....)

Thanks for any help.

Update: changed β€œCET” to β€œEurope / Rome” to ensure that the time intervals in this example are the same using feedback from the response

+6
source share
3 answers

This is a kind of ghetto, but I could compare the offsets of both time zones with a given time stamp.

 from datetime import datetime today = datetime.today() b.utcoffset(today) == c.utcoffset(today) 
+3
source

They do not represent the same thing.

  • "CET" always UTC + 01: 00

  • "Europe/Berlin" alternates between CET (UTC + 01: 00) in the winter, and CEST (UTC + 02: 00) in the summer.

See also:

As for editing, Europe/Rome is a separate time zone. This is not the same as Europe/Berlin , but Europe/Zurich , Europe/Amsterdam . At least not for their whole story.

If you compare their definitions (using the links in the previous paragraph), you will see that each of them complies with the EU rule for CET / CEST at some point in their past. Rome and Berlin since 1980, Zurich since 1981 and Amsterdam since 1977. Until these dates, they varied significantly. Other time zones also have different rules.

If you are interested in the history of these zones, I suggest reading the europe data in the TZ data. Comments are very interesting.

On the other hand, if you work only with modern dates, when all zones follow the same rules and offsets, then you can consider their substitutions - at least until they change in the future.

In addition, there are some time zones that are simply aliases and are completely interchangeable. In TZ data, they are called "links." For example, you can see here that Europe/Vatican and Europe/San_Marino are related to Europe/Rome and therefore equivalent.

+4
source

If the only reason you do not want to convert is because of inefficiency, I would question if this is really necessary. There is a good Wes McKinney blog post about vectorized date / time conversion http://wesmckinney.com/blog/?p=506 . As an example, for a series with 1e6 lines

 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 
+1
source

Source: https://habr.com/ru/post/970951/


All Articles