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 9, 2024
1 parent 3e3fec6 commit ba57bed
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions src/pendulum/tz/local_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,17 @@ 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 ba57bed

Please sign in to comment.