diff options
| author | Daniel Lemm <61800298+ffe4@users.noreply.github.com> | 2020-04-24 09:44:22 -0400 |
|---|---|---|
| committer | Paul Ganssle <paul@ganssle.io> | 2020-04-24 13:32:16 -0400 |
| commit | 1fb2ea995e8db43f7f75ea9950099f15e20de6b3 (patch) | |
| tree | 2e3f4d88947fc0672f4b525ed9f9897112c3701c | |
| parent | bd69e8e964b6572184ab6bf49b651fba617eae4f (diff) | |
| download | dateutil-git-1fb2ea995e8db43f7f75ea9950099f15e20de6b3.tar.gz | |
Added property test for tz.gettz()
The current tests only check that `tz.gettz()` and `tz.gettz("")` do
the same thing, but not that the thing that they do is equivalent to a
local time zone.
Defining what is a "local time" is a bit complicated, considering that
`tz.gettz()` may return a `tzfile` or a `tzlocal`, and neither of those
have the same behavior as Python's built-in local time support via
`.astimezone()`. This property test checks only the datetimes and
properties that are expected to be the same between the various methods
of getting a "local" time zone.
| -rw-r--r-- | dateutil/test/property/test_tz_prop.py | 35 | ||||
| -rw-r--r-- | dateutil/tz/tz.py | 2 |
2 files changed, 36 insertions, 1 deletions
diff --git a/dateutil/test/property/test_tz_prop.py b/dateutil/test/property/test_tz_prop.py new file mode 100644 index 0000000..ec6d271 --- /dev/null +++ b/dateutil/test/property/test_tz_prop.py @@ -0,0 +1,35 @@ +from datetime import datetime, timedelta + +import pytest +import six +from hypothesis import assume, given +from hypothesis import strategies as st + +from dateutil import tz as tz + +EPOCHALYPSE = datetime.fromtimestamp(2147483647) +NEGATIVE_EPOCHALYPSE = datetime.fromtimestamp(0) - timedelta(seconds=2147483648) + + +@pytest.mark.gettz +@pytest.mark.parametrize("gettz_arg", [None, ""]) +# TODO: Remove bounds when GH #590 is resolved +@given( + dt=st.datetimes( + min_value=NEGATIVE_EPOCHALYPSE, max_value=EPOCHALYPSE, timezones=st.just(tz.UTC), + ) +) +def test_gettz_returns_local(gettz_arg, dt): + act_tz = tz.gettz(gettz_arg) + if isinstance(act_tz, tz.tzlocal): + return + + dt_act = dt.astimezone(tz.gettz(gettz_arg)) + if six.PY2: + dt_exp = dt.astimezone(tz.tzlocal()) + else: + dt_exp = dt.astimezone() + + assert dt_act == dt_exp + assert dt_act.tzname() == dt_exp.tzname() + assert dt_act.utcoffset() == dt_exp.utcoffset() diff --git a/dateutil/tz/tz.py b/dateutil/tz/tz.py index 320708d..c67f56d 100644 --- a/dateutil/tz/tz.py +++ b/dateutil/tz/tz.py @@ -1596,7 +1596,7 @@ def __get_gettz(): name = os.environ["TZ"] except KeyError: pass - if not name or name == ":": + if name is None or name in ("", ":"): for filepath in TZFILES: if not os.path.isabs(filepath): filename = filepath |
