Skip to content

Fix: week_of_month returns negative values#718

Closed
ulasozguler wants to merge 1 commit intopython-pendulum:masterfrom
ulasozguler:master
Closed

Fix: week_of_month returns negative values#718
ulasozguler wants to merge 1 commit intopython-pendulum:masterfrom
ulasozguler:master

Conversation

@ulasozguler
Copy link
Copy Markdown

Pull Request Check List

  • Added tests for changed code.
  • Updated documentation for changed code.

Description

Resolves #587

Changes

Using day of month instead of week_of_year to calculate week_of_month.

assert pendulum.date(2020, 1, 1).week_of_month == 1
assert pendulum.date(2020, 1, 7).week_of_month == 2
assert pendulum.date(2020, 1, 14).week_of_month == 3
assert pendulum.date(2023, 1, 1).week_of_month == 1
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not correct, January 1st 2023 belongs to the last week of December of 2022. Since this is the edge case, it should return 0.

assert pendulum.date(2020, 1, 14).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(2023, 1, 31).week_of_month == 6
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This is not correct, from an ISO standpoint it's not possible to have a 6th week, it should be 5.

Comment on lines +87 to +91
week_days = days_of_week()
first_day_index = week_days.index(first_day_of_month.day_of_week)
days_in_first_week = pendulum.DAYS_PER_WEEK - first_day_index
days_after_first_week = self.day - days_in_first_week
return math.ceil(days_after_first_week / pendulum.DAYS_PER_WEEK) + 1
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
week_days = days_of_week()
first_day_index = week_days.index(first_day_of_month.day_of_week)
days_in_first_week = pendulum.DAYS_PER_WEEK - first_day_index
days_after_first_week = self.day - days_in_first_week
return math.ceil(days_after_first_week / pendulum.DAYS_PER_WEEK) + 1
return int(
math.ceil((self.day + first_day_of_month.day_of_week - 1) / DAYS_PER_WEEK)
)

This should give us what we want while taking care of the edge cases that needs fixing.

assert pendulum.date(2020, 1, 7).week_of_month == 2
assert pendulum.date(2020, 1, 14).week_of_month == 3
assert pendulum.date(2023, 1, 1).week_of_month == 1
assert pendulum.date(2023, 1, 2).week_of_month == 2
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
assert pendulum.date(2023, 1, 2).week_of_month == 2
assert pendulum.date(2023, 1, 2).week_of_month == 1

sdispater added a commit that referenced this pull request Dec 14, 2023
@sdispater sdispater closed this in c811ecd Dec 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

week_of_month in pendulum_parse for Timestamp '2021-01-15 14:00:00' returns -50

2 participants