Skip to content

Commit 29c15a6

Browse files
authored
Make day of week convention more consistent across the codebase (#731)
1 parent 7e4f806 commit 29c15a6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1313
-624
lines changed

clock

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ translations = {{}}
112112
data["days"] = {}
113113
for fmt, names in days.items():
114114
data["days"][fmt] = {}
115-
for value, name in names.items():
116-
data["days"][fmt][(value + 1) % 7] = name
115+
for value, name in sorted(names.items()):
116+
data["days"][fmt][value] = name
117117

118118
# Getting months names
119119
months = content["months"]["format"]
@@ -158,6 +158,9 @@ translations = {{}}
158158
# Day periods
159159
data["day_periods"] = content["day_periods"]["format"]["wide"]
160160

161+
# Week data
162+
data["week_data"] = content["week_data"]
163+
161164
result = self.TEMPLATE.format(
162165
locale=locale,
163166
plural=plural,
@@ -238,7 +241,7 @@ class LocaleRecreate(Command):
238241
locales = glob.glob(os.path.join(locales_dir, "*", "locale.py"))
239242
locales = [os.path.basename(os.path.dirname(locale)) for locale in locales]
240243

241-
self.call("locale:create", [("locales", locales)])
244+
self.call("locale create", "locales " + " ".join(locales))
242245

243246

244247
class WindowsTzDump(Command):

pendulum/__init__.py

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,18 @@
88

99
from pendulum.__version__ import __version__
1010
from pendulum.constants import DAYS_PER_WEEK
11-
from pendulum.constants import FRIDAY
1211
from pendulum.constants import HOURS_PER_DAY
1312
from pendulum.constants import MINUTES_PER_HOUR
14-
from pendulum.constants import MONDAY
1513
from pendulum.constants import MONTHS_PER_YEAR
16-
from pendulum.constants import SATURDAY
1714
from pendulum.constants import SECONDS_PER_DAY
1815
from pendulum.constants import SECONDS_PER_HOUR
1916
from pendulum.constants import SECONDS_PER_MINUTE
20-
from pendulum.constants import SUNDAY
21-
from pendulum.constants import THURSDAY
22-
from pendulum.constants import TUESDAY
23-
from pendulum.constants import WEDNESDAY
2417
from pendulum.constants import WEEKS_PER_YEAR
2518
from pendulum.constants import YEARS_PER_CENTURY
2619
from pendulum.constants import YEARS_PER_DECADE
2720
from pendulum.date import Date
2821
from pendulum.datetime import DateTime
22+
from pendulum.day import WeekDay
2923
from pendulum.duration import Duration
3024
from pendulum.formatting import Formatter
3125
from pendulum.helpers import format_diff
@@ -48,10 +42,19 @@
4842
from pendulum.tz.timezone import Timezone
4943

5044

45+
MONDAY = WeekDay.MONDAY
46+
TUESDAY = WeekDay.TUESDAY
47+
WEDNESDAY = WeekDay.WEDNESDAY
48+
THURSDAY = WeekDay.THURSDAY
49+
FRIDAY = WeekDay.FRIDAY
50+
SATURDAY = WeekDay.SATURDAY
51+
SUNDAY = WeekDay.SUNDAY
52+
53+
5154
_TEST_NOW: DateTime | None = None
5255
_LOCALE = "en"
53-
_WEEK_STARTS_AT = MONDAY
54-
_WEEK_ENDS_AT = SUNDAY
56+
_WEEK_STARTS_AT: WeekDay = WeekDay.MONDAY
57+
_WEEK_ENDS_AT: WeekDay = WeekDay.SUNDAY
5558

5659
_formatter = Formatter()
5760

@@ -335,26 +338,20 @@ def interval(start: DateTime, end: DateTime, absolute: bool = False) -> Interval
335338
__all__ = [
336339
"__version__",
337340
"DAYS_PER_WEEK",
338-
"FRIDAY",
339341
"HOURS_PER_DAY",
340342
"MINUTES_PER_HOUR",
341-
"MONDAY",
342343
"MONTHS_PER_YEAR",
343-
"SATURDAY",
344344
"SECONDS_PER_DAY",
345345
"SECONDS_PER_HOUR",
346346
"SECONDS_PER_MINUTE",
347-
"SUNDAY",
348-
"THURSDAY",
349-
"TUESDAY",
350-
"WEDNESDAY",
351347
"WEEKS_PER_YEAR",
352348
"YEARS_PER_CENTURY",
353349
"YEARS_PER_DECADE",
354350
"Date",
355351
"DateTime",
356352
"Duration",
357353
"Formatter",
354+
"WeekDay",
358355
"date",
359356
"datetime",
360357
"duration",

pendulum/constants.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,6 @@
22
from __future__ import annotations
33

44

5-
SUNDAY = 0
6-
MONDAY = 1
7-
TUESDAY = 2
8-
WEDNESDAY = 3
9-
THURSDAY = 4
10-
FRIDAY = 5
11-
SATURDAY = 6
12-
135
# Number of X in Y.
146
YEARS_PER_CENTURY = 100
157
YEARS_PER_DECADE = 10

pendulum/date.py

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,10 @@
1616

1717
import pendulum
1818

19-
from pendulum.constants import FRIDAY
20-
from pendulum.constants import MONDAY
2119
from 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
2720
from pendulum.constants import YEARS_PER_CENTURY
2821
from pendulum.constants import YEARS_PER_DECADE
22+
from pendulum.day import WeekDay
2923
from pendulum.exceptions import PendulumException
3024
from pendulum.helpers import add_duration
3125
from pendulum.interval import Interval
@@ -38,17 +32,6 @@
3832

3933

4034
class 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

Comments
 (0)