Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix leap year & negative week number issue #766

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 22 additions & 2 deletions src/pendulum/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,28 @@ def days_in_month(self) -> int:
@property
def week_of_month(self) -> int:
first_day_of_month = self.replace(day=1)

return self.week_of_year - first_day_of_month.week_of_year + 1
last_day_of_month = self.replace(
day=1,
month=1 if self.month == 12 else self.month + 1,
year=self.year + 1 if self.month == 12 else self.year,
) - timedelta(days=1)

week_of_month = self.week_of_year - first_day_of_month.week_of_year + 1

if (
self.month == 1
and first_day_of_month.week_of_year >= 52
and self.week_of_year < 52
):
return self.week_of_year + 1

elif self.month == 12 and (
last_day_of_month.week_of_year < 52
and self.week_of_year == last_day_of_month.week_of_year
):
return 52 + week_of_month

return week_of_month

@property
def age(self) -> int:
Expand Down
31 changes: 31 additions & 0 deletions tests/date/test_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,37 @@ def test_week_of_month():
assert pendulum.date(2020, 1, 14).week_of_month == 3


def test_week_of_month_negative():

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you could parametrize this test using @pytest.mark.parametrize

assert pendulum.Date(2000, 1, 1).week_of_month == 1
assert pendulum.Date(2000, 1, 3).week_of_month == 2
assert pendulum.Date(2019, 12, 29).week_of_month == 5
assert pendulum.Date(2019, 12, 30).week_of_month == 6
assert pendulum.Date(2019, 12, 31).week_of_month == 6
assert pendulum.Date(2020, 1, 7).week_of_month == 2
assert pendulum.Date(2020, 1, 14).week_of_month == 3
assert pendulum.Date(2021, 1, 1).week_of_month == 1
assert pendulum.Date(2021, 1, 2).week_of_month == 1
assert pendulum.Date(2021, 1, 9).week_of_month == 2
assert pendulum.Date(2021, 1, 10).week_of_month == 2
assert pendulum.Date(2021, 1, 15).week_of_month == 3
assert pendulum.Date(2021, 1, 16).week_of_month == 3
assert pendulum.Date(2021, 1, 17).week_of_month == 3
assert pendulum.Date(2021, 1, 23).week_of_month == 4
assert pendulum.Date(2021, 1, 30).week_of_month == 5
assert pendulum.Date(2021, 12, 19).week_of_month == 3
assert pendulum.Date(2021, 12, 25).week_of_month == 4
assert pendulum.Date(2021, 12, 26).week_of_month == 4
assert pendulum.Date(2021, 12, 29).week_of_month == 5
assert pendulum.Date(2021, 12, 30).week_of_month == 5
assert pendulum.Date(2021, 12, 31).week_of_month == 5
assert pendulum.Date(2022, 1, 1).week_of_month == 1
assert pendulum.Date(2022, 1, 3).week_of_month == 2
assert pendulum.Date(2022, 1, 10).week_of_month == 3
assert pendulum.Date(2023, 1, 1).week_of_month == 1
assert pendulum.Date(2023, 1, 2).week_of_month == 2
assert pendulum.Date(2029, 12, 31).week_of_month == 6


def test_week_of_year_first_week():
assert pendulum.Date(2012, 1, 1).week_of_year == 52
assert pendulum.Date(2012, 1, 2).week_of_year == 1
Expand Down