tor-browser

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

field-widths.js (3764B)


      1 // |reftest| skip-if(!this.hasOwnProperty("Intl"))
      2 
      3 const date = new Date(Date.UTC(2021, 1-1, 2, 3, 4, 5, 678));
      4 
      5 const tests = [
      6  // Standalone 'hour' field.
      7  {
      8    options: {
      9      hour: "numeric",
     10    },
     11    locales: {
     12      "en": "3 AM",
     13      "de": "03 Uhr",
     14    },
     15  },
     16  {
     17    options: {
     18      hour: "2-digit",
     19    },
     20    locales: {
     21      "en": "03 AM",
     22      "de": "03 Uhr",
     23    },
     24  },
     25 
     26  // Standalone 'minute' field.
     27  {
     28    options: {
     29      minute: "numeric",
     30    },
     31    locales: {
     32      "en": "4",
     33      "de": "4",
     34    },
     35  },
     36  {
     37    options: {
     38      minute: "2-digit",
     39    },
     40    locales: {
     41      "en": "04",
     42      "de": "04",
     43    },
     44  },
     45 
     46  // Standalone 'second' field.
     47  {
     48    options: {
     49      second: "numeric",
     50    },
     51    locales: {
     52      "en": "5",
     53      "de": "5",
     54    },
     55  },
     56  {
     57    options: {
     58      second: "2-digit",
     59    },
     60    locales: {
     61      "en": "05",
     62      "de": "05",
     63    },
     64  },
     65 
     66  // 'hour' and 'minute' fields with all possible field width combinations.
     67  {
     68    options: {
     69      hour: "numeric",
     70      minute: "numeric",
     71    },
     72    locales: {
     73      "en": "3:04 AM",
     74      "de": "03:04",
     75    },
     76  },
     77  {
     78    options: {
     79      hour: "numeric",
     80      minute: "2-digit",
     81    },
     82    locales: {
     83      "en": "3:04 AM",
     84      "de": "03:04",
     85    },
     86  },
     87  {
     88    options: {
     89      hour: "2-digit",
     90      minute: "numeric",
     91    },
     92    locales: {
     93      "en": "03:04 AM",
     94      "de": "03:04",
     95    },
     96  },
     97  {
     98    options: {
     99      hour: "2-digit",
    100      minute: "2-digit",
    101    },
    102    locales: {
    103      "en": "03:04 AM",
    104      "de": "03:04",
    105    },
    106  },
    107 
    108  // 'minute' and 'second' fields with all possible field width combinations.
    109  {
    110    options: {
    111      minute: "numeric",
    112      second: "numeric",
    113    },
    114    locales: {
    115      "en": "04:05",
    116      "de": "04:05",
    117    },
    118  },
    119  {
    120    options: {
    121      minute: "numeric",
    122      second: "2-digit",
    123    },
    124    locales: {
    125      "en": "04:05",
    126      "de": "04:05",
    127    },
    128  },
    129  {
    130    options: {
    131      minute: "2-digit",
    132      second: "numeric",
    133    },
    134    locales: {
    135      "en": "04:05",
    136      "de": "04:05",
    137    },
    138  },
    139  {
    140    options: {
    141      minute: "2-digit",
    142      second: "2-digit",
    143    },
    144    locales: {
    145      "en": "04:05",
    146      "de": "04:05",
    147    },
    148  },
    149 
    150  // Test 'hour' and 'minute' with 'hourCycle=h12'.
    151  {
    152    options: {
    153      hour: "numeric",
    154      minute: "numeric",
    155      hourCycle: "h12",
    156    },
    157    locales: {
    158      "en": "3:04 AM",
    159      "de": "3:04 AM",
    160    },
    161  },
    162  {
    163    options: {
    164      hour: "2-digit",
    165      minute: "2-digit",
    166      hourCycle: "h12",
    167    },
    168    locales: {
    169      "en": "03:04 AM",
    170      "de": "03:04 AM",
    171    },
    172  },
    173 
    174  // Test 'hour' and 'minute' with 'hourCycle=h23'.
    175  {
    176    options: {
    177      hour: "numeric",
    178      minute: "numeric",
    179      hourCycle: "h23",
    180    },
    181    locales: {
    182      "en": "03:04",
    183      "de": "03:04",
    184    },
    185  },
    186  {
    187    options: {
    188      hour: "2-digit",
    189      minute: "2-digit",
    190      hourCycle: "h23",
    191    },
    192    locales: {
    193      "en": "03:04",
    194      "de": "03:04",
    195    },
    196  },
    197 ];
    198 
    199 for (let {options, locales} of tests) {
    200  for (let [locale, expected] of Object.entries(locales)) {
    201    let dtf = new Intl.DateTimeFormat(locale, {timeZone: "UTC", ...options});
    202    assertEq(dtf.format(date), expected);
    203  }
    204 }
    205 
    206 const toLocaleTests = {
    207  "en": "1/2/2021, 3:04:05 AM",
    208  "de": "2.1.2021, 03:04:05",
    209 };
    210 
    211 for (let [locale, expected] of Object.entries(toLocaleTests)) {
    212  assertEq(date.toLocaleString(locale, {timeZone: "UTC"}), expected);
    213 }
    214 
    215 const toLocaleTimeTests = {
    216  "en": "3:04:05 AM",
    217  "de": "03:04:05",
    218 };
    219 
    220 for (let [locale, expected] of Object.entries(toLocaleTimeTests)) {
    221  assertEq(date.toLocaleTimeString(locale, {timeZone: "UTC"}), expected);
    222 }
    223 
    224 if (typeof reportCompare === "function")
    225  reportCompare(0, 0, "ok");