| 1 |
// |reftest| shell-option(--enable-temporal) skip-if(!this.hasOwnProperty('Temporal')||!xulRuntime.shell) -- Temporal is not enabled unconditionally, requires shell-options |
| 2 |
// Copyright (C) 2023 Igalia, S.L. All rights reserved. |
| 3 |
// This code is governed by the BSD license found in the LICENSE file. |
| 4 |
|
| 5 |
/*--- |
| 6 |
esid: sec-temporal.calendar.prototype.yearmonthfromfields |
| 7 |
description: Reference ISO day is chosen to be the first of the calendar month |
| 8 |
info: | |
| 9 |
6.d. Perform ! CreateDataPropertyOrThrow(_fields_, *"day"*, *1*<sub>𝔽</sub>). |
| 10 |
e. Let _result_ be ? CalendarDateToISO(_calendar_.[[Identifier]], _fields_, _options_). |
| 11 |
includes: [temporalHelpers.js] |
| 12 |
features: [Temporal] |
| 13 |
---*/ |
| 14 |
|
| 15 |
// Month codes, month indices, and the ISO reference days of the months in 2022 |
| 16 |
const months2022TestData = [ |
| 17 |
// TODO: Sources conflict over whether M01L and M12L exist in _any_ year. |
| 18 |
// Clarify this, and delete if appropriate. ICU has them, but may be wrong. |
| 19 |
// |
| 20 |
// - ICU4C and ICU4X both have M01L in Gregorian year 1651. |
| 21 |
// - ICU4C has M12L in Gregorian year 1889, but ICU4X doesn't. |
| 22 |
// - ICU4X has M12L in Gregorian year 1403, but ICU4C doesn't. |
| 23 |
|
| 24 |
["M01", 1, 1], |
| 25 |
["M02", 2, 3], |
| 26 |
["M03", 3, 1], |
| 27 |
["M04", 4, 1], |
| 28 |
["M05", 5, 30], |
| 29 |
["M06", 6, 29], |
| 30 |
["M07", 7, 29], |
| 31 |
["M08", 8, 27], |
| 32 |
["M09", 9, 26], |
| 33 |
["M10", 10, 25], |
| 34 |
["M11", 11, 24], |
| 35 |
["M12", 12, 23], |
| 36 |
]; |
| 37 |
for (let [nonLeapMonthCode, month, referenceISODay] of months2022TestData) { |
| 38 |
// Allow implementation-defined "epoch year" for the Chinese calendar. |
| 39 |
const year = new Temporal.PlainDate(2022, 3, 1).withCalendar("chinese").year; |
| 40 |
const leapMonthCode = nonLeapMonthCode + "L"; |
| 41 |
const fields = { year, monthCode: leapMonthCode, calendar: "chinese" }; |
| 42 |
|
| 43 |
const result = Temporal.PlainYearMonth.from(fields, { overflow: "constrain" }); |
| 44 |
|
| 45 |
// CalendarDateToISO ( calendar, fields, overflow ) |
| 46 |
// |
| 47 |
// > If the month is a leap month that doesn't exist in the year, pick another |
| 48 |
// > date according to the cultural conventions of that calendar's users. |
| 49 |
// > Usually this will result in the same day in the month before or after |
| 50 |
// > where that month would normally fall in a leap year. |
| 51 |
// |
| 52 |
// Without clear information in which direction the month has to be adjusted, |
| 53 |
// we have to allow two possible implementations: |
| 54 |
// 1. The previous month is used, i.e. "M01L" is constrained to "M01". |
| 55 |
// 2. The next month is used, i.e. "M01L" is constrained to "M02". |
| 56 |
if (result.month !== month) { |
| 57 |
assert.sameValue(result.month, month + 1); |
| 58 |
|
| 59 |
// Adjust nonLeapMonthCode, month, referenceISODay using the data from the |
| 60 |
// next month. |
| 61 |
const nextMonth = months2022TestData.find(e => e[1] === month + 1); |
| 62 |
[nonLeapMonthCode, month, referenceISODay] = nextMonth; |
| 63 |
} |
| 64 |
|
| 65 |
TemporalHelpers.assertPlainYearMonth( |
| 66 |
result, |
| 67 |
year, month, nonLeapMonthCode, |
| 68 |
`Chinese intercalary month ${leapMonthCode} does not exist in year 2022 (overflow constrain)`, |
| 69 |
/* era = */ undefined, /* era year = */ undefined, referenceISODay |
| 70 |
); |
| 71 |
|
| 72 |
assert.throws( |
| 73 |
RangeError, |
| 74 |
() => Temporal.PlainYearMonth.from(fields, { overflow: "reject" }), |
| 75 |
`Chinese intercalary month ${leapMonthCode} does not exist in year 2022 (overflow reject)` |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
// Years in which leap months exist according to ICU4C/ICU4X. |
| 80 |
const leapMonthsTestData = [ |
| 81 |
["M01L", 1651, 2, 20], |
| 82 |
["M02L", 2023, 3, 22], |
| 83 |
["M03L", 1993, 4, 22], |
| 84 |
["M04L", 2020, 5, 23], |
| 85 |
["M05L", 2009, 6, 23], |
| 86 |
["M06L", 2017, 7, 23], |
| 87 |
["M07L", 2006, 8, 24], |
| 88 |
["M08L", 1995, 9, 25], |
| 89 |
["M09L", 2014, 10, 24], |
| 90 |
["M10L", 1984, 11, 23], |
| 91 |
["M11L", 1517, 12, 23], |
| 92 |
]; |
| 93 |
for (const [monthCode, relatedYear, month, referenceISODay, isoYear = relatedYear, isoMonth = month] of leapMonthsTestData) { |
| 94 |
// Allow implementation-defined "epoch year" for the Chinese calendar. |
| 95 |
const year = new Temporal.PlainDate(relatedYear, 3, 1).withCalendar("chinese").year; |
| 96 |
const result = Temporal.PlainYearMonth.from({ year, monthCode, calendar: "chinese" }); |
| 97 |
TemporalHelpers.assertPlainYearMonth( |
| 98 |
result, |
| 99 |
year, month, monthCode, |
| 100 |
`Date of sample Chinese intercalary month ${monthCode}`, |
| 101 |
/* era = */ undefined, /* era year = */ undefined, referenceISODay |
| 102 |
); |
| 103 |
const isoYearMonth = result.toString().slice(0, 7); |
| 104 |
assert.sameValue(isoYearMonth, `${isoYear}-${String(isoMonth).padStart(2, '0')}`, `${year}-${monthCode} starts in ISO month ${isoYear}-${isoMonth}`); |
| 105 |
} |
| 106 |
|
| 107 |
reportCompare(0, 0); |