Modify to the last occurrence of a given day of the week in the current month. If no day_of_week is provided, modify to the last 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)
| 1039 | return dt.set(day=day_of_month) |
| 1040 | |
| 1041 | def _last_of_month(self, day_of_week: WeekDay | None = None) -> Self: |
| 1042 | """ |
| 1043 | Modify to the last occurrence of a given day of the week |
| 1044 | in the current month. If no day_of_week is provided, |
| 1045 | modify to the last day of the month. Use the supplied consts |
| 1046 | to indicate the desired day_of_week, ex. DateTime.MONDAY. |
| 1047 | """ |
| 1048 | dt = self.start_of("day") |
| 1049 | |
| 1050 | if day_of_week is None: |
| 1051 | return dt.set(day=self.days_in_month) |
| 1052 | |
| 1053 | month = calendar.monthcalendar(dt.year, dt.month) |
| 1054 | |
| 1055 | calendar_day = day_of_week |
| 1056 | |
| 1057 | if month[-1][calendar_day] > 0: |
| 1058 | day_of_month = month[-1][calendar_day] |
| 1059 | else: |
| 1060 | day_of_month = month[-2][calendar_day] |
| 1061 | |
| 1062 | return dt.set(day=day_of_month) |
| 1063 | |
| 1064 | def _nth_of_month( |
| 1065 | self, nth: int, day_of_week: WeekDay | None = None |