Skip to content

Commit

Permalink
Fix week of month calculation (#446)
Browse files Browse the repository at this point in the history
  • Loading branch information
sdispater authored Mar 6, 2020
1 parent 9af9efe commit 00b7fdd
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pendulum/date.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,9 @@ def days_in_month(self):

@property
def week_of_month(self):
return int(math.ceil(self.day / DAYS_PER_WEEK))
first_day_of_month = self.replace(day=1)

return self.week_of_year - first_day_of_month.week_of_year + 1

@property
def age(self):
Expand Down
7 changes: 5 additions & 2 deletions tests/date/test_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,13 @@ def test_is_long_year():

def test_week_of_month():
assert pendulum.Date(2012, 9, 30).week_of_month == 5
assert pendulum.Date(2012, 9, 28).week_of_month == 4
assert pendulum.Date(2012, 9, 20).week_of_month == 3
assert pendulum.Date(2012, 9, 28).week_of_month == 5
assert pendulum.Date(2012, 9, 20).week_of_month == 4
assert pendulum.Date(2012, 9, 8).week_of_month == 2
assert pendulum.Date(2012, 9, 1).week_of_month == 1
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


def test_week_of_year_first_week():
Expand Down
7 changes: 5 additions & 2 deletions tests/datetime/test_getters.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,10 +185,13 @@ def test_is_long_year():

def test_week_of_month():
assert pendulum.datetime(2012, 9, 30).week_of_month == 5
assert pendulum.datetime(2012, 9, 28).week_of_month == 4
assert pendulum.datetime(2012, 9, 20).week_of_month == 3
assert pendulum.datetime(2012, 9, 28).week_of_month == 5
assert pendulum.datetime(2012, 9, 20).week_of_month == 4
assert pendulum.datetime(2012, 9, 8).week_of_month == 2
assert pendulum.datetime(2012, 9, 1).week_of_month == 1
assert pendulum.datetime(2020, 1, 1).week_of_month == 1
assert pendulum.datetime(2020, 1, 7).week_of_month == 2
assert pendulum.datetime(2020, 1, 14).week_of_month == 3


def test_week_of_year_first_week():
Expand Down

0 comments on commit 00b7fdd

Please sign in to comment.