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

Method previous

src/pendulum/datetime.py:943–964  ·  view source on GitHub ↗

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

(
        self, day_of_week: WeekDay | None = None, keep_time: bool = False
    )

Source from the content-addressed store, hash-verified

941 return dt
942
943 def previous(
944 self, day_of_week: WeekDay | None = None, keep_time: bool = False
945 ) -> Self:
946 """
947 Modify to the previous occurrence of a given day of the week.
948 If no day_of_week is provided, modify to the previous occurrence
949 of the current day of the week. Use the supplied consts
950 to indicate the desired day_of_week, ex. DateTime.MONDAY.
951 """
952 if day_of_week is None:
953 day_of_week = self.day_of_week
954
955 if day_of_week < WeekDay.MONDAY or day_of_week > WeekDay.SUNDAY:
956 raise ValueError("Invalid day of week")
957
958 dt = self if keep_time else self.start_of("day")
959
960 dt = dt.subtract(days=1)
961 while dt.day_of_week != day_of_week:
962 dt = dt.subtract(days=1)
963
964 return dt
965
966 def first_of(self, unit: str, day_of_week: WeekDay | None = None) -> Self:
967 """

Callers 1

_start_of_weekMethod · 0.95

Calls 2

start_ofMethod · 0.95
subtractMethod · 0.45

Tested by

no test coverage detected