Skip to content

Fix Date.is_same_day() returning False for a datetime on the same day - #1110

Open
Yasser-Ameur wants to merge 1 commit into
python-pendulum:masterfrom
Yasser-Ameur:fix/date-is-same-day-datetime
Open

Yasser-Ameur wants to merge 1 commit into
python-pendulum:masterfrom
Yasser-Ameur:fix/date-is-same-day-datetime

Conversation

@Yasser-Ameur

Copy link
Copy Markdown

Date.is_same_day() returns False when it is given a datetime or DateTime on the same day. The argument is annotated as date, and DateTime.is_same_day() and Date.is_anniversary() already accept datetimes.

>>> pendulum.Date(2015, 5, 28).is_same_day(datetime.datetime(2015, 5, 28, 10))
# before: False
# now:    True

Cause

is_same_day() in src/pendulum/date.py is return self == dt, and a datetime never compares equal to a date.

Change

It compares (year, month, day) of both sides, which is the rule DateTime.is_same_day() follows. One line in the library. For an aware argument it uses the wall clock fields it carries and does no timezone conversion. Converting an aware DateTime to the date's calendar first would be a different rule, and I left that open.

Tests

test_is_same_day_with_datetime in tests/date/test_comparison.py: a stdlib datetime, a pendulum.datetime and an aware Paris midnight on the same day give True, and the next day gives False for both kinds. It fails on master with assert False.

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) reports no issues in 228 source files, and ruff check and ruff format --check are clean (ruff 0.14.11).

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

Not run: other Python versions. 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.

Date(2015, 5, 28).is_same_day(datetime(2015, 5, 28, 10)) returned
False, although the argument is annotated as date and DateTime
already compares calendar days in is_same_day().

Date.is_same_day() used self == dt, and a datetime never compares equal
to a date.

Compare (year, month, day) of both sides, as is_anniversary() does by
normalising its argument. Add datetime and DateTime cases to the tests.
@Yasser-Ameur
Yasser-Ameur marked this pull request as ready for review December 5, 2026 03:56

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