How to get timezone rules version used by datetime?

In John Skeet's blog post about handling timezone information when storing future datetimes, he suggests storing the version of timezone rules in the database along with the local time and timezone id.

His example:

ID: 1
Name: KindConf
LocalStart: 2022-07-10T09:00:00
Address: Europaplein 24, 1078 GZ Amsterdam, Netherlands
TimeZoneId: Europe/Amsterdam
UtcStart: 2022-07-10T07:00:00Z
TimeZoneRules: 2019a

In python, how do you get the version of timezone rules used by datetime? For example:

date.astimezone(zoneinfo.ZoneInfo('US/Pacific'))

The method of getting timezone rules depends on the library used to create the tzinfo instance.

If using pytz, the timezone database used is the Olson timezone database.

import pytz
print(pytz.OLSEN_VERSION)  # e.g. 2021a

When using zoneinfo, the system's timezone data is used by default.

From the docs: By default, zoneinfo uses the system’s time zone data if available; if no system time zone data is available, the library will fall back to using the first-party tzdata package available on PyPI.

There are two options:

  1. Find a platform specific method of getting the version number
  2. Setup zoneinfo with the tzdata library, which makes the timezone version number readily available.

For option 2:

To setup zoneinfo to ignore the system data and use the tzdata package instead, set the environment variable PYTHONTZPATH=""

The IANA version number is exported by tzdata:

import tzdata
print(tzdata.IANA_VERSION). # e.g. 2021e