Why doesn't pytz localize () create a datetime object with tzinfo matching the tz object that localized it?

Is there anyone who can help me understand what is going on here?

import pytz from datetime import datetime tz = pytz.timezone('Europe/Berlin') print repr(tz) # <DstTzInfo 'Europe/Berlin' LMT+0:53:00 STD> dt = datetime(2011, 1, 3, 18, 40) result = tz.localize(dt) print repr(result.tzinfo) # <DstTzInfo 'Europe/Berlin' CET+1:00:00 STD> assert result.tzinfo == tz, "Why aren't these the same timezone?" 

My understanding was that the localize() method of the pytz timezone object would accept a naive datetime object and add the tzinfo property, which corresponds to the timezone object performing the localization. It does not appear to be happening in this case.

It's clear that I'm misunderstanding something about time zones or how pytz handles time zones. Can someone explain?

+6
source share
1 answer

They are the same time zone - "Europe/Berlin" .

When you print them, the output includes the abbreviation and bias that apply at that particular point in time.

If you look at tz data sources , you will see:

 # Zone NAME GMTOFF RULES FORMAT [UNTIL] Zone Europe/Berlin 0:53:28 - LMT 1893 Apr 1:00 C-Eur CE%sT 1945 May 24 2:00 1:00 SovietZone CE%sT 1946 1:00 Germany CE%sT 1980 1:00 EU CE%sT 

Thus, it would seem that when the time zone did not localize the date and time, it simply uses the first record.

It would also seem that pytz does not save an additional 28 seconds from the initial local average time deviation, but it does not matter if you do not work with dates in Berlin until April 1893.

+7
source

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


All Articles