Skip to content

Commit b927a70

Browse files
committed
Reuse private calendars for weekday modifiers
1 parent 26551b0 commit b927a70

3 files changed

Lines changed: 24 additions & 4 deletions

File tree

‎src/pendulum/date.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,10 @@
3131
from typing_extensions import SupportsIndex
3232

3333

34+
# Weekday modifiers always use Monday-based indices.
35+
_MONTH_CALENDAR = calendar.Calendar(firstweekday=calendar.MONDAY)
36+
37+
3438
class Date(FormattableMixin, date):
3539
_MODIFIERS_VALID_UNITS: ClassVar[list[str]] = [
3640
"day",
@@ -575,7 +579,7 @@ def _first_of_month(self, day_of_week: WeekDay) -> Self:
575579
if day_of_week is None:
576580
return dt.set(day=1)
577581

578-
month = calendar.Calendar().monthdayscalendar(dt.year, dt.month)
582+
month = _MONTH_CALENDAR.monthdayscalendar(dt.year, dt.month)
579583

580584
calendar_day = day_of_week
581585

@@ -600,7 +604,7 @@ def _last_of_month(self, day_of_week: WeekDay | None = None) -> Self:
600604
if day_of_week is None:
601605
return dt.set(day=self.days_in_month)
602606

603-
month = calendar.Calendar().monthdayscalendar(dt.year, dt.month)
607+
month = _MONTH_CALENDAR.monthdayscalendar(dt.year, dt.month)
604608

605609
calendar_day = day_of_week
606610

‎src/pendulum/datetime.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@
4646
from typing_extensions import SupportsIndex
4747

4848

49+
# Weekday modifiers always use Monday-based indices.
50+
_MONTH_CALENDAR = calendar.Calendar(firstweekday=calendar.MONDAY)
51+
52+
4953
class DateTime(datetime.datetime, Date):
5054
EPOCH: ClassVar[DateTime]
5155
min: ClassVar[DateTime]
@@ -1026,7 +1030,7 @@ def _first_of_month(self, day_of_week: WeekDay | None = None) -> Self:
10261030
if day_of_week is None:
10271031
return dt.set(day=1)
10281032

1029-
month = calendar.Calendar().monthdayscalendar(dt.year, dt.month)
1033+
month = _MONTH_CALENDAR.monthdayscalendar(dt.year, dt.month)
10301034

10311035
calendar_day = day_of_week
10321036

@@ -1049,7 +1053,7 @@ def _last_of_month(self, day_of_week: WeekDay | None = None) -> Self:
10491053
if day_of_week is None:
10501054
return dt.set(day=self.days_in_month)
10511055

1052-
month = calendar.Calendar().monthdayscalendar(dt.year, dt.month)
1056+
month = _MONTH_CALENDAR.monthdayscalendar(dt.year, dt.month)
10531057

10541058
calendar_day = day_of_week
10551059

‎tests/date/test_day_of_week_modifiers.py‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,18 @@ def test_weekday_modifiers_ignore_calendar_firstweekday(first_weekday, date_type
7373
calendar.setfirstweekday(original_first_weekday)
7474

7575

76+
@pytest.mark.parametrize("date_type", [pendulum.Date, pendulum.DateTime])
77+
def test_weekday_modifiers_reuse_calendar(date_type, monkeypatch):
78+
def unexpected_calendar(*args, **kwargs):
79+
raise AssertionError("Weekday modifiers should reuse their calendar")
80+
81+
monkeypatch.setattr(calendar, "Calendar", unexpected_calendar)
82+
instance = date_type(2026, 10, 15)
83+
assert_date(instance.first_of("month", pendulum.MONDAY), 2026, 10, 5)
84+
assert_date(instance.last_of("month", pendulum.MONDAY), 2026, 10, 26)
85+
assert_date(instance.nth_of("month", 2, pendulum.MONDAY), 2026, 10, 12)
86+
87+
7688
def test_start_of_week():
7789
d = pendulum.date(1980, 8, 7).start_of("week")
7890
assert_date(d, 1980, 8, 4)

0 commit comments

Comments
 (0)