Get the Olson TZ name for the local timezone?

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

提交回复
热议问题