Skip to content

Commit

Permalink
local_timezone: gracefully handle retrieval errors on Darwin
Browse files Browse the repository at this point in the history
In some sandbox environments, the program may not have
permission to access /etc/localtime or it may not exist.

Catch any exceptions accessing and parsing its value and return UTC by
default to avoid crashing the program.
  • Loading branch information
tpwrules committed Jan 6, 2024
1 parent 3e3fec6 commit a4d98ce
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/pendulum/tz/local_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,16 @@ def _get_windows_timezone() -> Timezone:


def _get_darwin_timezone() -> Timezone:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9 :]
try:
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9 :]
except Exception:
warnings.warn(
"Unable to find any timezone configuration, defaulting to UTC.", stacklevel=1
)

return UTC

return Timezone(tzname)

Expand Down

0 comments on commit a4d98ce

Please sign in to comment.