Skip to content

Commit

Permalink
Fixed span_range omitting dates when frame is 'month', exact is True,…
Browse files Browse the repository at this point in the history
… and start.day is 31 (arrow-py#1185)
  • Loading branch information
rkendra committed Dec 5, 2024
1 parent 68634d5 commit 3024950
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
10 changes: 10 additions & 0 deletions arrow/arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,17 @@ def span_range(
yield r.span(frame, bounds=bounds, exact=exact)

for r in _range:
day_is_clipped = False
floor, ceil = r.span(frame, bounds=bounds, exact=exact)
next = ceil.shift(microseconds=+1)
if frame == "month" and next.day < start.day:
day_is_clipped = True
if day_is_clipped and not next._is_last_day_of_month(next):
days_to_shift = (
min(start.day, calendar.monthrange(next.year, next.month)[1])
- next.day
)
ceil = ceil.shift(days=days_to_shift)
if ceil > end:
ceil = end
if bounds[1] == ")":
Expand Down
16 changes: 15 additions & 1 deletion tests/test_arrow.py
Original file line number Diff line number Diff line change
Expand Up @@ -1185,7 +1185,7 @@ def test_month(self):
(arrow.Arrow(2013, 4, 1), arrow.Arrow(2013, 4, 30, 23, 59, 59, 999999)),
]

def test_month_end(self):
def test_month_exact(self):
result = list(
arrow.Arrow.span_range(
"month", datetime(2013, 1, 31), datetime(2014, 1, 31), exact=True
Expand All @@ -1207,6 +1207,20 @@ def test_month_end(self):
(arrow.Arrow(2013, 12, 31), arrow.Arrow(2014, 1, 30, 23, 59, 59, 999999)),
]

def test_month_exact_leap(self):
result = list(
arrow.Arrow.span_range(
"month", datetime(2012, 1, 31), datetime(2012, 5, 31), exact=True
)
)

assert result == [
(arrow.Arrow(2012, 1, 31), arrow.Arrow(2012, 2, 28, 23, 59, 59, 999999)),
(arrow.Arrow(2012, 2, 29), arrow.Arrow(2012, 3, 30, 23, 59, 59, 999999)),
(arrow.Arrow(2012, 3, 31), arrow.Arrow(2012, 4, 29, 23, 59, 59, 999999)),
(arrow.Arrow(2012, 4, 30), arrow.Arrow(2012, 5, 30, 23, 59, 59, 999999)),
]

def test_week(self):
result = list(
arrow.Arrow.span_range("week", datetime(2013, 2, 2), datetime(2013, 2, 28))
Expand Down

0 comments on commit 3024950

Please sign in to comment.