Get the Olson TZ name for the local timezone?

前端 未结 11 1288
孤街浪徒
孤街浪徒 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:33

    Install pytz

    import pytz
    import time
    #import locale
    import urllib2
    
    yourOlsonTZ = None
    #yourCountryCode = locale.getdefaultlocale()[0].split('_')[1]
    yourCountryCode = urllib2.urlopen('http://api.hostip.info/country.php').read()
    
    for olsonTZ in [pytz.timezone(olsonTZ) for olsonTZ in pytz.all_timezones]:
        if (olsonTZ._tzname in time.tzname) and (str(olsonTZ) in pytz.country_timezones[yourCountryCode]):
            yourOlsonTZ = olsonTZ
            break
    
    print yourOlsonTZ
    

    This code will take a best-guess crack at your Olson Timezone based both on your Timezone Name (as according to Python's time module), and your Country Code (as according to Python's locale module the hostip.info project, which references your IP address and geolocates you accordingly).

    For example, simply matching the Timzone Names could result in America/Moncton, America/Montreal, or America/New_York for EST (GMT-5). If your country is the US, however, it will limit the answer to America/New_York.

    However, if your country is Canada, the script will simply default to the topmost Canadian result (America/Moncton). If there is a way to further refine this, please feel free to leave suggestions in comments.

    0 讨论(0)
  • 2020-11-30 03:36

    One problem is that there are multiple "pretty names" , like "Australia/Sydney" , which point to the same time zone (e.g. CST).

    So you will need to get all the possible names for the local time zone, and then select the name you like.

    e.g.: for Australia, there are 5 time zones, but way more time zone identifiers:

         "Australia/Lord_Howe", "Australia/Hobart", "Australia/Currie", 
         "Australia/Melbourne", "Australia/Sydney", "Australia/Broken_Hill", 
         "Australia/Brisbane", "Australia/Lindeman", "Australia/Adelaide", 
         "Australia/Darwin", "Australia/Perth", "Australia/Eucla"
    

    you should check if there is a library which wraps TZinfo , to handle the time zone API.

    e.g.: for Python, check the pytz library:

    http://pytz.sourceforge.net/

    and

    http://pypi.python.org/pypi/pytz/

    in Python you can do:

    from pytz import timezone
    import pytz
    
    In [56]: pytz.country_timezones('AU')
    Out[56]: 
    [u'Australia/Lord_Howe',
     u'Australia/Hobart',
     u'Australia/Currie',
     u'Australia/Melbourne',
     u'Australia/Sydney',
     u'Australia/Broken_Hill',
     u'Australia/Brisbane',
     u'Australia/Lindeman',
     u'Australia/Adelaide',
     u'Australia/Darwin',
     u'Australia/Perth',
     u'Australia/Eucla']
    

    but the API for Python seems to be pretty limited, e.g. it doesn't seem to have a call like Ruby's all_linked_zone_names -- which can find all the synonym names for a given time zone.

    0 讨论(0)
  • Here's another posibility, using PyICU instead; which is working for my purposes:

    >>> from PyICU import ICUtzinfo
    >>> from datetime import datetime
    >>> datetime(2012, 1, 1, 12, 30, 18).replace(tzinfo=ICUtzinfo.getDefault()).isoformat()
    '2012-01-01T12:30:18-05:00'
    >>> datetime(2012, 6, 1, 12, 30, 18).replace(tzinfo=ICUtzinfo.getDefault()).isoformat()
    '2012-06-01T12:30:18-04:00'
    

    Here it is interpreting niave datetimes (as would be returned by a database query) in the local timezone.

    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-30 03:45

    The tzlocal module for Python is aimed at exactly this problem. It produces consistent results under both Linux and Windows, properly converting from Windows time zone ids to Olson using the CLDR mappings.

    0 讨论(0)
提交回复
热议问题