Skip to content

Conversation

@petersalomonsen
Copy link
Collaborator

Problem

The monthly interval increment function would panic when incrementing from dates like January 31st to February, because February doesn't have 31 days.

Solution

Use chrono's checked_add_months method which automatically handles day overflow by clamping to the last valid day of the target month.

Changes

  • Updated Interval::increment() to use checked_add_months(Months::new(1))
  • Added comprehensive unit tests covering edge cases:
    • Jan 31 → Feb 29 (leap year)
    • Jan 31 → Feb 28 (non-leap year)
    • Mar 31 → Apr 30
    • May 31 → Jun 30
    • Jan 29-30 → Feb (both leap and non-leap years)

Testing

All 13 unit tests pass:

cargo test interval_increment --lib

Use chrono's checked_add_months to properly handle cases where the target
month has fewer days than the current day (e.g., Jan 31 -> Feb 29).

The function now clamps to the last valid day of the target month:
- Jan 31 -> Feb 29 (leap year) or Feb 28 (non-leap)
- Mar 31 -> Apr 30
- May 31 -> Jun 30

Added comprehensive unit tests covering all edge cases.
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR fixes a panic in the monthly interval increment function when handling end-of-month date overflow scenarios (e.g., January 31st → February). The solution uses chrono's built-in checked_add_months method which automatically clamps to the last valid day of the target month.

Key Changes:

  • Replaced manual month/year boundary handling with chrono's checked_add_months(Months::new(1))
  • Added comprehensive unit tests covering 13 edge cases including leap years, various month-end dates, and year boundaries
  • Updated documentation to clarify day overflow clamping behavior

@petersalomonsen petersalomonsen changed the title Fix monthly interval increment to handle day overflow fix: handle day overflow in monthly interval increment Jan 3, 2026
@akorchyn akorchyn merged commit a0c8bc6 into main Jan 5, 2026
1 check passed
@petersalomonsen petersalomonsen deleted the fix/monthly-interval-day-clamping branch January 5, 2026 11:19
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.

3 participants