Skip to content

Fix DateTime.is_dst() returning True for naive datetimes - #1108

Open
Yasser-Ameur wants to merge 1 commit into
python-pendulum:masterfrom
Yasser-Ameur:fix/is-dst-naive
Open

Yasser-Ameur wants to merge 1 commit into
python-pendulum:masterfrom
Yasser-Ameur:fix/is-dst-naive

Conversation

@Yasser-Ameur

Copy link
Copy Markdown

DateTime.is_dst() returns True for every naive DateTime, in January as well as July. The standard library gives None from dst() for a naive datetime, and aware values already work.

>>> pendulum.naive(2024, 1, 15, 12).is_dst()
# before: True
# now:    False

Cause

is_dst() in src/pendulum/datetime.py is self.dst() != datetime.timedelta(). For a naive datetime dst() is None, and None != timedelta() is True.

Change

It now returns bool(self.dst()). None and timedelta(0) are both falsy and a nonzero DST offset is still truthy, so aware values behave as before. One line in the library.

Tests

test_is_dst_naive in tests/datetime/test_getters.py checks a naive January and a naive July value. It fails on master with assert not True.

Run locally on Python 3.14 against the worktree sources: pytest -q tests gives 1 failed, 1868 passed, 5 skipped, 1 warning. The one failure is tests/test_main.py::test_instance_with_aware_datetime_any_tzinfo, which depends on the host timezone and fails without this change too (open PR #1072 covers it). mypy (strict, from pyproject.toml) reports no issues in 228 source files, and ruff check and ruff format --check are clean (ruff 0.14.11, the version pinned in prek.toml).

The same suite with PENDULUM_EXTENSIONS=0 gives the same result.

Not run: other Python versions and the benchmarks. I did not edit CHANGELOG.md, since recent PRs do not touch it.

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

No documentation describes the old result. This is a bug fix, so it is based on master.

is_dst() reported daylight saving time for every naive DateTime, in
January as well as July.

For a naive datetime, dst() returns None, and None != timedelta() is
True, so the comparison in is_dst() always succeeded.

Use bool(self.dst()) instead. None and timedelta(0) are both falsy, so
naive and standard-time values return False, and a nonzero DST offset
still returns True. Add a naive case to the is_dst tests.
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review December 5, 2026 01:45

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant