MCPcopy Index your code
hub / github.com/python-pendulum/pendulum / _first_of_month

Method _first_of_month

src/pendulum/datetime.py:1018–1039  ·  view source on GitHub ↗

Modify to the first occurrence of a given day of the week in the current month. If no day_of_week is provided, modify to the first day of the month. Use the supplied consts to indicate the desired day_of_week, ex. DateTime.MONDAY.

(self, day_of_week: WeekDay | None = None)

Source from the content-addressed store, hash-verified

1016 return dt
1017
1018 def _first_of_month(self, day_of_week: WeekDay | None = None) -> Self:
1019 """
1020 Modify to the first occurrence of a given day of the week
1021 in the current month. If no day_of_week is provided,
1022 modify to the first day of the month. Use the supplied consts
1023 to indicate the desired day_of_week, ex. DateTime.MONDAY.
1024 """
1025 dt = self.start_of("day")
1026
1027 if day_of_week is None:
1028 return dt.set(day=1)
1029
1030 month = calendar.monthcalendar(dt.year, dt.month)
1031
1032 calendar_day = day_of_week
1033
1034 if month[0][calendar_day] > 0:
1035 day_of_month = month[0][calendar_day]
1036 else:
1037 day_of_month = month[1][calendar_day]
1038
1039 return dt.set(day=day_of_month)
1040
1041 def _last_of_month(self, day_of_week: WeekDay | None = None) -> Self:
1042 """

Callers

nothing calls this directly

Calls 2

start_ofMethod · 0.95
setMethod · 0.45

Tested by

no test coverage detected