Skip to content

Commit

Permalink
Make day of week convention more consistent across the codebase (#731)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored Aug 14, 2023
1 parent 7e4f806 commit 29c15a6
Show file tree
Hide file tree
Showing 47 changed files with 1,313 additions and 624 deletions.
9 changes: 6 additions & 3 deletions clock
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,8 @@ translations = {{}}
data["days"] = {}
for fmt, names in days.items():
data["days"][fmt] = {}
for value, name in names.items():
data["days"][fmt][(value + 1) % 7] = name
for value, name in sorted(names.items()):
data["days"][fmt][value] = name

# Getting months names
months = content["months"]["format"]
Expand Down Expand Up @@ -158,6 +158,9 @@ translations = {{}}
# Day periods
data["day_periods"] = content["day_periods"]["format"]["wide"]

# Week data
data["week_data"] = content["week_data"]

result = self.TEMPLATE.format(
locale=locale,
plural=plural,
Expand Down Expand Up @@ -238,7 +241,7 @@ class LocaleRecreate(Command):
locales = glob.glob(os.path.join(locales_dir, "*", "locale.py"))
locales = [os.path.basename(os.path.dirname(locale)) for locale in locales]

self.call("locale:create", [("locales", locales)])
self.call("locale create", "locales " + " ".join(locales))


class WindowsTzDump(Command):
Expand Down
29 changes: 13 additions & 16 deletions pendulum/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,18 @@

from pendulum.__version__ import __version__
from pendulum.constants import DAYS_PER_WEEK
from pendulum.constants import FRIDAY
from pendulum.constants import HOURS_PER_DAY
from pendulum.constants import MINUTES_PER_HOUR
from pendulum.constants import MONDAY
from pendulum.constants import MONTHS_PER_YEAR
from pendulum.constants import SATURDAY
from pendulum.constants import SECONDS_PER_DAY
from pendulum.constants import SECONDS_PER_HOUR
from pendulum.constants import SECONDS_PER_MINUTE
from pendulum.constants import SUNDAY
from pendulum.constants import THURSDAY
from pendulum.constants import TUESDAY
from pendulum.constants import WEDNESDAY
from pendulum.constants import WEEKS_PER_YEAR
from pendulum.constants import YEARS_PER_CENTURY
from pendulum.constants import YEARS_PER_DECADE
from pendulum.date import Date
from pendulum.datetime import DateTime
from pendulum.day import WeekDay
from pendulum.duration import Duration
from pendulum.formatting import Formatter
from pendulum.helpers import format_diff
Expand All @@ -48,10 +42,19 @@
from pendulum.tz.timezone import Timezone


MONDAY = WeekDay.MONDAY
TUESDAY = WeekDay.TUESDAY
WEDNESDAY = WeekDay.WEDNESDAY
THURSDAY = WeekDay.THURSDAY
FRIDAY = WeekDay.FRIDAY
SATURDAY = WeekDay.SATURDAY
SUNDAY = WeekDay.SUNDAY


_TEST_NOW: DateTime | None = None
_LOCALE = "en"
_WEEK_STARTS_AT = MONDAY
_WEEK_ENDS_AT = SUNDAY
_WEEK_STARTS_AT: WeekDay = WeekDay.MONDAY
_WEEK_ENDS_AT: WeekDay = WeekDay.SUNDAY

_formatter = Formatter()

Expand Down Expand Up @@ -335,26 +338,20 @@ def interval(start: DateTime, end: DateTime, absolute: bool = False) -> Interval
__all__ = [
"__version__",
"DAYS_PER_WEEK",
"FRIDAY",
"HOURS_PER_DAY",
"MINUTES_PER_HOUR",
"MONDAY",
"MONTHS_PER_YEAR",
"SATURDAY",
"SECONDS_PER_DAY",
"SECONDS_PER_HOUR",
"SECONDS_PER_MINUTE",
"SUNDAY",
"THURSDAY",
"TUESDAY",
"WEDNESDAY",
"WEEKS_PER_YEAR",
"YEARS_PER_CENTURY",
"YEARS_PER_DECADE",
"Date",
"DateTime",
"Duration",
"Formatter",
"WeekDay",
"date",
"datetime",
"duration",
Expand Down
8 changes: 0 additions & 8 deletions pendulum/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,6 @@
from __future__ import annotations


SUNDAY = 0
MONDAY = 1
TUESDAY = 2
WEDNESDAY = 3
THURSDAY = 4
FRIDAY = 5
SATURDAY = 6

# Number of X in Y.
YEARS_PER_CENTURY = 100
YEARS_PER_DECADE = 10
Expand Down
61 changes: 22 additions & 39 deletions pendulum/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,10 @@

import pendulum

from pendulum.constants import FRIDAY
from pendulum.constants import MONDAY
from pendulum.constants import MONTHS_PER_YEAR
from pendulum.constants import SATURDAY
from pendulum.constants import SUNDAY
from pendulum.constants import THURSDAY
from pendulum.constants import TUESDAY
from pendulum.constants import WEDNESDAY
from pendulum.constants import YEARS_PER_CENTURY
from pendulum.constants import YEARS_PER_DECADE
from pendulum.day import WeekDay
from pendulum.exceptions import PendulumException
from pendulum.helpers import add_duration
from pendulum.interval import Interval
Expand All @@ -38,17 +32,6 @@


class Date(FormattableMixin, date):
# Names of days of the week
_days: ClassVar[dict[int, str]] = {
SUNDAY: "Sunday",
MONDAY: "Monday",
TUESDAY: "Tuesday",
WEDNESDAY: "Wednesday",
THURSDAY: "Thursday",
FRIDAY: "Friday",
SATURDAY: "Saturday",
}

_MODIFIERS_VALID_UNITS: ClassVar[list[str]] = [
"day",
"week",
Expand All @@ -66,11 +49,11 @@ def set(
return self.replace(year=year, month=month, day=day)

@property
def day_of_week(self) -> int:
def day_of_week(self) -> WeekDay:
"""
Returns the day of the week (0-6).
"""
return self.isoweekday() % 7
return WeekDay(self.weekday())

@property
def day_of_year(self) -> int:
Expand Down Expand Up @@ -479,7 +462,7 @@ def _end_of_week(self) -> Self:

return dt.end_of("day")

def next(self, day_of_week: int | None = None) -> Self:
def next(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the next occurrence of a given day of the week.
If no day_of_week is provided, modify to the next occurrence
Expand All @@ -491,7 +474,7 @@ def next(self, day_of_week: int | None = None) -> Self:
if day_of_week is None:
day_of_week = self.day_of_week

if day_of_week < SUNDAY or day_of_week > SATURDAY:
if day_of_week < WeekDay.MONDAY or day_of_week > WeekDay.SUNDAY:
raise ValueError("Invalid day of week")

dt = self.add(days=1)
Expand All @@ -500,7 +483,7 @@ def next(self, day_of_week: int | None = None) -> Self:

return dt

def previous(self, day_of_week: int | None = None) -> Self:
def previous(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the previous occurrence of a given day of the week.
If no day_of_week is provided, modify to the previous occurrence
Expand All @@ -512,7 +495,7 @@ def previous(self, day_of_week: int | None = None) -> Self:
if day_of_week is None:
day_of_week = self.day_of_week

if day_of_week < SUNDAY or day_of_week > SATURDAY:
if day_of_week < WeekDay.MONDAY or day_of_week > WeekDay.SUNDAY:
raise ValueError("Invalid day of week")

dt = self.subtract(days=1)
Expand All @@ -521,7 +504,7 @@ def previous(self, day_of_week: int | None = None) -> Self:

return dt

def first_of(self, unit: str, day_of_week: int | None = None) -> Self:
def first_of(self, unit: str, day_of_week: WeekDay | None = None) -> Self:
"""
Returns an instance set to the first occurrence
of a given day of the week in the current unit.
Expand All @@ -539,7 +522,7 @@ def first_of(self, unit: str, day_of_week: int | None = None) -> Self:

return cast("Self", getattr(self, f"_first_of_{unit}")(day_of_week))

def last_of(self, unit: str, day_of_week: int | None = None) -> Self:
def last_of(self, unit: str, day_of_week: WeekDay | None = None) -> Self:
"""
Returns an instance set to the last occurrence
of a given day of the week in the current unit.
Expand All @@ -557,7 +540,7 @@ def last_of(self, unit: str, day_of_week: int | None = None) -> Self:

return cast("Self", getattr(self, f"_last_of_{unit}")(day_of_week))

def nth_of(self, unit: str, nth: int, day_of_week: int) -> Self:
def nth_of(self, unit: str, nth: int, day_of_week: WeekDay) -> Self:
"""
Returns a new instance set to the given occurrence
of a given day of the week in the current unit.
Expand All @@ -578,12 +561,12 @@ def nth_of(self, unit: str, nth: int, day_of_week: int) -> Self:
if not dt:
raise PendulumException(
f"Unable to find occurence {nth}"
f" of {self._days[day_of_week]} in {unit}"
f" of {WeekDay(day_of_week).name.capitalize()} in {unit}"
)

return dt

def _first_of_month(self, day_of_week: int) -> Self:
def _first_of_month(self, day_of_week: WeekDay) -> Self:
"""
Modify to the first occurrence of a given day of the week
in the current month. If no day_of_week is provided,
Expand All @@ -599,7 +582,7 @@ def _first_of_month(self, day_of_week: int) -> Self:

month = calendar.monthcalendar(dt.year, dt.month)

calendar_day = (day_of_week - 1) % 7
calendar_day = day_of_week

if month[0][calendar_day] > 0:
day_of_month = month[0][calendar_day]
Expand All @@ -608,7 +591,7 @@ def _first_of_month(self, day_of_week: int) -> Self:

return dt.set(day=day_of_month)

def _last_of_month(self, day_of_week: int | None = None) -> Self:
def _last_of_month(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the last occurrence of a given day of the week
in the current month. If no day_of_week is provided,
Expand All @@ -624,7 +607,7 @@ def _last_of_month(self, day_of_week: int | None = None) -> Self:

month = calendar.monthcalendar(dt.year, dt.month)

calendar_day = (day_of_week - 1) % 7
calendar_day = day_of_week

if month[-1][calendar_day] > 0:
day_of_month = month[-1][calendar_day]
Expand All @@ -633,7 +616,7 @@ def _last_of_month(self, day_of_week: int | None = None) -> Self:

return dt.set(day=day_of_month)

def _nth_of_month(self, nth: int, day_of_week: int) -> Self | None:
def _nth_of_month(self, nth: int, day_of_week: WeekDay) -> Self | None:
"""
Modify to the given occurrence of a given day of the week
in the current month. If the calculated occurrence is outside,
Expand All @@ -654,7 +637,7 @@ def _nth_of_month(self, nth: int, day_of_week: int) -> Self | None:

return None

def _first_of_quarter(self, day_of_week: int | None = None) -> Self:
def _first_of_quarter(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the first occurrence of a given day of the week
in the current quarter. If no day_of_week is provided,
Expand All @@ -665,7 +648,7 @@ def _first_of_quarter(self, day_of_week: int | None = None) -> Self:
"month", day_of_week
)

def _last_of_quarter(self, day_of_week: int | None = None) -> Self:
def _last_of_quarter(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the last occurrence of a given day of the week
in the current quarter. If no day_of_week is provided,
Expand All @@ -674,7 +657,7 @@ def _last_of_quarter(self, day_of_week: int | None = None) -> Self:
"""
return self.set(self.year, self.quarter * 3, 1).last_of("month", day_of_week)

def _nth_of_quarter(self, nth: int, day_of_week: int) -> Self | None:
def _nth_of_quarter(self, nth: int, day_of_week: WeekDay) -> Self | None:
"""
Modify to the given occurrence of a given day of the week
in the current quarter. If the calculated occurrence is outside,
Expand All @@ -697,7 +680,7 @@ def _nth_of_quarter(self, nth: int, day_of_week: int) -> Self | None:

return self.set(self.year, dt.month, dt.day)

def _first_of_year(self, day_of_week: int | None = None) -> Self:
def _first_of_year(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the first occurrence of a given day of the week
in the current year. If no day_of_week is provided,
Expand All @@ -706,7 +689,7 @@ def _first_of_year(self, day_of_week: int | None = None) -> Self:
"""
return self.set(month=1).first_of("month", day_of_week)

def _last_of_year(self, day_of_week: int | None = None) -> Self:
def _last_of_year(self, day_of_week: WeekDay | None = None) -> Self:
"""
Modify to the last occurrence of a given day of the week
in the current year. If no day_of_week is provided,
Expand All @@ -715,7 +698,7 @@ def _last_of_year(self, day_of_week: int | None = None) -> Self:
"""
return self.set(month=MONTHS_PER_YEAR).last_of("month", day_of_week)

def _nth_of_year(self, nth: int, day_of_week: int) -> Self | None:
def _nth_of_year(self, nth: int, day_of_week: WeekDay) -> Self | None:
"""
Modify to the given occurrence of a given day of the week
in the current year. If the calculated occurrence is outside,
Expand Down
Loading

0 comments on commit 29c15a6

Please sign in to comment.