Skip to content

Commit ba57bed

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 ba57bed

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/pendulum/tz/local_timezone.py

+11-3
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,17 @@ 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.",
156+
stacklevel=1,
157+
)
158+
159+
return UTC
152160

153161
return Timezone(tzname)
154162

0 commit comments

Comments
 (0)