Skip to content

Commit a4d98ce

Browse files
committed
local_timezone: gracefully handle retrieval errors on Darwin
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.
1 parent 3e3fec6 commit a4d98ce

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/pendulum/tz/local_timezone.py

+10-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,16 @@ def _get_windows_timezone() -> Timezone:
146146

147147

148148
def _get_darwin_timezone() -> Timezone:
149-
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
150-
link = os.readlink("/etc/localtime")
151-
tzname = link[link.rfind("zoneinfo/") + 9 :]
149+
try:
150+
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
151+
link = os.readlink("/etc/localtime")
152+
tzname = link[link.rfind("zoneinfo/") + 9 :]
153+
except Exception:
154+
warnings.warn(
155+
"Unable to find any timezone configuration, defaulting to UTC.", stacklevel=1
156+
)
157+
158+
return UTC
152159

153160
return Timezone(tzname)
154161

0 commit comments

Comments
 (0)