Skip to content

Commit 267591b

Browse files
committed
Fix TZ env variable not being respected on MacOS
1 parent 02f87ce commit 267591b

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed

src/pendulum/tz/local_timezone.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ def _get_windows_timezone() -> Timezone:
150150

151151

152152
def _get_darwin_timezone() -> Timezone:
153+
if tzenv := os.environ.get("TZ"):
154+
with contextlib.suppress(ValueError):
155+
return _tz_from_env(tzenv)
153156
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
154157
link = os.readlink("/etc/localtime")
155158
tzname = link[link.rfind("zoneinfo/") + 9 :]

tests/tz/test_local_timezone.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77

88
from pendulum.tz.local_timezone import _get_unix_timezone
99
from pendulum.tz.local_timezone import _get_windows_timezone
10+
from pendulum.tz.local_timezone import get_local_timezone
11+
from pendulum.tz.timezone import Timezone
1012

1113

1214
@pytest.mark.skipif(
@@ -50,3 +52,15 @@ def test_unix_etc_timezone_dir():
5052
tz = _get_unix_timezone(_root=root_path)
5153

5254
assert tz.name == "Europe/Paris"
55+
56+
57+
@pytest.mark.skipif(
58+
sys.platform == "win32", reason="Test only available for UNIX systems"
59+
)
60+
def test_unix_respects_TZ_env_var(monkeypatch): # noqa: N802
61+
monkeypatch.setattr("pendulum.tz.local_timezone._local_timezone", None)
62+
monkeypatch.setenv("TZ", "Europe/Paris")
63+
64+
tz = get_local_timezone()
65+
66+
assert tz == Timezone("Europe/Paris")

0 commit comments

Comments
 (0)