roundtrip-from-iso.js (3010B)
1 // |reftest| skip-if(!this.hasOwnProperty('Temporal')) -- Temporal is not enabled unconditionally 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.plaindate.from 7 description: > 8 Check that various dates created from calculated properties have the expected 9 properties 10 includes: [temporalHelpers.js] 11 features: [Temporal, Intl.Era-monthcode] 12 ---*/ 13 14 const options = { overflow: "reject" }; 15 16 const calendars = [ 17 "buddhist", 18 "chinese", 19 "coptic", 20 "dangi", 21 "ethioaa", 22 "ethiopic", 23 "gregory", 24 "hebrew", 25 "indian", 26 "islamic-civil", 27 "islamic-tbla", 28 "islamic-umalqura", 29 "japanese", 30 "persian", 31 "roc", 32 ]; 33 34 for (const calendar of calendars) { 35 const year2000 = new Temporal.PlainDate(2000, 1, 1, calendar); 36 testRoundtrip(year2000, calendar, "ISO date 2000-01-01"); 37 const year1 = new Temporal.PlainDate(1, 1, 1, calendar); 38 testRoundtrip(year1, calendar, "ISO date 0001-01-01"); 39 } 40 41 // Additional cases that were moved in from staging tests, or that we add to 42 // catch regressions 43 const additionalCases = [ 44 ["indian", 2000, 12, 31, "https://github.com/unicode-org/icu4x/issues/4914"], 45 ]; 46 47 for (const [calendar, isoYear, isoMonth, isoDay, descr] of additionalCases) { 48 const date = new Temporal.PlainDate(isoYear, isoMonth, isoDay, calendar); 49 testRoundtrip(date, calendar, descr); 50 } 51 52 function testRoundtrip(date, calendar, descr) { 53 const dateFromYearMonth = Temporal.PlainDate.from({ 54 calendar, 55 year: date.year, 56 month: date.month, 57 day: date.day, 58 }); 59 TemporalHelpers.assertPlainDate( 60 dateFromYearMonth, 61 date.year, date.month, date.monthCode, date.day, 62 `${descr} (${calendar}) - created from year and month`, 63 date.era, date.eraYear); 64 65 const dateFromYearMonthCode = Temporal.PlainDate.from({ 66 calendar, 67 year: date.year, 68 monthCode: date.monthCode, 69 day: date.day, 70 }); 71 TemporalHelpers.assertPlainDate( 72 dateFromYearMonthCode, 73 date.year, date.month, date.monthCode, date.day, 74 `${descr} (${calendar}) - created from year and month code`, 75 date.era, date.eraYear); 76 77 if (date.era === undefined) return; // skip era-less calendars 78 79 const dateFromEraMonth = Temporal.PlainDate.from({ 80 calendar, 81 era: date.era, 82 eraYear: date.eraYear, 83 month: date.month, 84 day: date.day, 85 }); 86 TemporalHelpers.assertPlainDate( 87 dateFromEraMonth, 88 date.year, date.month, date.monthCode, date.day, 89 `${descr} (${calendar}) - created from era, era year, and month`, 90 date.era, date.eraYear); 91 92 const dateFromEraMonthCode = Temporal.PlainDate.from({ 93 calendar, 94 era: date.era, 95 eraYear: date.eraYear, 96 monthCode: date.monthCode, 97 day: date.day, 98 }); 99 TemporalHelpers.assertPlainDate( 100 dateFromEraMonthCode, 101 date.year, date.month, date.monthCode, date.day, 102 `${descr} (${calendar}) - created from era, era year, and month code`, 103 date.era, date.eraYear); 104 } 105 106 reportCompare(0, 0);