Skip to content

Adding try/except workaround when Pendulum tries to treat environment variable local timezone as a filepath #468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
6 changes: 5 additions & 1 deletion pendulum/tz/local_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,11 @@ def _tz_from_env(tzenv): # type: (str) -> Timezone

# TZ specifies a file
if os.path.exists(tzenv):
return TimezoneFile(tzenv)
try:
return TimezoneFile(tzenv)
except FileNotFoundError:
# if cannot load from timezone file, assume path existing is coincidence
pass

# TZ specifies a zoneinfo zone.
try:
Expand Down
10 changes: 10 additions & 0 deletions tests/tz/test_local_timezone.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@
from pendulum.tz.local_timezone import _get_windows_timezone


@pytest.mark.skipif(
sys.platform == "win32", reason="Test only available for UNIX systems"
)
def test_unix_environment_variable(monkeypatch):
# localtime can be set on unix with TZ environment variable
monkeypatch.setenv("TZ", "UTC")
tz = _get_unix_timezone()
assert tz.name == "UTC"


@pytest.mark.skipif(
sys.platform == "win32", reason="Test only available for UNIX systems"
)
Expand Down