0

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?

FObersteiner
  • 22,500
  • 8
  • 42
  • 72
run_the_race
  • 1,344
  • 2
  • 36
  • 62
  • 2
    Wikipedia is probably not the best source but in this case has some relevant information `The special area of "Etc" is used for some administrative zones, particularly for "Etc/UTC" which represents Coordinated Universal Time. In order to conform with the POSIX style, those zone names beginning with "Etc/GMT" have their sign reversed from the standard ISO 8601 convention. In the "Etc" area, zones west of GMT have a positive sign and those east have a negative sign in their name (e.g "Etc/GMT-14" is 14 hours ahead of GMT).` https://en.wikipedia.org/wiki/Tz_database#Area – Iain Shelvington Jan 01 '22 at 15:04
  • Wow good find, I was searching the lib to try to find an answer, never thought outside sites would help, thank you! How absurd and unintuitive. – run_the_race Jan 01 '22 at 15:09
  • Does this answer your question? [Time zones \`Etc/GMT\`, why it is other way round?](https://stackoverflow.com/questions/53076575/time-zones-etc-gmt-why-it-is-other-way-round) – FObersteiner Jan 01 '22 at 15:42
  • @IainShelvington Could you make an answer to this that basically just has the same info as your comment, that way it can be marked as solved? I'd do it myself, but I really don't know if it's bad etiquette to submit someone else's comment as an answer. – jpobst Aug 07 '22 at 12:54

0 Answers0