Hi! I’ve been looking at calendar arithmetic involving leap-day dates and recurring anniversary-style periods.
Date.add(years=1) correctly truncates invalid target dates, e.g. Date(2024, 2, 29).add(years=1) becomes Date(2025, 2, 28). However, for contract-like schedules, bond interest periods, birthdays, anniversaries, etc., there is a subtly different operation: computing the nth occurrence from the original anchor date.
For example, if a contract starts on 2024-02-29, the expected yearly boundaries might be:
n = 0 -> 2024-02-29
n = 1 -> 2025-02-28
n = 2 -> 2026-02-28
n = 3 -> 2027-02-28
n = 4 -> 2028-02-29
This works if each occurrence is calculated from the original date:
>>> whenever.Date(2024, 2, 29).add(years=4)
Date("2028-02-29")
But it does not work if one year is repeatedly added to the previous occurrence (e.g. in a loop), because the original leap-day anchor is lost after the first truncation:
>>> whenever.Date(2024, 2, 29).add(years=1).add(years=1).add(years=1).add(years=1)
Date("2028-02-28")
Would you be open to either documenting this pattern or considering an API for anchored calendar recurrences? I’m not sure whether this belongs in core API, but it feels like an easy pitfall for financial/legal/subscription-style schedules.
Hi! I’ve been looking at calendar arithmetic involving leap-day dates and recurring anniversary-style periods.
Date.add(years=1)correctly truncates invalid target dates, e.g.Date(2024, 2, 29).add(years=1)becomesDate(2025, 2, 28). However, for contract-like schedules, bond interest periods, birthdays, anniversaries, etc., there is a subtly different operation: computing the nth occurrence from the original anchor date.For example, if a contract starts on
2024-02-29, the expected yearly boundaries might be:This works if each occurrence is calculated from the original date:
But it does not work if one year is repeatedly added to the previous occurrence (e.g. in a loop), because the original leap-day anchor is lost after the first truncation:
Would you be open to either documenting this pattern or considering an API for anchored calendar recurrences? I’m not sure whether this belongs in core API, but it feels like an easy pitfall for financial/legal/subscription-style schedules.