or
https://bugs.webkit.org/show_bug.cgi?id=331726
In the indian calendar, Temporal.PlainDate.prototype.add() and subtract() with years or months throw
indian
Temporal.PlainDate.prototype.add()
subtract()
RangeError: Result of calendar date addition is outside representable range
when the result is the last day of a month whose first day is on or before ISO −004713-11-24 (Julian Day Number 0). That is Saka year −4792 and earlier, and months M01 to M09 of Saka −4791. until() and since() with largestUnit "years" throw across such dates too.
until()
since()
"years"
These dates are about 267,000 years inside the Temporal range. from(), with() and day-only add() produce them without error, so the dates themselves are valid and representable. Results that are not the last day of a month are correct.
from()
with()
add()
Steps to reproduce (paste into the Web Inspector console):
const indian = (year, monthCode, day) => Temporal.PlainDate.from({ calendar: "indian", year, monthCode, day }); const show = (d) => `${d.year} ${d.monthCode} ${d.day} = ${d.toString()}`; const tryShow = (f) => { try { return show(f()); } catch (e) { return `${e.name}: ${e.message}`; } }; console.log("1", tryShow(() => indian(-4792, "M01", 30).add({ years: 1 }))); console.log("2", tryShow(() => indian(-4792, "M09", 30).add({ years: 1 }))); console.log("3", tryShow(() => indian(-4791, "M08", 30).add({ months: 1 }))); console.log("4", tryShow(() => indian(-4791, "M02", 31).subtract({ months: 1 }))); console.log("5", tryShow(() => indian(-4878, "M01", 31).add({ years: 1 }))); console.log("6", tryShow(() => indian(-10002, "M06", 31).add({ months: 1 }))); try { console.log("7", Temporal.PlainDate.from("-271821-04-20[u-ca=indian]") .until("-271819-06-27[u-ca=indian]", { largestUnit: "years" }).toString()); } catch (e) { console.log("7", `${e.name}: ${e.message}`); } // The target dates themselves are valid and reachable in other ways: console.log("8", show(indian(-4791, "M01", 30)), indian(-4791, "M01", 1).daysInMonth); console.log("9", show(indian(-4792, "M01", 30).with({ year: -4791 }))); console.log("10", show(indian(-4792, "M01", 30).add({ days: 365 }))); // Controls: one year later, or day 29, works. console.log("11", tryShow(() => indian(-4791, "M01", 30).add({ years: 1 }))); console.log("12", tryShow(() => indian(-4792, "M01", 29).add({ years: 1 })));
Actual results (WebKit 27.2):
1 RangeError: Result of calendar date addition is outside representable range 2 RangeError: Result of calendar date addition is outside representable range 3 RangeError: Result of calendar date addition is outside representable range 4 RangeError: Result of calendar date addition is outside representable range 5 RangeError: Result of calendar date addition is outside representable range 6 RangeError: Result of calendar date addition is outside representable range 7 RangeError: Result of calendar date addition is outside representable range 8 -4791 M01 30 = -004713-04-20[u-ca=indian] 30 9 -4791 M01 30 = -004713-04-20[u-ca=indian] 10 -4791 M01 30 = -004713-04-20[u-ca=indian] 11 -4790 M01 30 = -004712-04-19[u-ca=indian] 12 -4791 M01 29 = -004713-04-19[u-ca=indian]
Expected results:
1 -4791 M01 30 = -004713-04-20[u-ca=indian] 2 -4791 M09 30 = -004713-12-21[u-ca=indian] 3 -4791 M09 30 = -004713-12-21[u-ca=indian] 4 -4791 M01 30 = -004713-04-20[u-ca=indian] 5 -4877 M01 30 = -004799-04-20[u-ca=indian] 6 -10002 M07 30 = -009924-10-22[u-ca=indian] 7 P2Y2M7D 8 -4791 M01 30 = -004713-04-20[u-ca=indian] 30 9 -4791 M01 30 = -004713-04-20[u-ca=indian] 10 -4791 M01 30 = -004713-04-20[u-ca=indian] 11 -4790 M01 30 = -004712-04-19[u-ca=indian] 12 -4791 M01 29 = -004713-04-19[u-ca=indian]
Node 26.12.1 (V8 14.6.202.34, ICU 78.3) and Chromium 153.0.8010.12 print exactly these expected lines. Lines 8 to 12 show that WebKit itself agrees on the target dates.
Why the expected values are right:
NonISODateAdd in the Intl era and monthCode proposal (https://tc39.es/proposal-intl-era-monthcode/, sup-temporal-nonisodateadd) adds the years and months, keeps the month code, and sets the day to the smaller of the source day and the days in the target month. For line 1 that is Saka −4791, M01, min(30, 30) = 30. It then converts to ISO with CalendarIntegersToISO.
sup-temporal-nonisodateadd
The steps throw only for overflow "reject" when the day does not fit, or when the ISO result fails ISODateWithinLimits. −004713-04-20 is well within the limits, so the add must succeed. until() and since() call the same add while they balance, so line 7 must succeed too.
"reject"
Cause:
JSC sends indian-calendar year and month arithmetic to fixedSolarDateAdd in Source/JavaScriptCore/runtime/temporal/core/CalendarICUBridge.cpp. It sets the target fields on an ICU4C calendar and then calls verifyAndCorrectFixedSolarDay to check them.
fixedSolarDateAdd
Source/JavaScriptCore/runtime/temporal/core/CalendarICUBridge.cpp
verifyAndCorrectFixedSolarDay
ICU4C resolves those fields one day late for months that start on or before Julian Day Number 0. IndianCalendar::handleComputeMonthStart in i18n/indiancal.cpp returns IndianToJD(...), a double that ends in .5, converted to an integer:
IndianCalendar::handleComputeMonthStart
i18n/indiancal.cpp
IndianToJD(...)
int64_t jd = IndianToJD(eyear ,imonth, 1); // ICU main return (int32_t)jd; // ICU 74.2
The conversion truncates toward zero, so for a negative Julian day it rounds up instead of down. Querying ICU4C 78.3 directly: setting Saka −4791 M01 day 1 gives ISO −4713-03-23 instead of −4713-03-22, and setting M01 day 30 gives M02 day 1.
verifyAndCorrectFixedSolarDay fixes a one-day error only when ICU's year and month already match the expected ones. When the target is the last day of the month, the late day falls in the next month. The check fails, and fixedSolarDateAdd throws the RangeError above. On other days the late day stays in the same month and is corrected, which is why only month-end results fail. The comment next to that call says this case is expected only at ICU's extreme millisecond boundary. These dates are far from that boundary.
nonISOCalendarDateToISO already avoids the same ICU4C problem for from() and with(), through indianSakaToISO (the ICU4C-WORKAROUND: rdar://182960658 comment). fixedSolarDateAdd does not use it.
nonISOCalendarDateToISO
indianSakaToISO
ICU4C-WORKAROUND: rdar://182960658
Suggested fix:
In fixedSolarDateAdd, for the indian calendar, compute the regulated target (Saka year, month, day) as now and convert it with indianSakaToISO, the same helper nonISOCalendarDateToISO uses, instead of setting the fields on the ICU calendar.
The root fix is in ICU4C: IndianCalendar::handleComputeMonthStart should floor the Julian day instead of truncating it.
Environment:
The ICU side is filed as https://unicode-org.atlassian.net/browse/ICU-23627 (same truncation in ICU4C and ICU4J).