from __future__ import with_statement
import os
import dateutil.tz

_cache_tz = None


def _get_localzone():
    tzname = os.popen("systemsetup -gettimezone").read().replace("Time Zone: ", "").strip()
    if not tzname:
        # link will be something like /usr/share/zoneinfo/America/Los_Angeles.
        link = os.readlink("/etc/localtime")
        tzname = link[link.rfind('/', 0, link.rfind('/')) + 1:]
    try:
        dateutil.tz.gettz(tzname)
        return tzname
    except:
        return None


def get_localzone():
    """Get the computers configured local timezone, if any."""
    global _cache_tz
    if _cache_tz is None:
        _cache_tz = _get_localzone()
    return _cache_tz


def reload_localzone():
    """Reload the cached localzone. You need to call this if the timezone has changed."""
    global _cache_tz
    _cache_tz = _get_localzone()
    return _cache_tz

@@ -59,16 +59,34 @@
 eutil.tz
+%0Aimport subprocess
 %0A%0A_cache
@@ -137,17 +137,33 @@
 e = 
-os.popen(
+subprocess.check_output(%5B
 %22sys
@@ -170,17 +170,20 @@
 temsetup
- 
+%22, %22
 -gettime
@@ -191,51 +191,167 @@
 one%22
+%5D
 ).
-read().replace(%22Time Zone: %22, %22%22).strip()
+decode('utf-8')%0A    tzname = tzname.replace(%22Time Zone: %22, %22%22)%0A    # OS X 10.9+, this command is root-only%0A    if 'exiting!' in tzname:%0A        tzname = ''%0A
 %0A   
@@ -563,11 +563,85 @@
    t
-ry:
+zname = tzname.strip()%0A    try:%0A        # test the name%0A        assert tzname
 %0A   

fix #1483 -- fix OS X time zone detection