Truly independent web browser
LibJS: Balance large non-ISO date offsets without walking month by month
Shannon Booth0f7de082885d
diff --git a/Libraries/LibJS/Rust/src/runtime/temporal/calendar.rs b/Libraries/LibJS/Rust/src/runtime/temporal/calendar.rsindex 2ab05e90fbb..e18948389a9 100644--- a/Libraries/LibJS/Rust/src/runtime/temporal/calendar.rs+++ b/Libraries/LibJS/Rust/src/runtime/temporal/calendar.rs@@ -2482,6 +2482,9 @@ pub fn calendar_months_in_year(calendar: Utf16View<'_>, arithmetic_year: i32) ->unicode_calendar::calendar_months_in_year(&calendar_for_unicode(calendar), arithmetic_year)}+/// Further from year 0 than the arithmetic year of any date within the limits of Temporal, in any calendar.+const BALANCED_YEAR_LIMIT: i64 = 300_000;+// 4.1.16 BalanceNonISODate ( calendar, arithmeticYear, ordinalMonth, day ), https://tc39.es/proposal-intl-era-monthcode/#sec-temporal-balancenonisodatepub fn balance_non_iso_date(calendar: Utf16View<'_>,@@ -2498,6 +2501,22 @@ pub fn balance_non_iso_date(// 2. Let resolvedMonth be ordinalMonth.let mut resolved_month = ordinal_month;+ // AD-HOC: A year has at most 13 months and 385 days in every calendar, so once the months or the days alone move the+ // year past any year of a date within the limits of Temporal, the result is out of range whatever it+ // balances to, and walking there month by month can take very long.+ let years_moved_by_months = i64::from(resolved_month) / 13;+ let years_moved_by_days = i64::from(day) / 385;+ if years_moved_by_months.signum() * years_moved_by_days.signum() >= 0 {+ let year_at_least = i64::from(resolved_year) + years_moved_by_months + years_moved_by_days;+ if year_at_least.abs() > BALANCED_YEAR_LIMIT {+ return BalancedDate {+ year: ((BALANCED_YEAR_LIMIT + 1) * year_at_least.signum()) as i32,+ month: 1,+ day: 1,+ };+ }+ }+// 3. Let monthsInYear be CalendarMonthsInYear(calendar, resolvedYear).let mut months_in_year = months_in_year_of(resolved_year);@@ -2531,6 +2550,24 @@ pub fn balance_non_iso_date(// 7. Let daysInMonth be CalendarDaysInMonth(calendar, resolvedYear, resolvedMonth).let mut days_in_month = days_in_month_of(resolved_year, resolved_month);+ // OPTIMIZATION: Steps 8 and 9 move resolvedDay - 1 days on from the first day of the month one month at a time,+ // which takes very long for a count of many years of days. Adding them to the ISO date of the first+ // day of the month finds the same date directly.+ if resolved_day.unsigned_abs() > 400 {+ let unicode_calendar_name = calendar_for_unicode(calendar);+ if let Some(first_day_of_month) =+ unicode_calendar::calendar_date_to_iso_date(&unicode_calendar_name, resolved_year, resolved_month as u8, 1)+ {+ let iso_date = add_days_to_iso_date(first_day_of_month, f64::from(resolved_day) - 1.0);+ let date = unicode_calendar::iso_date_to_calendar_date(&unicode_calendar_name, iso_date);+ return BalancedDate {+ year: date.year,+ month: date.month,+ day: date.day,+ };+ }+ }+// 8. Repeat, while resolvedDay ≤ 0,while resolved_day <= 0 {// a. Set resolvedMonth to resolvedMonth - 1.diff --git a/Tests/LibJS/Runtime/builtins/Temporal/PlainDate/PlainDate.prototype.add.js b/Tests/LibJS/Runtime/builtins/Temporal/PlainDate/PlainDate.prototype.add.jsindex 99716666ba0..b8d07067ec0 100644--- a/Tests/LibJS/Runtime/builtins/Temporal/PlainDate/PlainDate.prototype.add.js+++ b/Tests/LibJS/Runtime/builtins/Temporal/PlainDate/PlainDate.prototype.add.js@@ -42,3 +42,24 @@ describe("errors", () => {}).toThrowWithMessage(TypeError, "Not an object of type Temporal.PlainDate");});});++test("adding far beyond the limits in a lunisolar calendar throws without walking there", () => {+ const plainDate = Temporal.PlainDate.from("2000-01-01[u-ca=chinese]");+ expect(() => {+ plainDate.add({ months: 1e9 });+ }).toThrow(RangeError);+ expect(() => {+ plainDate.add({ days: 1e8 });+ }).toThrow(RangeError);+});++test("adding days in any calendar gives the date that many days later", () => {+ const plainDate = Temporal.PlainDate.from("2000-01-31");+ for (const calendar of ["chinese", "hebrew", "islamic-umalqura", "ethiopic", "japanese", "persian"]) {+ for (const days of [401, -401, 123456, -123456]) {+ expect(plainDate.withCalendar(calendar).add({ days }).toString()).toBe(+ plainDate.add({ days }).withCalendar(calendar).toString()+ );+ }+ }+});