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

Method _nth_of_year

src/pendulum/datetime.py:1150–1169  ·  view source on GitHub ↗

Modify to the given occurrence of a given day of the week in the current year. If the calculated occurrence is outside, the scope of the current year, then return False and no modifications are made. Use the supplied consts to indicate the desired day_of_week

(self, nth: int, day_of_week: WeekDay | None = None)

Source from the content-addressed store, hash-verified

1148 return self.set(month=MONTHS_PER_YEAR).last_of("month", day_of_week)
1149
1150 def _nth_of_year(self, nth: int, day_of_week: WeekDay | None = None) -> Self | None:
1151 """
1152 Modify to the given occurrence of a given day of the week
1153 in the current year. If the calculated occurrence is outside,
1154 the scope of the current year, then return False and no
1155 modifications are made. Use the supplied consts
1156 to indicate the desired day_of_week, ex. DateTime.MONDAY.
1157 """
1158 if nth == 1:
1159 return self.first_of("year", day_of_week)
1160
1161 dt = self.first_of("year")
1162 year = dt.year
1163 for _ in range(nth - (1 if dt.day_of_week == day_of_week else 0)):
1164 dt = dt.next(day_of_week)
1165
1166 if year != dt.year:
1167 return None
1168
1169 return self.on(self.year, dt.month, dt.day).start_of("day")
1170
1171 def average( # type: ignore[override]
1172 self, dt: datetime.datetime | None = None

Callers

nothing calls this directly

Calls 4

first_ofMethod · 0.95
onMethod · 0.95
nextMethod · 0.45
start_ofMethod · 0.45

Tested by

no test coverage detected