Fix: week_of_month returns negative values#718
Closed
ulasozguler wants to merge 1 commit intopython-pendulum:masterfrom
Closed
Fix: week_of_month returns negative values#718ulasozguler wants to merge 1 commit intopython-pendulum:masterfrom
ulasozguler wants to merge 1 commit intopython-pendulum:masterfrom
Conversation
sdispater
requested changes
Aug 6, 2023
| 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 |
Collaborator
There was a problem hiding this comment.
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 |
Collaborator
There was a problem hiding this comment.
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 |
Collaborator
There was a problem hiding this comment.
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 |
Collaborator
There was a problem hiding this comment.
Suggested change
| assert pendulum.date(2023, 1, 2).week_of_month == 2 | |
| assert pendulum.date(2023, 1, 2).week_of_month == 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Pull Request Check List
Description
Resolves #587
Changes
Using day of month instead of
week_of_yearto calculateweek_of_month.