Home > Net >  Get timezones with utc offset
Get timezones with utc offset

Time:02-24

is there a module or public API that given the UTC offset returns a list of all timezones in that offset ?

For example given UTC 2 it returns the following list: Africa/Blantyre Africa/Bujumbura Africa/Cairo Africa/Gaborone Africa/Harare Africa/Johannesburg Africa/Juba Africa/Khartoum Africa/Kigali Africa/Lubumbashi Africa/Lusaka Africa/Maputo Africa/Maseru Africa/Mbabane Africa/Tripoli Africa/Windhoek Asia/Amman Asia/Beirut Asia/Damascus Asia/Famagusta Asia/Gaza Asia/Hebron Asia/Jerusalem Asia/Nicosia Europe/Athens Europe/Bucharest Europe/Chisinau Europe/Helsinki Europe/Kaliningrad Europe/Kiev Europe/Mariehamn Europe/Riga Europe/Sofia Europe/Tallinn Europe/Uzhgorod Europe/Vilnius Europe/Zaporozhye

CodePudding user response:

Because of daylight savings, the result depends on the current time.

>>> for zone in sorted(zoneinfo.available_timezones()):
...     tz = zoneinfo.ZoneInfo(zone)
...     if datetime.datetime.now(tz).utcoffset() == datetime.timedelta(hours=2):
...         print(tz)
Africa/Blantyre
Africa/Bujumbura
Africa/Cairo
Africa/Gaborone
Africa/Harare
Africa/Johannesburg
Africa/Juba
Africa/Khartoum
Africa/Kigali
Africa/Lubumbashi
Africa/Lusaka
Africa/Maputo
Africa/Maseru
Africa/Mbabane
Africa/Tripoli
Africa/Windhoek
Asia/Amman
Asia/Beirut
Asia/Damascus
Asia/Famagusta
Asia/Gaza
Asia/Hebron
Asia/Jerusalem
Asia/Nicosia
Asia/Tel_Aviv
EET
Egypt
Etc/GMT-2
Europe/Athens
Europe/Bucharest
Europe/Chisinau
Europe/Helsinki
Europe/Kaliningrad
Europe/Kiev
Europe/Mariehamn
Europe/Nicosia
Europe/Riga
Europe/Sofia
Europe/Tallinn
Europe/Tiraspol
Europe/Uzhgorod
Europe/Vilnius
Europe/Zaporozhye
Israel
Libya

Python 3.9 required for stdlib zoneinfo module.

  • Related