Get the Olson TZ name for the local timezone?

前端 未结 11 1297
孤街浪徒
孤街浪徒 2020-11-30 03:19

How do I get the Olson timezone name (such as Australia/Sydney) corresponding to the value given by C\'s localtime call?

This is the value overridden vi

11条回答
  •  失恋的感觉
    2020-11-30 03:39

    I prefer following a slightly better than poking around _xxx values

    import time, pytz, os
    
    cur_name=time.tzname
    cur_TZ=os.environ.get("TZ")
    
    def is_current(name):
       os.environ["TZ"]=name
       time.tzset()
       return time.tzname==cur_name
    
    print "Possible choices:", filter(is_current, pytz.all_timezones)
    
    # optional tz restore
    if cur_TZ is None: del os.environ["TZ"]
    else: os.environ["TZ"]=cur_TZ
    time.tzset()
    

提交回复
热议问题