1616
1717import pendulum
1818
19- from pendulum .constants import FRIDAY
20- from pendulum .constants import MONDAY
2119from pendulum .constants import MONTHS_PER_YEAR
22- from pendulum .constants import SATURDAY
23- from pendulum .constants import SUNDAY
24- from pendulum .constants import THURSDAY
25- from pendulum .constants import TUESDAY
26- from pendulum .constants import WEDNESDAY
2720from pendulum .constants import YEARS_PER_CENTURY
2821from pendulum .constants import YEARS_PER_DECADE
22+ from pendulum .day import WeekDay
2923from pendulum .exceptions import PendulumException
3024from pendulum .helpers import add_duration
3125from pendulum .interval import Interval
3832
3933
4034class Date (FormattableMixin , date ):
41- # Names of days of the week
42- _days : ClassVar [dict [int , str ]] = {
43- SUNDAY : "Sunday" ,
44- MONDAY : "Monday" ,
45- TUESDAY : "Tuesday" ,
46- WEDNESDAY : "Wednesday" ,
47- THURSDAY : "Thursday" ,
48- FRIDAY : "Friday" ,
49- SATURDAY : "Saturday" ,
50- }
51-
5235 _MODIFIERS_VALID_UNITS : ClassVar [list [str ]] = [
5336 "day" ,
5437 "week" ,
@@ -66,11 +49,11 @@ def set(
6649 return self .replace (year = year , month = month , day = day )
6750
6851 @property
69- def day_of_week (self ) -> int :
52+ def day_of_week (self ) -> WeekDay :
7053 """
7154 Returns the day of the week (0-6).
7255 """
73- return self .isoweekday () % 7
56+ return WeekDay ( self .weekday ())
7457
7558 @property
7659 def day_of_year (self ) -> int :
@@ -479,7 +462,7 @@ def _end_of_week(self) -> Self:
479462
480463 return dt .end_of ("day" )
481464
482- def next (self , day_of_week : int | None = None ) -> Self :
465+ def next (self , day_of_week : WeekDay | None = None ) -> Self :
483466 """
484467 Modify to the next occurrence of a given day of the week.
485468 If no day_of_week is provided, modify to the next occurrence
@@ -491,7 +474,7 @@ def next(self, day_of_week: int | None = None) -> Self:
491474 if day_of_week is None :
492475 day_of_week = self .day_of_week
493476
494- if day_of_week < SUNDAY or day_of_week > SATURDAY :
477+ if day_of_week < WeekDay . MONDAY or day_of_week > WeekDay . SUNDAY :
495478 raise ValueError ("Invalid day of week" )
496479
497480 dt = self .add (days = 1 )
@@ -500,7 +483,7 @@ def next(self, day_of_week: int | None = None) -> Self:
500483
501484 return dt
502485
503- def previous (self , day_of_week : int | None = None ) -> Self :
486+ def previous (self , day_of_week : WeekDay | None = None ) -> Self :
504487 """
505488 Modify to the previous occurrence of a given day of the week.
506489 If no day_of_week is provided, modify to the previous occurrence
@@ -512,7 +495,7 @@ def previous(self, day_of_week: int | None = None) -> Self:
512495 if day_of_week is None :
513496 day_of_week = self .day_of_week
514497
515- if day_of_week < SUNDAY or day_of_week > SATURDAY :
498+ if day_of_week < WeekDay . MONDAY or day_of_week > WeekDay . SUNDAY :
516499 raise ValueError ("Invalid day of week" )
517500
518501 dt = self .subtract (days = 1 )
@@ -521,7 +504,7 @@ def previous(self, day_of_week: int | None = None) -> Self:
521504
522505 return dt
523506
524- def first_of (self , unit : str , day_of_week : int | None = None ) -> Self :
507+ def first_of (self , unit : str , day_of_week : WeekDay | None = None ) -> Self :
525508 """
526509 Returns an instance set to the first occurrence
527510 of a given day of the week in the current unit.
@@ -539,7 +522,7 @@ def first_of(self, unit: str, day_of_week: int | None = None) -> Self:
539522
540523 return cast ("Self" , getattr (self , f"_first_of_{ unit } " )(day_of_week ))
541524
542- def last_of (self , unit : str , day_of_week : int | None = None ) -> Self :
525+ def last_of (self , unit : str , day_of_week : WeekDay | None = None ) -> Self :
543526 """
544527 Returns an instance set to the last occurrence
545528 of a given day of the week in the current unit.
@@ -557,7 +540,7 @@ def last_of(self, unit: str, day_of_week: int | None = None) -> Self:
557540
558541 return cast ("Self" , getattr (self , f"_last_of_{ unit } " )(day_of_week ))
559542
560- def nth_of (self , unit : str , nth : int , day_of_week : int ) -> Self :
543+ def nth_of (self , unit : str , nth : int , day_of_week : WeekDay ) -> Self :
561544 """
562545 Returns a new instance set to the given occurrence
563546 of a given day of the week in the current unit.
@@ -578,12 +561,12 @@ def nth_of(self, unit: str, nth: int, day_of_week: int) -> Self:
578561 if not dt :
579562 raise PendulumException (
580563 f"Unable to find occurence { nth } "
581- f" of { self . _days [ day_of_week ] } in { unit } "
564+ f" of { WeekDay ( day_of_week ). name . capitalize () } in { unit } "
582565 )
583566
584567 return dt
585568
586- def _first_of_month (self , day_of_week : int ) -> Self :
569+ def _first_of_month (self , day_of_week : WeekDay ) -> Self :
587570 """
588571 Modify to the first occurrence of a given day of the week
589572 in the current month. If no day_of_week is provided,
@@ -599,7 +582,7 @@ def _first_of_month(self, day_of_week: int) -> Self:
599582
600583 month = calendar .monthcalendar (dt .year , dt .month )
601584
602- calendar_day = ( day_of_week - 1 ) % 7
585+ calendar_day = day_of_week
603586
604587 if month [0 ][calendar_day ] > 0 :
605588 day_of_month = month [0 ][calendar_day ]
@@ -608,7 +591,7 @@ def _first_of_month(self, day_of_week: int) -> Self:
608591
609592 return dt .set (day = day_of_month )
610593
611- def _last_of_month (self , day_of_week : int | None = None ) -> Self :
594+ def _last_of_month (self , day_of_week : WeekDay | None = None ) -> Self :
612595 """
613596 Modify to the last occurrence of a given day of the week
614597 in the current month. If no day_of_week is provided,
@@ -624,7 +607,7 @@ def _last_of_month(self, day_of_week: int | None = None) -> Self:
624607
625608 month = calendar .monthcalendar (dt .year , dt .month )
626609
627- calendar_day = ( day_of_week - 1 ) % 7
610+ calendar_day = day_of_week
628611
629612 if month [- 1 ][calendar_day ] > 0 :
630613 day_of_month = month [- 1 ][calendar_day ]
@@ -633,7 +616,7 @@ def _last_of_month(self, day_of_week: int | None = None) -> Self:
633616
634617 return dt .set (day = day_of_month )
635618
636- def _nth_of_month (self , nth : int , day_of_week : int ) -> Self | None :
619+ def _nth_of_month (self , nth : int , day_of_week : WeekDay ) -> Self | None :
637620 """
638621 Modify to the given occurrence of a given day of the week
639622 in the current month. If the calculated occurrence is outside,
@@ -654,7 +637,7 @@ def _nth_of_month(self, nth: int, day_of_week: int) -> Self | None:
654637
655638 return None
656639
657- def _first_of_quarter (self , day_of_week : int | None = None ) -> Self :
640+ def _first_of_quarter (self , day_of_week : WeekDay | None = None ) -> Self :
658641 """
659642 Modify to the first occurrence of a given day of the week
660643 in the current quarter. If no day_of_week is provided,
@@ -665,7 +648,7 @@ def _first_of_quarter(self, day_of_week: int | None = None) -> Self:
665648 "month" , day_of_week
666649 )
667650
668- def _last_of_quarter (self , day_of_week : int | None = None ) -> Self :
651+ def _last_of_quarter (self , day_of_week : WeekDay | None = None ) -> Self :
669652 """
670653 Modify to the last occurrence of a given day of the week
671654 in the current quarter. If no day_of_week is provided,
@@ -674,7 +657,7 @@ def _last_of_quarter(self, day_of_week: int | None = None) -> Self:
674657 """
675658 return self .set (self .year , self .quarter * 3 , 1 ).last_of ("month" , day_of_week )
676659
677- def _nth_of_quarter (self , nth : int , day_of_week : int ) -> Self | None :
660+ def _nth_of_quarter (self , nth : int , day_of_week : WeekDay ) -> Self | None :
678661 """
679662 Modify to the given occurrence of a given day of the week
680663 in the current quarter. If the calculated occurrence is outside,
@@ -697,7 +680,7 @@ def _nth_of_quarter(self, nth: int, day_of_week: int) -> Self | None:
697680
698681 return self .set (self .year , dt .month , dt .day )
699682
700- def _first_of_year (self , day_of_week : int | None = None ) -> Self :
683+ def _first_of_year (self , day_of_week : WeekDay | None = None ) -> Self :
701684 """
702685 Modify to the first occurrence of a given day of the week
703686 in the current year. If no day_of_week is provided,
@@ -706,7 +689,7 @@ def _first_of_year(self, day_of_week: int | None = None) -> Self:
706689 """
707690 return self .set (month = 1 ).first_of ("month" , day_of_week )
708691
709- def _last_of_year (self , day_of_week : int | None = None ) -> Self :
692+ def _last_of_year (self , day_of_week : WeekDay | None = None ) -> Self :
710693 """
711694 Modify to the last occurrence of a given day of the week
712695 in the current year. If no day_of_week is provided,
@@ -715,7 +698,7 @@ def _last_of_year(self, day_of_week: int | None = None) -> Self:
715698 """
716699 return self .set (month = MONTHS_PER_YEAR ).last_of ("month" , day_of_week )
717700
718- def _nth_of_year (self , nth : int , day_of_week : int ) -> Self | None :
701+ def _nth_of_year (self , nth : int , day_of_week : WeekDay ) -> Self | None :
719702 """
720703 Modify to the given occurrence of a given day of the week
721704 in the current year. If the calculated occurrence is outside,
0 commit comments