How can I get the timezone name from a given UTC offset in Python?
For example, I have,
"GMT+0530"
And I want to get,
"Asia/Calcutta"
If there are multiple matches, the result should be a list of timezone names.
The following statement returns the time zone offset of the session time zone from UTC: The following tables illustrates the valid time zones and their offsets from UTC: In this tutorial, you have learned how to use the Oracle TZ_OFFSET () function to get the time zone offset from UTC of a time zone name.
The Universal Time Coordinated (UTC) is the time set by the World Time Standard. UTC time is the same as GMT (Greenwich Mean Time). The time difference between UTC and Local Time in minutes.
The TimeZoneInfo.GetUtcOffset (DateTime) method is similar in operation to the GetUtcOffset method of the TimeZone class. Calculates the offset or difference between the time in this time zone and Coordinated Universal Time (UTC) for a particular date and time.
If you are writing an international website, it is better to show timestamps to the users in their local timezone. Not everybody can easily translate time from UTC to their local time. Displaying time in a different timezone is confusing to the users and can lead to many misunderstandings.
There could be zero or more (multiple) timezones that correspond to a single UTC offset. To find these timezones that have a 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({tz.zone for tz in map(pytz.timezone, pytz.all_timezones_set)
if now.astimezone(tz).utcoffset() == utc_offset})
set(['Asia/Colombo', 'Asia/Calcutta', 'Asia/Kolkata'])
If you want to take historical data into account (timezones that had/will have a given utc offset at some date according to the current time zone rules):
#!/usr/bin/env python
from datetime import datetime, timedelta
import pytz # $ pip install pytz
utc_offset = timedelta(hours=5, minutes=30) # +5:30
names = 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())])
if any(off == utc_offset for off, _, _ in tzinfos):
names.add(tz.zone)
print("\n".join(sorted(names)))
Asia/Calcutta
Asia/Colombo
Asia/Dacca
Asia/Dhaka
Asia/Karachi
Asia/Kathmandu
Asia/Katmandu
Asia/Kolkata
Asia/Thimbu
Asia/Thimphu
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With