From b07802b89d7557c217452927779bbab7a2a98c11 Mon Sep 17 00:00:00 2001 From: Yasser-Ameur Date: Thu, 26 Nov 2026 11:06:08 +0100 Subject: [PATCH] Fix Date.is_same_day() returning False for a datetime on the same day 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. --- src/pendulum/date.py | 2 +- tests/date/test_comparison.py | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/pendulum/date.py b/src/pendulum/date.py index cd703d2ed..bcecb4785 100644 --- a/src/pendulum/date.py +++ b/src/pendulum/date.py @@ -165,7 +165,7 @@ def is_same_day(self, dt: date) -> bool: """ Checks if the passed in date is the same day as the instance current day. """ - return self == dt + return (self.year, self.month, self.day) == (dt.year, dt.month, dt.day) def is_anniversary(self, dt: date | None = None) -> bool: """ diff --git a/tests/date/test_comparison.py b/tests/date/test_comparison.py index 9fd999bb5..fe2420491 100644 --- a/tests/date/test_comparison.py +++ b/tests/date/test_comparison.py @@ -1,6 +1,7 @@ from __future__ import annotations from datetime import date +from datetime import datetime import pendulum @@ -240,6 +241,16 @@ def test_is_same_day(): assert not dt1.is_same_day(dt5) +def test_is_same_day_with_datetime(): + d = pendulum.Date(2015, 5, 28) + + assert d.is_same_day(datetime(2015, 5, 28, 10)) + assert d.is_same_day(pendulum.datetime(2015, 5, 28, 10)) + assert d.is_same_day(pendulum.datetime(2015, 5, 28, 0, tz="Europe/Paris")) + assert not d.is_same_day(datetime(2015, 5, 29, 10)) + assert not d.is_same_day(pendulum.datetime(2015, 5, 29, 10)) + + def test_comparison_to_unsupported(): dt1 = pendulum.Date.today()