Skip to content

Commit

Permalink
Fix tz.gettz() behavior with empty string
Browse files Browse the repository at this point in the history
The documented behavior of the function is to return a local time zone
when the argument is None or an empty string.

This was working if the TZ environment variable was set, but not working
otherwise.

Fixes dateutil#925, dateutil#926
  • Loading branch information
ffe4 authored and pganssle committed Apr 24, 2020
1 parent 4a50ff7 commit bd69e8e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
2 changes: 2 additions & 0 deletions changelog.d/1024.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fixed tz.gettz() not returning local time when passed an empty string.
Reported by @labrys (gh issues #925, #926). Fixed by @ffe4 (gh pr #1024)
9 changes: 9 additions & 0 deletions dateutil/test/test_tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1079,6 +1079,15 @@ def testGettzCacheTzLocal(self):
assert local1 is not local2


@pytest.mark.gettz
def test_gettz_same_result_for_none_and_empty_string():
local_from_none = tz.gettz()
local_from_empty_string = tz.gettz("")
assert local_from_none is not None
assert local_from_empty_string is not None
assert local_from_none == local_from_empty_string


@pytest.mark.gettz
@pytest.mark.parametrize('badzone', [
'Fake.Region/Abcdefghijklmnop', # Violates several tz project name rules
Expand Down
2 changes: 1 addition & 1 deletion dateutil/tz/tz.py
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,7 @@ def nocache(name=None):
name = os.environ["TZ"]
except KeyError:
pass
if name is None or name == ":":
if not name or name == ":":
for filepath in TZFILES:
if not os.path.isabs(filepath):
filename = filepath
Expand Down

0 comments on commit bd69e8e

Please sign in to comment.