Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.zone is different than c.zone

Is there any way to see that b is in reality equal to 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. 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....)

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

like image 515
Mannaggia Avatar asked Jun 17 '14 14:06

Mannaggia


People also ask

How does Python compare month and year?

But correct version would be: if lastMonthAllowed. year > lastUserUploadMonth. year or (lastMonthAllowed. year == lastUserUploadMonth.

How does Python handle time zones?

The parameter pytz. timezone allows us to specify the timezone information as a string. We can pass in any available timezone and will get the current date time of that timezone, and it will also print the offset with respect to the UTC. i.e., the difference between UTC timezone(+00:00) and the specified time zone.


1 Answers

They do not represent the same thing.

  • "CET" is 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:

  • The timezone tag wiki - specifically, the section "Time Zone != Offset"
  • The dst tag wiki - covering daylight saving time.

With regards to the edit, Europe/Rome is a distinct time zone. It is not the same as Europe/Berlin, nor Europe/Zurich, nor Europe/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. Rome and Berlin since 1980, Zurich since 1981, and Amsterdam since 1977. Before those dates, they differed significantly. 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. 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". For example, you can see here that Europe/Vatican and Europe/San_Marino are both linked to Europe/Rome, and are therefore equivalent.

like image 171
Matt Johnson-Pint Avatar answered Oct 12 '22 17:10

Matt Johnson-Pint