2

Given a time zone abbreviation, is there a way to get a list of possible time zones?

Like IST could mean one of the following:

  • Asia/Kolkata (India)
  • Asia/Tel_Aviv (Israel)
  • Europe/Dublin (Ireland)

What I was looking for was a way to get ['Asia/Kolkata', 'Asia/Tel_Aviv', 'Europe/Dublin'] as output when 'IST' is given as input.

I was hoping that there would a way using the standard modules itself. Can it be done with the new zoneinfo module?

Being inspired by this answer, I did

from datetime import datetime as dt                                                                                                      
import zoneinfo                                                                                                                 
s = zoneinfo.available_timezones()                                                                                                              
d = dict()                                                                                                                               
for tz in s:                                                                                                                             
    t = z.ZoneInfo(tz).tzname(dt.utcnow())                                                                                                                                                                                                                          
    if t not in d:                                                                                                                       
        d[t] = [tz]                                                                                                                      
    else:                                                                                                                                
        d[t].append(tz)                                                                                                                  

But this is giving 'PDT' as

 'PDT': ['America/Ensenada',                                                                                                             
         'America/Santa_Isabel',                                                                                                         
         'Mexico/BajaNorte',                                                                                                             
         'US/Pacific-New',                                                                                                               
         'US/Pacific',                                                                                                                   
         'America/Vancouver',                                                                                                            
         'PST8PDT',                                                                                                                      
         'America/Tijuana',                                                                                                              
         'America/Los_Angeles',

and 'PST' as just

 'PST': ['Asia/Manila'], 

But wouldn't 'America/Los_Angeles' also come under 'PST' at some point (not right now but later on).

I'm obviously missing something, but couldn't understand what it is..

                                                                                                            Can there be a way to get the list of possible timezones from a time zone abbreviation?
J...S
  • 5,079
  • 1
  • 20
  • 35
  • for a given geographic time zone, different UTC offsets might apply over the course of the years due to DST being active or inactive. Therefore, using `datetime.now()` (no need to use [utcnow](https://blog.ganssle.io/articles/2019/11/utcnow.html) btw.) gives you only part of the full picture. – FObersteiner Jun 21 '21 at 13:01

1 Answers1

1

extending the linked answer, in a kind-of-naive approach, you can generate your lookup table for a specific year like

from collections import defaultdict
from datetime import datetime, timedelta
import zoneinfo

year = 2021
checkdays = [datetime(year, 1, 1) + timedelta(i) for i in range(0, 365, 5)]

abbreviationmap = defaultdict(set)

for d in checkdays:
    for z in zoneinfo.available_timezones():
        abbreviationmap[zoneinfo.ZoneInfo(z).tzname(d)].add(z)
        
print(sorted(abbreviationmap['PDT']))
# ['America/Ensenada', 'America/Los_Angeles', 'America/Santa_Isabel', 
#  'America/Tijuana', 'America/Vancouver', 'Canada/Pacific', 'Mexico/BajaNorte', 
#  'PST8PDT', 'US/Pacific']
FObersteiner
  • 22,500
  • 8
  • 42
  • 72