I am migrating away from pytz
(the fastest footgun in the west), to ZoneInfo
.
I am doing a test with timezone Africa/Johannesburg
, which is GMT+2 (Note: GMT = UTC). I expect the following to yield the same result, however the GMT+2 one is incorrect:
Since I am using python 3.8 I have to use the backports version of zoneinfo
.
>> from backports import zoneinfo
>>> jhb_tz = zoneinfo.ZoneInfo("Africa/Johannesburg")
>>> gmt2_tz = zoneinfo.ZoneInfo("Etc/GMT+2")
>>> from datetime import datetime, timezone
>>> UTC = timezone.UTC
>>> now = datetime.now(UTC)
>>> now
datetime.datetime(2022, 1, 1, 14, 50, 3, 305445, tzinfo=datetime.timezone.utc)
>>> now.astimezone(jhb_tz) # Is correct, i.e. UTC+2
datetime.datetime(2022, 1, 1, 16, 50, 3, 305445, tzinfo=backports.zoneinfo.ZoneInfo(key='Africa/Johannesburg'))
>>> now.astimezone(gmt2_tz) # Incorrect, i.e. UTC-2
datetime.datetime(2022, 1, 1, 12, 50, 3, 305445, tzinfo=backports.zoneinfo.ZoneInfo(key='Etc/GMT+2'))
The GMT+2 is actually GMT-2 instead of GMT+2, why is that?