-
Notifications
You must be signed in to change notification settings - Fork 1
Description
Add a third overflow type on PlainDate.add, PlainDate.subtract... that will return the "day after" when the date does not exists.
| Start Date | Duration | Actual Result | Wanted Result |
|---|---|---|---|
| 2024-01-01 | P1M | 2024-02-01 | ✔️ 2024-02-01 |
| 2024-01-15 | P1M | 2024-01-15 | ✔️ 2024-01-15 |
| 2024-01-31 | P1M | 2024-02-28 | |
| 2024-01-31 | P1M1D | 2024-03-01 | ✔️ 2024-03-01 |
| 2024-02-29 | P2Y | 2026-02-28 |
Advantages:
The actual way to achieve the goal is using a try-catch which is not really intuitive.
try {
Temporal.PlainDate.from('2024-01-31')
.add('P1M', { overflow: 'reject' })
.toString()
// throw RangeError
// with { overflow: 'constrain' } the result is '2024-02-29'
} catch(e) {
Temporal.PlainDate.from('2024-01-31')
.add('P1M1D', { overflow: 'constrain' })
.toString()
// '2024-03-01'
}This can be useful for statistics, when you want to group them by month with different anchor dates and the day generated by the + P1M should not be included in the range.
Meaning 1 month starting from 2024-01-31 must include 2024-02-28 and one month starting from 2024-01-01 must not include 2024-02-01.
Concerns:
None
Prior art:
I check the behaviour on Java/Kotlin (with LocalDate) and rust (with chrono). They both have the same behavior as { overflow: 'constrain' }. Even the reject behavior does not exists when adding a month.
Constraints / corner cases:
For an anchor day at the end of a month e.g. 2024-01-31, the result table I want should be something like:
| Start Date | Duration | End Date |
|---|---|---|
| 2024-01-31 | +P1M |
2024-03-01 |
| 2024-01-31 | +P2M |
2024-03-31 |
| 2024-01-31 | +P3M |
2024-05-01 |
| 2024-01-31 | +P4M |
2024-05-31 |