# Buggy code
def get_local_timezone():
    """Returns the Olson identifier of the local timezone.
    In a happy world, tzlocal.get_localzone would do this, but there's a bug on OS X
    that prevents that right now: https://github.com/regebro/tzlocal/issues/6"""
    global __cached_tz
    if not __cached_tz and "darwin" in sys.platform:
        __cached_tz = (
            os.popen("systemsetup -gettimezone")
            .read()
            .replace("Time Zone: ", "")
            .strip()
        )
    elif not __cached_tz:
        __cached_tz = str(get_localzone())
    return __cached_tz


# Traceback (pytz.exceptions.UnknownTimeZoneError)
dio:~ milo$ jrnl new
Traceback (most recent call last):
File "/usr/local/share/python/jrnl", line 9, in <module>
load_entry_point('jrnl==1.5.5', 'console_scripts', 'jrnl')()
File "/usr/local/lib/python2.7/site-packages/jrnl/jrnl.py", line 156, in cli
journal = Journal.DayOne(**config)
File "/usr/local/lib/python2.7/site-packages/jrnl/Journal.py", line 303, in __init__
super(DayOne, self).__init__(**kwargs)
File "/usr/local/lib/python2.7/site-packages/jrnl/Journal.py", line 60, in __init__
self.entries = self.parse(journal_txt)
File "/usr/local/lib/python2.7/site-packages/jrnl/Journal.py", line 319, in parse
timezone = pytz.timezone(util.get_local_timezone())
File "/usr/local/lib/python2.7/site-packages/pytz/__init__.py", line 185, in timezone
raise UnknownTimeZoneError(zone)
pytz.exceptions.UnknownTimeZoneError: 'You need administrator access to run this tool... exiting!'
dio:~ milo$ id
uid=502(milo) gid=20(staff) groups=20(staff),402(com.apple.sharepoint.group.1),12(everyone),61(localaccounts)
dio:~ milo$ ls -ld "/Users/milo/Library/Mobile Documents/5U8NS4GX82~com~dayoneapp~dayone/Documents/Journal_dayone"
drwxr-xr-x  5 milo  staff  170  3 Sep 16:15 /Users/milo/Library/Mobile Documents/5U8NS4GX82~com~dayoneapp~dayone/Documents/Journal_dayone
dio:~ milo$ cat .jrnl_config
{
"default_hour": 9,
"timeformat": "%Y-%m-%d %H:%M",
"linewrap": 80,
"encrypt": false,
"editor": "",
"default_minute": 0,
"highlight": true,
"password": "",
"journals": {
"default": "/Users/milo/Library/Mobile Documents/5U8NS4GX82~com~dayoneapp~dayone/Documents/Journal_dayone"
},
"tagsymbols": "@"
}

# Fixed code
def get_local_timezone():
    """Returns the Olson identifier of the local timezone.
    In a happy world, tzlocal.get_localzone would do this, but there's a bug on OS X
    that prevents that right now: https://github.com/regebro/tzlocal/issues/6"""
    global __cached_tz
    if not __cached_tz and "darwin" in sys.platform:
        __cached_tz = (
            os.popen("systemsetup -gettimezone")
            .read()
            .replace("Time Zone: ", "")
            .strip()
        )
    if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
        link = os.readlink("/etc/localtime")
        # This is something like /usr/share/zoneinfo/America/Los_Angeles.
        # Find second / from right and take the substring
        __cached_tz = link[link.rfind("/", 0, link.rfind("/")) + 1 :]
    if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
        __cached_tz = str(get_localzone())
    if not __cached_tz or __cached_tz not in pytz.all_timezones_set:
        __cached_tz = "UTC"
    return __cached_tz