Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting timezone name from UTC offset

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.

like image 623
Hegde Avin Avatar asked Jan 29 '16 13:01

Hegde Avin


People also ask

How to get time zone offset from UTC in Oracle?

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.

What is UTC (Universal Time Coordinated)?

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.

What is timezoneinfo getutcoffset in Java?

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.

Is it better to display time in UTC or local timezone?

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.


1 Answers

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})

Output

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)))

Output

Asia/Calcutta
Asia/Colombo
Asia/Dacca
Asia/Dhaka
Asia/Karachi
Asia/Kathmandu
Asia/Katmandu
Asia/Kolkata
Asia/Thimbu
Asia/Thimphu
like image 83
jfs Avatar answered Oct 03 '22 08:10

jfs