Sakib Rahman
Sakib Rahman

Reputation: 376

Django Pytz timezone full names with GMT value

pytz provides a list of timezones in formats like America/Chicago, America/Los_Angeles, Asia/Kolkata or the tz abbreviation.

I want the full name for timezones GMT value like the following

(GMT-11:00) Pacific/Pago_Pago

(GMT-07:00) America/Santa_Isabel

Upvotes: 0

Views: 260

Answers (1)

Sakib Rahman
Sakib Rahman

Reputation: 376

Here I got the solution.

for timezone in pytz.common_timezones:
    tzone = pytz.timezone(timezone)
    std_date = None

    try:
        for utcdate, info in zip(tzone._utc_transition_times, tzone._transition_info):
        utcoffset, dstoffset, tzname = info
        if dstoffset == ZERO:
            std_date = utcdate
        if utcdate > NOW:
            break
    except AttributeError:
        std_date = NOW

    std_date = tzone.localize(std_date)  
    offset = std_date.strftime('%z')
    timezone_offset = "(GMT{0}:{1}){2}".format(offset[:-2],offset[-2:],timezone)

Upvotes: 1

Related Questions