Repository navigation
grass.temporal: Clamp day when adding months or years - #8091
bhuvan-somisetty wants to merge 1 commit into
Conversation
Adding months or years to a date replaced only the month and year and kept the day, so 2001-01-31 + 1 month became an invalid 2001-02-31 and raised ValueError. The same happened for decrements and for February 29 plus a year. Clamp the day to the last day of the target month instead. Fixes OSGeo#8090
124946f to
9aa65da
Compare
|
Here is my response AI response: Thanks for the investigation. I'm going to close this one, though, because clamping changes what a month step means in ways that are worse than the current error. The main problem shows up in callers that step through time. There are also two issues in the change itself: months and years are clamped separately and summed as day counts, so Month-end starts with month or year increments are rare and nobody has reported hitting this in practice, so I'd rather keep rejecting them than clamp. If someone has a real workflow that needs month-end monthly stepping, that would be a good reason to revisit, but it needs a decision about how the stepping callers derive their dates, not just a change inside |
Description
When
modify_datetime()adds or subtracts months or years, it now clamps the day to the last day of the target month. So Jan 31 + 1 month gives Feb 28 (Feb 29 in a leap year), and Feb 29 + 1 year gives Feb 28. Before, it kept the day as is and blew up withValueError: day is out of range for month.Motivation and context
Fixes #8090. This goes through
increment_datetime_by_string()/decrement_datetime_by_string(), so anything that steps through time by months or years (t.registerwithincrement=,t.rast.accumulate, etc.) could crash on end-of-month dates.How has this been tested?
Added cases to
grass_temporal_datetime_math_test.pyfor month-end and Feb 29, both forward and backward. They fail on main and pass with this change. The existing datetime_math tests and doctests still pass.Screenshots (if appropriate)
Same calls on main vs. this branch:
Types of changes
functionality to not work as before)
Checklist
pre-defined prefixes
of this project.