Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/pendulum/tz/local_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ def _get_windows_timezone() -> Timezone:


def _get_darwin_timezone() -> Timezone:
if tzenv := os.environ.get("TZ"):
with contextlib.suppress(ValueError):
return _tz_from_env(tzenv)
# link will be something like /usr/share/zoneinfo/America/Los_Angeles.
link = os.readlink("/etc/localtime")
tzname = link[link.rfind("zoneinfo/") + 9 :]
Expand Down
14 changes: 14 additions & 0 deletions tests/tz/test_local_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

from pendulum.tz.local_timezone import _get_unix_timezone
from pendulum.tz.local_timezone import _get_windows_timezone
from pendulum.tz.local_timezone import get_local_timezone
from pendulum.tz.timezone import Timezone


@pytest.mark.skipif(
Expand Down Expand Up @@ -50,3 +52,15 @@ def test_unix_etc_timezone_dir():
tz = _get_unix_timezone(_root=root_path)

assert tz.name == "Europe/Paris"


@pytest.mark.skipif(
sys.platform == "win32", reason="Test only available for UNIX systems"
)
def test_unix_respects_TZ_env_var(monkeypatch): # noqa: N802
monkeypatch.setattr("pendulum.tz.local_timezone._local_timezone", None)
monkeypatch.setenv("TZ", "Europe/Paris")

tz = get_local_timezone()

assert tz == Timezone("Europe/Paris")
Loading