date.py 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760
  1. # The following is only needed because of Python 3.7
  2. # mypy: no-warn-unused-ignores
  3. from __future__ import annotations
  4. import calendar
  5. import math
  6. from datetime import date
  7. from datetime import datetime
  8. from datetime import timedelta
  9. from typing import TYPE_CHECKING
  10. from typing import ClassVar
  11. from typing import NoReturn
  12. from typing import cast
  13. from typing import overload
  14. import pendulum
  15. from pendulum.constants import MONTHS_PER_YEAR
  16. from pendulum.constants import YEARS_PER_CENTURY
  17. from pendulum.constants import YEARS_PER_DECADE
  18. from pendulum.day import WeekDay
  19. from pendulum.exceptions import PendulumException
  20. from pendulum.helpers import add_duration
  21. from pendulum.interval import Interval
  22. from pendulum.mixins.default import FormattableMixin
  23. if TYPE_CHECKING:
  24. from typing_extensions import Self
  25. from typing_extensions import SupportsIndex
  26. class Date(FormattableMixin, date):
  27. _MODIFIERS_VALID_UNITS: ClassVar[list[str]] = [
  28. "day",
  29. "week",
  30. "month",
  31. "year",
  32. "decade",
  33. "century",
  34. ]
  35. # Getters/Setters
  36. def set(
  37. self, year: int | None = None, month: int | None = None, day: int | None = None
  38. ) -> Self:
  39. return self.replace(year=year, month=month, day=day)
  40. @property
  41. def day_of_week(self) -> WeekDay:
  42. """
  43. Returns the day of the week (0-6).
  44. """
  45. return WeekDay(self.weekday())
  46. @property
  47. def day_of_year(self) -> int:
  48. """
  49. Returns the day of the year (1-366).
  50. """
  51. k = 1 if self.is_leap_year() else 2
  52. return (275 * self.month) // 9 - k * ((self.month + 9) // 12) + self.day - 30
  53. @property
  54. def week_of_year(self) -> int:
  55. return self.isocalendar()[1]
  56. @property
  57. def days_in_month(self) -> int:
  58. return calendar.monthrange(self.year, self.month)[1]
  59. @property
  60. def week_of_month(self) -> int:
  61. return math.ceil((self.day + self.first_of("month").isoweekday() - 1) / 7)
  62. @property
  63. def age(self) -> int:
  64. return self.diff(abs=False).in_years()
  65. @property
  66. def quarter(self) -> int:
  67. return math.ceil(self.month / 3)
  68. # String Formatting
  69. def to_date_string(self) -> str:
  70. """
  71. Format the instance as date.
  72. :rtype: str
  73. """
  74. return self.strftime("%Y-%m-%d")
  75. def to_formatted_date_string(self) -> str:
  76. """
  77. Format the instance as a readable date.
  78. :rtype: str
  79. """
  80. return self.strftime("%b %d, %Y")
  81. def __repr__(self) -> str:
  82. return f"{self.__class__.__name__}({self.year}, {self.month}, {self.day})"
  83. # COMPARISONS
  84. def closest(self, dt1: date, dt2: date) -> Self:
  85. """
  86. Get the closest date from the instance.
  87. """
  88. dt1 = self.__class__(dt1.year, dt1.month, dt1.day)
  89. dt2 = self.__class__(dt2.year, dt2.month, dt2.day)
  90. if self.diff(dt1).in_seconds() < self.diff(dt2).in_seconds():
  91. return dt1
  92. return dt2
  93. def farthest(self, dt1: date, dt2: date) -> Self:
  94. """
  95. Get the farthest date from the instance.
  96. """
  97. dt1 = self.__class__(dt1.year, dt1.month, dt1.day)
  98. dt2 = self.__class__(dt2.year, dt2.month, dt2.day)
  99. if self.diff(dt1).in_seconds() > self.diff(dt2).in_seconds():
  100. return dt1
  101. return dt2
  102. def is_future(self) -> bool:
  103. """
  104. Determines if the instance is in the future, ie. greater than now.
  105. """
  106. return self > self.today()
  107. def is_past(self) -> bool:
  108. """
  109. Determines if the instance is in the past, ie. less than now.
  110. """
  111. return self < self.today()
  112. def is_leap_year(self) -> bool:
  113. """
  114. Determines if the instance is a leap year.
  115. """
  116. return calendar.isleap(self.year)
  117. def is_long_year(self) -> bool:
  118. """
  119. Determines if the instance is a long year
  120. See link `<https://en.wikipedia.org/wiki/ISO_8601#Week_dates>`_
  121. """
  122. return Date(self.year, 12, 28).isocalendar()[1] == 53
  123. def is_same_day(self, dt: date) -> bool:
  124. """
  125. Checks if the passed in date is the same day as the instance current day.
  126. """
  127. return self == dt
  128. def is_anniversary(self, dt: date | None = None) -> bool:
  129. """
  130. Check if it's the anniversary.
  131. Compares the date/month values of the two dates.
  132. """
  133. if dt is None:
  134. dt = self.__class__.today()
  135. instance = self.__class__(dt.year, dt.month, dt.day)
  136. return (self.month, self.day) == (instance.month, instance.day)
  137. # the additional method for checking if today is the anniversary day
  138. # the alias is provided to start using a new name and keep the backward
  139. # compatibility the old name can be completely replaced with the new in
  140. # one of the future versions
  141. is_birthday = is_anniversary
  142. # ADDITIONS AND SUBTRACTIONS
  143. def add(
  144. self, years: int = 0, months: int = 0, weeks: int = 0, days: int = 0
  145. ) -> Self:
  146. """
  147. Add duration to the instance.
  148. :param years: The number of years
  149. :param months: The number of months
  150. :param weeks: The number of weeks
  151. :param days: The number of days
  152. """
  153. dt = add_duration(
  154. date(self.year, self.month, self.day),
  155. years=years,
  156. months=months,
  157. weeks=weeks,
  158. days=days,
  159. )
  160. return self.__class__(dt.year, dt.month, dt.day)
  161. def subtract(
  162. self, years: int = 0, months: int = 0, weeks: int = 0, days: int = 0
  163. ) -> Self:
  164. """
  165. Remove duration from the instance.
  166. :param years: The number of years
  167. :param months: The number of months
  168. :param weeks: The number of weeks
  169. :param days: The number of days
  170. """
  171. return self.add(years=-years, months=-months, weeks=-weeks, days=-days)
  172. def _add_timedelta(self, delta: timedelta) -> Self:
  173. """
  174. Add timedelta duration to the instance.
  175. :param delta: The timedelta instance
  176. """
  177. if isinstance(delta, pendulum.Duration):
  178. return self.add(
  179. years=delta.years,
  180. months=delta.months,
  181. weeks=delta.weeks,
  182. days=delta.remaining_days,
  183. )
  184. return self.add(days=delta.days)
  185. def _subtract_timedelta(self, delta: timedelta) -> Self:
  186. """
  187. Remove timedelta duration from the instance.
  188. :param delta: The timedelta instance
  189. """
  190. if isinstance(delta, pendulum.Duration):
  191. return self.subtract(
  192. years=delta.years,
  193. months=delta.months,
  194. weeks=delta.weeks,
  195. days=delta.remaining_days,
  196. )
  197. return self.subtract(days=delta.days)
  198. def __add__(self, other: timedelta) -> Self:
  199. if not isinstance(other, timedelta):
  200. return NotImplemented
  201. return self._add_timedelta(other)
  202. @overload # type: ignore[override] # this is only needed because of Python 3.7
  203. def __sub__(self, __delta: timedelta) -> Self:
  204. ...
  205. @overload
  206. def __sub__(self, __dt: datetime) -> NoReturn:
  207. ...
  208. @overload
  209. def __sub__(self, __dt: Self) -> Interval:
  210. ...
  211. def __sub__(self, other: timedelta | date) -> Self | Interval:
  212. if isinstance(other, timedelta):
  213. return self._subtract_timedelta(other)
  214. if not isinstance(other, date):
  215. return NotImplemented
  216. dt = self.__class__(other.year, other.month, other.day)
  217. return dt.diff(self, False)
  218. # DIFFERENCES
  219. def diff(self, dt: date | None = None, abs: bool = True) -> Interval:
  220. """
  221. Returns the difference between two Date objects as an Interval.
  222. :param dt: The date to compare to (defaults to today)
  223. :param abs: Whether to return an absolute interval or not
  224. """
  225. if dt is None:
  226. dt = self.today()
  227. return Interval(self, Date(dt.year, dt.month, dt.day), absolute=abs)
  228. def diff_for_humans(
  229. self,
  230. other: date | None = None,
  231. absolute: bool = False,
  232. locale: str | None = None,
  233. ) -> str:
  234. """
  235. Get the difference in a human readable format in the current locale.
  236. When comparing a value in the past to default now:
  237. 1 day ago
  238. 5 months ago
  239. When comparing a value in the future to default now:
  240. 1 day from now
  241. 5 months from now
  242. When comparing a value in the past to another value:
  243. 1 day before
  244. 5 months before
  245. When comparing a value in the future to another value:
  246. 1 day after
  247. 5 months after
  248. :param other: The date to compare to (defaults to today)
  249. :param absolute: removes time difference modifiers ago, after, etc
  250. :param locale: The locale to use for localization
  251. """
  252. is_now = other is None
  253. if is_now:
  254. other = self.today()
  255. diff = self.diff(other)
  256. return pendulum.format_diff(diff, is_now, absolute, locale)
  257. # MODIFIERS
  258. def start_of(self, unit: str) -> Self:
  259. """
  260. Returns a copy of the instance with the time reset
  261. with the following rules:
  262. * day: time to 00:00:00
  263. * week: date to first day of the week and time to 00:00:00
  264. * month: date to first day of the month and time to 00:00:00
  265. * year: date to first day of the year and time to 00:00:00
  266. * decade: date to first day of the decade and time to 00:00:00
  267. * century: date to first day of century and time to 00:00:00
  268. :param unit: The unit to reset to
  269. """
  270. if unit not in self._MODIFIERS_VALID_UNITS:
  271. raise ValueError(f'Invalid unit "{unit}" for start_of()')
  272. return cast("Self", getattr(self, f"_start_of_{unit}")())
  273. def end_of(self, unit: str) -> Self:
  274. """
  275. Returns a copy of the instance with the time reset
  276. with the following rules:
  277. * week: date to last day of the week
  278. * month: date to last day of the month
  279. * year: date to last day of the year
  280. * decade: date to last day of the decade
  281. * century: date to last day of century
  282. :param unit: The unit to reset to
  283. """
  284. if unit not in self._MODIFIERS_VALID_UNITS:
  285. raise ValueError(f'Invalid unit "{unit}" for end_of()')
  286. return cast("Self", getattr(self, f"_end_of_{unit}")())
  287. def _start_of_day(self) -> Self:
  288. """
  289. Compatibility method.
  290. """
  291. return self
  292. def _end_of_day(self) -> Self:
  293. """
  294. Compatibility method
  295. """
  296. return self
  297. def _start_of_month(self) -> Self:
  298. """
  299. Reset the date to the first day of the month.
  300. """
  301. return self.set(self.year, self.month, 1)
  302. def _end_of_month(self) -> Self:
  303. """
  304. Reset the date to the last day of the month.
  305. """
  306. return self.set(self.year, self.month, self.days_in_month)
  307. def _start_of_year(self) -> Self:
  308. """
  309. Reset the date to the first day of the year.
  310. """
  311. return self.set(self.year, 1, 1)
  312. def _end_of_year(self) -> Self:
  313. """
  314. Reset the date to the last day of the year.
  315. """
  316. return self.set(self.year, 12, 31)
  317. def _start_of_decade(self) -> Self:
  318. """
  319. Reset the date to the first day of the decade.
  320. """
  321. year = self.year - self.year % YEARS_PER_DECADE
  322. return self.set(year, 1, 1)
  323. def _end_of_decade(self) -> Self:
  324. """
  325. Reset the date to the last day of the decade.
  326. """
  327. year = self.year - self.year % YEARS_PER_DECADE + YEARS_PER_DECADE - 1
  328. return self.set(year, 12, 31)
  329. def _start_of_century(self) -> Self:
  330. """
  331. Reset the date to the first day of the century.
  332. """
  333. year = self.year - 1 - (self.year - 1) % YEARS_PER_CENTURY + 1
  334. return self.set(year, 1, 1)
  335. def _end_of_century(self) -> Self:
  336. """
  337. Reset the date to the last day of the century.
  338. """
  339. year = self.year - 1 - (self.year - 1) % YEARS_PER_CENTURY + YEARS_PER_CENTURY
  340. return self.set(year, 12, 31)
  341. def _start_of_week(self) -> Self:
  342. """
  343. Reset the date to the first day of the week.
  344. """
  345. dt = self
  346. if self.day_of_week != pendulum._WEEK_STARTS_AT:
  347. dt = self.previous(pendulum._WEEK_STARTS_AT)
  348. return dt.start_of("day")
  349. def _end_of_week(self) -> Self:
  350. """
  351. Reset the date to the last day of the week.
  352. """
  353. dt = self
  354. if self.day_of_week != pendulum._WEEK_ENDS_AT:
  355. dt = self.next(pendulum._WEEK_ENDS_AT)
  356. return dt.end_of("day")
  357. def next(self, day_of_week: WeekDay | None = None) -> Self:
  358. """
  359. Modify to the next occurrence of a given day of the week.
  360. If no day_of_week is provided, modify to the next occurrence
  361. of the current day of the week. Use the supplied consts
  362. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  363. :param day_of_week: The next day of week to reset to.
  364. """
  365. if day_of_week is None:
  366. day_of_week = self.day_of_week
  367. if day_of_week < WeekDay.MONDAY or day_of_week > WeekDay.SUNDAY:
  368. raise ValueError("Invalid day of week")
  369. dt = self.add(days=1)
  370. while dt.day_of_week != day_of_week:
  371. dt = dt.add(days=1)
  372. return dt
  373. def previous(self, day_of_week: WeekDay | None = None) -> Self:
  374. """
  375. Modify to the previous occurrence of a given day of the week.
  376. If no day_of_week is provided, modify to the previous occurrence
  377. of the current day of the week. Use the supplied consts
  378. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  379. :param day_of_week: The previous day of week to reset to.
  380. """
  381. if day_of_week is None:
  382. day_of_week = self.day_of_week
  383. if day_of_week < WeekDay.MONDAY or day_of_week > WeekDay.SUNDAY:
  384. raise ValueError("Invalid day of week")
  385. dt = self.subtract(days=1)
  386. while dt.day_of_week != day_of_week:
  387. dt = dt.subtract(days=1)
  388. return dt
  389. def first_of(self, unit: str, day_of_week: WeekDay | None = None) -> Self:
  390. """
  391. Returns an instance set to the first occurrence
  392. of a given day of the week in the current unit.
  393. If no day_of_week is provided, modify to the first day of the unit.
  394. Use the supplied consts to indicate the desired day_of_week,
  395. ex. pendulum.MONDAY.
  396. Supported units are month, quarter and year.
  397. :param unit: The unit to use
  398. :param day_of_week: The day of week to reset to.
  399. """
  400. if unit not in ["month", "quarter", "year"]:
  401. raise ValueError(f'Invalid unit "{unit}" for first_of()')
  402. return cast("Self", getattr(self, f"_first_of_{unit}")(day_of_week))
  403. def last_of(self, unit: str, day_of_week: WeekDay | None = None) -> Self:
  404. """
  405. Returns an instance set to the last occurrence
  406. of a given day of the week in the current unit.
  407. If no day_of_week is provided, modify to the last day of the unit.
  408. Use the supplied consts to indicate the desired day_of_week,
  409. ex. pendulum.MONDAY.
  410. Supported units are month, quarter and year.
  411. :param unit: The unit to use
  412. :param day_of_week: The day of week to reset to.
  413. """
  414. if unit not in ["month", "quarter", "year"]:
  415. raise ValueError(f'Invalid unit "{unit}" for first_of()')
  416. return cast("Self", getattr(self, f"_last_of_{unit}")(day_of_week))
  417. def nth_of(self, unit: str, nth: int, day_of_week: WeekDay) -> Self:
  418. """
  419. Returns a new instance set to the given occurrence
  420. of a given day of the week in the current unit.
  421. If the calculated occurrence is outside the scope of the current unit,
  422. then raise an error. Use the supplied consts
  423. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  424. Supported units are month, quarter and year.
  425. :param unit: The unit to use
  426. :param nth: The occurrence to use
  427. :param day_of_week: The day of week to set to.
  428. """
  429. if unit not in ["month", "quarter", "year"]:
  430. raise ValueError(f'Invalid unit "{unit}" for first_of()')
  431. dt = cast("Self", getattr(self, f"_nth_of_{unit}")(nth, day_of_week))
  432. if not dt:
  433. raise PendulumException(
  434. f"Unable to find occurrence {nth}"
  435. f" of {WeekDay(day_of_week).name.capitalize()} in {unit}"
  436. )
  437. return dt
  438. def _first_of_month(self, day_of_week: WeekDay) -> Self:
  439. """
  440. Modify to the first occurrence of a given day of the week
  441. in the current month. If no day_of_week is provided,
  442. modify to the first day of the month. Use the supplied consts
  443. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  444. :param day_of_week: The day of week to set to.
  445. """
  446. dt = self
  447. if day_of_week is None:
  448. return dt.set(day=1)
  449. month = calendar.monthcalendar(dt.year, dt.month)
  450. calendar_day = day_of_week
  451. if month[0][calendar_day] > 0:
  452. day_of_month = month[0][calendar_day]
  453. else:
  454. day_of_month = month[1][calendar_day]
  455. return dt.set(day=day_of_month)
  456. def _last_of_month(self, day_of_week: WeekDay | None = None) -> Self:
  457. """
  458. Modify to the last occurrence of a given day of the week
  459. in the current month. If no day_of_week is provided,
  460. modify to the last day of the month. Use the supplied consts
  461. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  462. :param day_of_week: The day of week to set to.
  463. """
  464. dt = self
  465. if day_of_week is None:
  466. return dt.set(day=self.days_in_month)
  467. month = calendar.monthcalendar(dt.year, dt.month)
  468. calendar_day = day_of_week
  469. if month[-1][calendar_day] > 0:
  470. day_of_month = month[-1][calendar_day]
  471. else:
  472. day_of_month = month[-2][calendar_day]
  473. return dt.set(day=day_of_month)
  474. def _nth_of_month(self, nth: int, day_of_week: WeekDay) -> Self | None:
  475. """
  476. Modify to the given occurrence of a given day of the week
  477. in the current month. If the calculated occurrence is outside,
  478. the scope of the current month, then return False and no
  479. modifications are made. Use the supplied consts
  480. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  481. """
  482. if nth == 1:
  483. return self.first_of("month", day_of_week)
  484. dt = self.first_of("month")
  485. check = dt.format("YYYY-MM")
  486. for _ in range(nth - (1 if dt.day_of_week == day_of_week else 0)):
  487. dt = dt.next(day_of_week)
  488. if dt.format("YYYY-MM") == check:
  489. return self.set(day=dt.day)
  490. return None
  491. def _first_of_quarter(self, day_of_week: WeekDay | None = None) -> Self:
  492. """
  493. Modify to the first occurrence of a given day of the week
  494. in the current quarter. If no day_of_week is provided,
  495. modify to the first day of the quarter. Use the supplied consts
  496. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  497. """
  498. return self.set(self.year, self.quarter * 3 - 2, 1).first_of(
  499. "month", day_of_week
  500. )
  501. def _last_of_quarter(self, day_of_week: WeekDay | None = None) -> Self:
  502. """
  503. Modify to the last occurrence of a given day of the week
  504. in the current quarter. If no day_of_week is provided,
  505. modify to the last day of the quarter. Use the supplied consts
  506. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  507. """
  508. return self.set(self.year, self.quarter * 3, 1).last_of("month", day_of_week)
  509. def _nth_of_quarter(self, nth: int, day_of_week: WeekDay) -> Self | None:
  510. """
  511. Modify to the given occurrence of a given day of the week
  512. in the current quarter. If the calculated occurrence is outside,
  513. the scope of the current quarter, then return False and no
  514. modifications are made. Use the supplied consts
  515. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  516. """
  517. if nth == 1:
  518. return self.first_of("quarter", day_of_week)
  519. dt = self.replace(self.year, self.quarter * 3, 1)
  520. last_month = dt.month
  521. year = dt.year
  522. dt = dt.first_of("quarter")
  523. for _ in range(nth - (1 if dt.day_of_week == day_of_week else 0)):
  524. dt = dt.next(day_of_week)
  525. if last_month < dt.month or year != dt.year:
  526. return None
  527. return self.set(self.year, dt.month, dt.day)
  528. def _first_of_year(self, day_of_week: WeekDay | None = None) -> Self:
  529. """
  530. Modify to the first occurrence of a given day of the week
  531. in the current year. If no day_of_week is provided,
  532. modify to the first day of the year. Use the supplied consts
  533. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  534. """
  535. return self.set(month=1).first_of("month", day_of_week)
  536. def _last_of_year(self, day_of_week: WeekDay | None = None) -> Self:
  537. """
  538. Modify to the last occurrence of a given day of the week
  539. in the current year. If no day_of_week is provided,
  540. modify to the last day of the year. Use the supplied consts
  541. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  542. """
  543. return self.set(month=MONTHS_PER_YEAR).last_of("month", day_of_week)
  544. def _nth_of_year(self, nth: int, day_of_week: WeekDay) -> Self | None:
  545. """
  546. Modify to the given occurrence of a given day of the week
  547. in the current year. If the calculated occurrence is outside,
  548. the scope of the current year, then return False and no
  549. modifications are made. Use the supplied consts
  550. to indicate the desired day_of_week, ex. pendulum.MONDAY.
  551. """
  552. if nth == 1:
  553. return self.first_of("year", day_of_week)
  554. dt = self.first_of("year")
  555. year = dt.year
  556. for _ in range(nth - (1 if dt.day_of_week == day_of_week else 0)):
  557. dt = dt.next(day_of_week)
  558. if year != dt.year:
  559. return None
  560. return self.set(self.year, dt.month, dt.day)
  561. def average(self, dt: date | None = None) -> Self:
  562. """
  563. Modify the current instance to the average
  564. of a given instance (default now) and the current instance.
  565. """
  566. if dt is None:
  567. dt = Date.today()
  568. return self.add(days=int(self.diff(dt, False).in_days() / 2))
  569. # Native methods override
  570. @classmethod
  571. def today(cls) -> Self:
  572. dt = date.today()
  573. return cls(dt.year, dt.month, dt.day)
  574. @classmethod
  575. def fromtimestamp(cls, t: float) -> Self:
  576. dt = super().fromtimestamp(t)
  577. return cls(dt.year, dt.month, dt.day)
  578. @classmethod
  579. def fromordinal(cls, n: int) -> Self:
  580. dt = super().fromordinal(n)
  581. return cls(dt.year, dt.month, dt.day)
  582. def replace(
  583. self,
  584. year: SupportsIndex | None = None,
  585. month: SupportsIndex | None = None,
  586. day: SupportsIndex | None = None,
  587. ) -> Self:
  588. year = year if year is not None else self.year
  589. month = month if month is not None else self.month
  590. day = day if day is not None else self.day
  591. return self.__class__(year, month, day)