| 32 | |
| 33 | |
| 34 | class Date(FormattableMixin, date): |
| 35 | _MODIFIERS_VALID_UNITS: ClassVar[list[str]] = [ |
| 36 | "day", |
| 37 | "week", |
| 38 | "month", |
| 39 | "year", |
| 40 | "decade", |
| 41 | "century", |
| 42 | ] |
| 43 | |
| 44 | # Getters/Setters |
| 45 | |
| 46 | def set( |
| 47 | self, year: int | None = None, month: int | None = None, day: int | None = None |
| 48 | ) -> Self: |
| 49 | return self.replace(year=year, month=month, day=day) |
| 50 | |
| 51 | @property |
| 52 | def day_of_week(self) -> WeekDay: |
| 53 | """ |
| 54 | Returns the day of the week (0-6). |
| 55 | """ |
| 56 | return WeekDay(self.weekday()) |
| 57 | |
| 58 | @property |
| 59 | def day_of_year(self) -> int: |
| 60 | """ |
| 61 | Returns the day of the year (1-366). |
| 62 | """ |
| 63 | k = 1 if self.is_leap_year() else 2 |
| 64 | |
| 65 | return (275 * self.month) // 9 - k * ((self.month + 9) // 12) + self.day - 30 |
| 66 | |
| 67 | @property |
| 68 | def week_of_year(self) -> int: |
| 69 | return self.isocalendar()[1] |
| 70 | |
| 71 | @property |
| 72 | def days_in_month(self) -> int: |
| 73 | return calendar.monthrange(self.year, self.month)[1] |
| 74 | |
| 75 | @property |
| 76 | def week_of_month(self) -> int: |
| 77 | return math.ceil((self.day + self.first_of("month").isoweekday() - 1) / 7) |
| 78 | |
| 79 | @property |
| 80 | def age(self) -> int: |
| 81 | return self.diff(abs=False).in_years() |
| 82 | |
| 83 | @property |
| 84 | def quarter(self) -> int: |
| 85 | return math.ceil(self.month / 3) |
| 86 | |
| 87 | # String Formatting |
| 88 | |
| 89 | def to_date_string(self) -> str: |
| 90 | """ |
| 91 | Format the instance as date. |
no outgoing calls
searching dependent graphs…