tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

hour-cycle.js (1753B)


      1 // |reftest| skip-if(!this.hasOwnProperty("Temporal")||!this.hasOwnProperty("Intl"))
      2 
      3 // Test hour cycle options (hour12 and hourCycle) are correctly set when
      4 // formatting Temporal types.
      5 
      6 const locales = [
      7  "en",
      8  "de",
      9  "fr",
     10  "es",
     11  "ar",
     12  "th",
     13  "zh",
     14  "ja",
     15 ];
     16 
     17 const options = [
     18  {hour12: true},
     19  {hour12: false},
     20  {hourCycle: "h11"},
     21  {hourCycle: "h12"},
     22  {hourCycle: "h23"},
     23  {hourCycle: "h24"},
     24 ];
     25 
     26 const timeZone = "UTC";
     27 
     28 let date = new Date(0);
     29 let instant = date.toTemporalInstant();
     30 let zonedDateTime = instant.toZonedDateTimeISO(timeZone);
     31 let plainDateTime = zonedDateTime.toPlainDateTime();
     32 let plainTime = zonedDateTime.toPlainTime();
     33 
     34 for (let locale of locales) {
     35  for (let opts of options) {
     36    assertEq(
     37      instant.toLocaleString(locale, {timeZone, ...opts}),
     38      date.toLocaleString(locale, {timeZone, ...opts})
     39    );
     40    assertEq(
     41      zonedDateTime.toLocaleString(locale, {...opts}),
     42      date.toLocaleString(locale, {timeZone, timeZoneName: "short", ...opts})
     43    );
     44    assertEq(
     45      plainDateTime.toLocaleString(locale, {timeZone, ...opts}),
     46      date.toLocaleString(locale, {timeZone, ...opts})
     47    );
     48    assertEq(
     49      plainTime.toLocaleString(locale, {timeZone, ...opts}),
     50      date.toLocaleTimeString(locale, {timeZone, ...opts})
     51    );
     52 
     53    let dtf = new Intl.DateTimeFormat(locale, {
     54      hour: "2-digit",
     55      minute: "2-digit",
     56      timeZone,
     57      ...opts,
     58    });
     59 
     60    assertEq(dtf.format(instant), dtf.format(date));
     61    assertEq(dtf.format(plainDateTime), dtf.format(date));
     62    assertEq(dtf.format(plainTime), dtf.format(date));
     63    assertThrowsInstanceOf(() => dtf.format(zonedDateTime), TypeError);
     64  }
     65 }
     66 
     67 if (typeof reportCompare === "function")
     68  reportCompare(true, true);