| 1 |
// |reftest| shell-option(--enable-temporal) skip-if(!this.hasOwnProperty('Temporal')||!xulRuntime.shell) -- Temporal is not enabled unconditionally, requires shell-options |
| 2 |
// Copyright (C) 2025 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.plainyearmonth.from |
| 7 |
description: Test remapping behaviour of regnal eras |
| 8 |
info: | |
| 9 |
CalendarYearMonthFromFields: |
| 10 |
1. Perform ? CalendarResolveFields(_calendar_, _fields_, ~year-month~). |
| 11 |
2. Let _firstDayIndex_ be the 1-based index of the first day of the month |
| 12 |
described by _fields_ (i.e., 1 unless the month's first day is skipped by |
| 13 |
this calendar.) |
| 14 |
3. Set _fields_.[[Day]] to _firstDayIndex_. |
| 15 |
4. Let result be ? CalendarDateToISO(_calendar_, _fields_, _overflow_). |
| 16 |
|
| 17 |
CalendarResolveFields: |
| 18 |
When the fields of _fields_ are inconsistent with respect to a non-unset |
| 19 |
_fields_.[[Era]], it is recommended that _fields_.[[Era]] and |
| 20 |
_fields_.[[EraYear]] be updated to resolve the inconsistency by lenient |
| 21 |
interpretation of out-of-bounds values (rather than throwing a *RangeError*), |
| 22 |
which is particularly useful for consistent interpretation of dates in |
| 23 |
calendars with regnal eras. |
| 24 |
|
| 25 |
includes: [temporalHelpers.js] |
| 26 |
features: [Temporal] |
| 27 |
---*/ |
| 28 |
|
| 29 |
// Based on a test originally by André Bargull <[email protected]> |
| 30 |
|
| 31 |
// Notes: |
| 32 |
// - "heisei" era started January 8, 1989. |
| 33 |
// - "reiwa" era started May 1, 2019. |
| 34 |
|
| 35 |
for (const overflow of ["constrain", "reject"]) { |
| 36 |
function test(fields) { |
| 37 |
return Temporal.PlainYearMonth.from({ ...fields, calendar: "japanese" }, { overflow }); |
| 38 |
} |
| 39 |
|
| 40 |
// Reiwa era started in month 5 of the year, so the era of month 1 is remapped |
| 41 |
// to be the correct one for the month. |
| 42 |
TemporalHelpers.assertPlainYearMonth( |
| 43 |
test({ era: "reiwa", eraYear: 1, monthCode: "M01" }), |
| 44 |
2019, 1, "M01", |
| 45 |
"Reiwa 1 before May is mapped to Heisei 31", |
| 46 |
"heisei", 31, /* reference day = */ 1 |
| 47 |
); |
| 48 |
|
| 49 |
// Reiwa era started on the first day of the month, so the reference day 1 |
| 50 |
// does not need remapping. |
| 51 |
TemporalHelpers.assertPlainYearMonth( |
| 52 |
test({ era: "reiwa", eraYear: 1, monthCode: "M05" }), |
| 53 |
2019, 5, "M05", |
| 54 |
"reference day is 1", |
| 55 |
"reiwa", 1, /* reference day = */ 1 |
| 56 |
); |
| 57 |
|
| 58 |
// Heisei era started on the eighth day of the month, but PlainYearMonth |
| 59 |
// references the first day of the month. So the era is remapped to be the |
| 60 |
// correct one for the reference day. |
| 61 |
TemporalHelpers.assertPlainYearMonth( |
| 62 |
test({ era: "heisei", eraYear: 1, monthCode: "M01" }), |
| 63 |
1989, 1, "M01", |
| 64 |
"01-01 Heisei 1 is remapped to 01-01 Showa 64", |
| 65 |
"showa", 64, /* reference day = */ 1 |
| 66 |
); |
| 67 |
|
| 68 |
// Era year past the end of the Heisei era is remapped to Reiwa era |
| 69 |
TemporalHelpers.assertPlainYearMonth( |
| 70 |
test({ era: "heisei", eraYear: 37, monthCode: "M04" }), |
| 71 |
2025, 4, "M04", |
| 72 |
"Heisei 37 is remapped to Reiwa 7", |
| 73 |
"reiwa", 7, /* reference day = */ 1 |
| 74 |
); |
| 75 |
|
| 76 |
// Zero year in a 1-based era is remapped to the previous era |
| 77 |
TemporalHelpers.assertPlainYearMonth( |
| 78 |
test({ era: "reiwa", eraYear: 0, monthCode: "M04" }), |
| 79 |
2018, 4, "M04", |
| 80 |
"Reiwa 0 is remapped to Heisei 30", |
| 81 |
"heisei", 30, /* reference day = */ 1 |
| 82 |
); |
| 83 |
|
| 84 |
// Negative year in a forwards-counting era is remapped to the previous era |
| 85 |
TemporalHelpers.assertPlainYearMonth( |
| 86 |
test({ era: "reiwa", eraYear: -20, monthCode: "M04" }), |
| 87 |
1998, 4, "M04", |
| 88 |
"Reiwa -20 is remapped to Heisei 10", |
| 89 |
"heisei", 10, /* reference day = */ 1 |
| 90 |
); |
| 91 |
|
| 92 |
// Test the last two things for Gregorian eras as well |
| 93 |
function testGregorian(fields) { |
| 94 |
return Temporal.PlainYearMonth.from({ ...fields, calendar: "gregory" }, { overflow }); |
| 95 |
} |
| 96 |
TemporalHelpers.assertPlainYearMonth( |
| 97 |
testGregorian({ era: "ce", eraYear: 0, monthCode: "M04" }), |
| 98 |
0, 4, "M04", |
| 99 |
"0 CE is remapped to 1 BCE", |
| 100 |
"bce", 1, /* reference day = */ 1 |
| 101 |
); |
| 102 |
TemporalHelpers.assertPlainYearMonth( |
| 103 |
testGregorian({ era: "ce", eraYear: -20, monthCode: "M04" }), |
| 104 |
-20, 4, "M04", |
| 105 |
"-20 CE is remapped to 21 BCE", |
| 106 |
"bce", 21, /* reference day = */ 1 |
| 107 |
); |
| 108 |
|
| 109 |
// Zero year in a backwards-counting era is remapped to the next era |
| 110 |
TemporalHelpers.assertPlainYearMonth( |
| 111 |
testGregorian({ era: "bce", eraYear: 0, monthCode: "M04" }), |
| 112 |
1, 4, "M04", |
| 113 |
"0 BCE is remapped to 1 CE", |
| 114 |
"ce", 1, /* reference day = */ 1 |
| 115 |
); |
| 116 |
|
| 117 |
// Negative year in a backwards-counting era is remapped to the next era |
| 118 |
TemporalHelpers.assertPlainYearMonth( |
| 119 |
testGregorian({ era: "bce", eraYear: -20, monthCode: "M04" }), |
| 120 |
21, 4, "M04", |
| 121 |
"-20 BCE is remapped to 21 CE", |
| 122 |
"ce", 21, /* reference day = */ 1 |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
reportCompare(0, 0); |