How to use abbreviated timezone name(PST, IST) in

2019-07-28 01:43发布

问题:

I need to know the way of using Abbreviated timezone name like PST, IST etc on pytz.

Now I can be able to convert between timezone using the timezone name like "America/Los_Angeles".

Instead I need to find the way of using timezone name like PST, IST etc.,

Sample code I have used now for conversion.

def local2utc(self, dt):

    from_zone = tz.gettz('America/Los_Angeles')
    to_zone = tz.gettz('UTC')
    local = dt.replace(tzinfo=from_zone)
    print("converted time")
    print(local.astimezone(to_zone).replace(tzinfo = None))
    return local.astimezone(to_zone).replace(tzinfo = None)

Someone let me know the way of achieving the same.

回答1:

This is by nature impossible, because some abbreviations can mean multiple (different) time zones:

http://www.timeanddate.com/time/zones/

If you want it to work specifically for your timezones, make a dictionary mapping abbreviation to long name.