3
votes

I have the UTC offset in my DB for the users:

+5:30 

How can I get the timezone abbreviation from this UTC offset using Python?

Such as

+5:30 => IST

Is it even possible to do this using Python?

3
There isn't a guaranteed 1:1 mapping between timezone offsets from UTC and timezone abbreviations. Some are unique; others are not. In many parts of the world, the abbreviation for a given region changes at different times of the year (winter/summer time, or standard/daylight saving time), as does the offset from UTC. It gets to be fun. - Jonathan Leffler
@JonathanLeffler Isin't there any standard abbreviations for all the regions, or these regions can be approximated to the closest available standard timezone abbreviation? - Viren Rajput
You'll have to define what you mean by 'closest available standard timezone abbreviation'. If you provide a geolocation, then there are ways to translate that to time zone names (circuitous ones, but they exist), but the abbreviation also depends on the date when you're trying for which you're trying to find the information. If you simply provide an offset from UTC with no geolocation or date reference, you're stuck with ambiguities. A timezone offset of +1:00 can be BST or CET or … depending on where you are in the world. See IANA Time Zones for more info. - Jonathan Leffler

3 Answers

6
votes

This is not possible.

  • There are many time zones that share the same offset. See this Wikipedia article for details.

  • There is no uniform standard for time zone abbreviations. There are some listed here and here, and you can see that there are duplicates in both directions.

    For example:

    • CST might be -5:00, -6:00, +8:00, +9:30 or +10:30.
    • -10:00 could be HST, HAST, TAHT, or CKT

Read also the section "Time Zone != Offset" of the timezone tag wiki here on StackOverflow.

1
votes

You can get a set (zero or more) of timezone abbreviations (as specified in the tz database) that corresponds to the given UTC offset now:

#!/usr/bin/env python
from datetime import datetime, timedelta
import pytz # $ pip install pytz

utc_offset = timedelta(hours=5, minutes=30) # +5:30
now = datetime.now(pytz.utc) # current time
print({now.astimezone(tz).tzname() 
       for tz in map(pytz.timezone, pytz.all_timezones_set)
       if now.astimezone(tz).utcoffset() == utc_offset})

Output

set(['IST'])

If you want to get abbreviations including the historical data:

#!/usr/bin/env python
from datetime import datetime, timedelta
import pytz # $ pip install pytz

utc_offset = timedelta(hours=5, minutes=30) # +5:30
abbr = set()
now = datetime.now(pytz.utc)
for tz in map(pytz.timezone, pytz.all_timezones_set):
    dt = now.astimezone(tz)    
    tzinfos = getattr(tz, '_tzinfos',
                      [(dt.utcoffset(), dt.dst(), dt.tzname())])
    abbr.update(tzname for off, _, tzname in tzinfos if off == utc_offset)
print(abbr)

Output

set(['IST'])
0
votes

As Matt stated, going from offset to timezone does not make a lot of sense.

In case you were looking to find an appropriate pytz.timezone object for a given offset:

there are timezones ranging from "Etc/GMT-14" to "Etc/GMT+12"

just take a look at pytz.all_timezones.

Using those, I was able to use faulty client input (mistaking timezone for offset) to attach a valid timezone object to my user.