|
| 1 | +// Copyright (C) 2025 Igalia, S.L. All rights reserved. |
| 2 | +// This code is governed by the BSD license found in the LICENSE file. |
| 3 | + |
| 4 | +/*--- |
| 5 | +esid: sec-datetime-format-functions |
| 6 | +description: Different combinations of style options and Temporal types format correctly. |
| 7 | +locale: [en] |
| 8 | +features: [Temporal] |
| 9 | +---*/ |
| 10 | + |
| 11 | +const dateFormatterShort = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeZone: "Pacific/Apia" }); |
| 12 | +const dateFormatterMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeZone: "Pacific/Apia" }); |
| 13 | +const dateFormatterLong = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeZone: "Pacific/Apia" }); |
| 14 | +const dateFormatterFull = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeZone: "Pacific/Apia" }); |
| 15 | +const timeFormatterShort = new Intl.DateTimeFormat("en-US", { timeStyle: "short", timeZone: "Pacific/Apia" }); |
| 16 | +const timeFormatterMedium = new Intl.DateTimeFormat("en-US", { timeStyle: "medium", timeZone: "Pacific/Apia" }); |
| 17 | +const timeFormatterLong = new Intl.DateTimeFormat("en-US", { timeStyle: "long", timeZone: "Pacific/Apia" }); |
| 18 | +const timeFormatterFull = new Intl.DateTimeFormat("en-US", { timeStyle: "full", timeZone: "Pacific/Apia" }); |
| 19 | +const dateTimeFormatterShort = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "short", timeZone: "Pacific/Apia" }); |
| 20 | +const dateTimeFormatterMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "medium", timeZone: "Pacific/Apia" }); |
| 21 | +const dateTimeFormatterLong = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "long", timeZone: "Pacific/Apia" }); |
| 22 | +const dateTimeFormatterFull = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "full", timeZone: "Pacific/Apia" }); |
| 23 | +const dateTimeFormatterShortLong = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "long", timeZone: "Pacific/Apia" }); |
| 24 | +const dateTimeFormatterShortMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "medium", timeZone: "Pacific/Apia" }); |
| 25 | +const dateTimeFormatterShortFull = new Intl.DateTimeFormat("en-US", { dateStyle: "short", timeStyle: "full", timeZone: "Pacific/Apia" }); |
| 26 | +const dateTimeFormatterMediumLong = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "long", timeZone: "Pacific/Apia" }); |
| 27 | +const dateTimeFormatterMediumShort = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "short", timeZone: "Pacific/Apia" }); |
| 28 | +const dateTimeFormatterMediumFull = new Intl.DateTimeFormat("en-US", { dateStyle: "medium", timeStyle: "full", timeZone: "Pacific/Apia" }); |
| 29 | +const dateTimeFormatterLongMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "medium", timeZone: "Pacific/Apia" }); |
| 30 | +const dateTimeFormatterLongShort = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "short", timeZone: "Pacific/Apia" }); |
| 31 | +const dateTimeFormatterLongFull = new Intl.DateTimeFormat("en-US", { dateStyle: "long", timeStyle: "full", timeZone: "Pacific/Apia" }); |
| 32 | +const dateTimeFormatterFullMedium = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "medium", timeZone: "Pacific/Apia" }); |
| 33 | +const dateTimeFormatterFullShort = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "short", timeZone: "Pacific/Apia" }); |
| 34 | +const dateTimeFormatterFullLong = new Intl.DateTimeFormat("en-US", { dateStyle: "full", timeStyle: "long", timeZone: "Pacific/Apia" }); |
| 35 | + |
| 36 | +const date = new Temporal.PlainDate(2021, 8, 4); |
| 37 | +const datetime = new Temporal.PlainDateTime(2021, 8, 4, 0, 30, 45, 123, 456, 789); |
| 38 | +const monthday = new Temporal.PlainMonthDay(8, 4, "gregory"); |
| 39 | +const yearmonth = new Temporal.PlainYearMonth(2021, 8, "gregory"); |
| 40 | +const time = new Temporal.PlainTime(0, 30, 45, 123, 456, 789); |
| 41 | + |
| 42 | +// PlainDate |
| 43 | + |
| 44 | +var dateResult = dateFormatterShort.format(date); |
| 45 | +assert.sameValue(dateResult, "8/4/21", "plain date, dateStyle=short"); |
| 46 | +dateResult = dateFormatterMedium.format(date); |
| 47 | +assert.sameValue(dateResult, "Aug 4, 2021", "plain date, dateStyle=medium"); |
| 48 | +dateResult = dateFormatterLong.format(date); |
| 49 | +assert.sameValue(dateResult, "August 4, 2021", "plain date, dateStyle=long"); |
| 50 | +dateResult = dateFormatterFull.format(date); |
| 51 | +assert.sameValue(dateResult, "Wednesday, August 4, 2021", "plain date, dateStyle=full"); |
| 52 | +assert.throws(TypeError, () => timeFormatterShort.format(date), "plain date, timeStyle=short"); |
| 53 | +assert.throws(TypeError, () => timeFormatterMedium.format(date), "plain date, timeStyle=medium"); |
| 54 | +assert.throws(TypeError, () => timeFormatterLong.format(date), "plain date, timeStyle=long"); |
| 55 | +assert.throws(TypeError, () => timeFormatterFull.format(date), "plain date, timeStyle=full"); |
| 56 | +dateResult = dateTimeFormatterShort.format(date); |
| 57 | +assert.sameValue(dateResult, "8/4/21", "plain date, dateStyle = timeStyle = short"); |
| 58 | +dateResult = dateTimeFormatterMedium.format(date); |
| 59 | +assert.sameValue(dateResult, "Aug 4, 2021", "plain date, dateStyle = timeStyle = medium"); |
| 60 | +dateResult = dateTimeFormatterLong.format(date); |
| 61 | +assert.sameValue(dateResult, "August 4, 2021", "plain date, dateStyle = timeStyle = long"); |
| 62 | +dateResult = dateTimeFormatterFull.format(date); |
| 63 | +assert.sameValue(dateResult, "Wednesday, August 4, 2021", "plain date, dateStyle = timeStyle = full"); |
| 64 | +dateResult = dateTimeFormatterShortLong.format(date); |
| 65 | +assert.sameValue(dateResult, "8/4/21", "plain date, dateStyle = short, timeStyle = long"); |
| 66 | + |
| 67 | +// PlainDateTime |
| 68 | + |
| 69 | +var datetimeResult = dateFormatterShort.format(datetime); |
| 70 | +assert.sameValue(datetimeResult, "8/4/21", "plain datetime, dateStyle=short"); |
| 71 | +datetimeResult = dateFormatterMedium.format(datetime); |
| 72 | +assert.sameValue(datetimeResult, "Aug 4, 2021", "plain datetime, dateStyle=medium"); |
| 73 | +datetimeResult = dateFormatterLong.format(datetime); |
| 74 | +assert.sameValue(datetimeResult, "August 4, 2021", "plain datetime, dateStyle=long"); |
| 75 | +datetimeResult = dateFormatterFull.format(datetime); |
| 76 | +assert.sameValue(datetimeResult, "Wednesday, August 4, 2021", "plain datetime, dateStyle=full"); |
| 77 | +datetimeResult = timeFormatterShort.format(datetime); |
| 78 | +assert.sameValue(datetimeResult, "12:30 AM", "plain datetime, timeStyle=short"); |
| 79 | +datetimeResult = timeFormatterMedium.format(datetime); |
| 80 | +assert.sameValue(datetimeResult, "12:30:45 AM", "plain datetime, timeStyle=medium"); |
| 81 | +datetimeResult = timeFormatterLong.format(datetime); |
| 82 | +assert.sameValue(datetimeResult, "12:30:45 AM", "plain datetime, timeStyle=long"); |
| 83 | +datetimeResult = timeFormatterFull.format(datetime); |
| 84 | +assert.sameValue(datetimeResult, "12:30:45 AM", "plain datetime, timeStyle=full"); |
| 85 | +datetimeResult = dateTimeFormatterShort.format(datetime); |
| 86 | +assert.sameValue(datetimeResult, "8/4/21, 12:30 AM", "plain datetime, dateStyle = timeStyle = short"); |
| 87 | +datetimeResult = dateTimeFormatterMedium.format(datetime); |
| 88 | +assert.sameValue(datetimeResult, "Aug 4, 2021, 12:30:45 AM", "plain datetime, dateStyle = timeStyle = medium"); |
| 89 | +datetimeResult = dateTimeFormatterLong.format(datetime); |
| 90 | +assert.sameValue(datetimeResult, "August 4, 2021 at 12:30:45 AM", "plain datetime, dateStyle = timeStyle = long"); |
| 91 | +datetimeResult = dateTimeFormatterFull.format(datetime); |
| 92 | +assert.sameValue(datetimeResult, "Wednesday, August 4, 2021 at 12:30:45 AM", "plain datetime, dateStyle = timeStyle = full"); |
| 93 | +datetimeResult = dateTimeFormatterShortLong.format(datetime); |
| 94 | +assert.sameValue(datetimeResult, "8/4/2021, 12:30:45 AM", "plain datetime, dateStyle = short, timeStyle = long"); |
| 95 | +datetimeResult = dateTimeFormatterShortMedium.format(datetime); |
| 96 | +assert.sameValue(datetimeResult, "8/4/21, 12:30:45 AM", "plain datetime, dateStyle = short, timeStyle = medium"); |
| 97 | +datetimeResult = dateTimeFormatterShortFull.format(datetime); |
| 98 | +assert.sameValue(datetimeResult, "8/4/2021, 12:30:45 AM", "plain datetime, dateStyle = short, timeStyle = full"); |
| 99 | +datetimeResult = dateTimeFormatterMediumLong.format(datetime); |
| 100 | +assert.sameValue(datetimeResult, "Aug 4, 2021, 12:30:45 AM", "plain datetime, dateStyle = medium, timeStyle = long"); |
| 101 | +datetimeResult = dateTimeFormatterMediumShort.format(datetime); |
| 102 | +assert.sameValue(datetimeResult, "Aug 4, 2021, 12:30 AM", "plain datetime, dateStyle = medium, timeStyle = short"); |
| 103 | +datetimeResult = dateTimeFormatterMediumFull.format(datetime); |
| 104 | +assert.sameValue(datetimeResult, "Aug 4, 2021, 12:30:45 AM", "plain datetime, dateStyle = medium, timeStyle = full"); |
| 105 | +datetimeResult = dateTimeFormatterLongMedium.format(datetime); |
| 106 | +assert.sameValue(datetimeResult, "August 4, 2021 at 12:30:45 AM", "plain datetime, dateStyle = long, timeStyle = medium"); |
| 107 | +datetimeResult = dateTimeFormatterLongShort.format(datetime); |
| 108 | +assert.sameValue(datetimeResult, "August 4, 2021 at 12:30 AM", "plain datetime, dateStyle = long, timeStyle = short"); |
| 109 | +datetimeResult = dateTimeFormatterLongFull.format(datetime); |
| 110 | +assert.sameValue(datetimeResult, "August 4, 2021 at 12:30:45 AM", "plain datetime, dateStyle = long, timeStyle = full"); |
| 111 | +datetimeResult = dateTimeFormatterFullMedium.format(datetime); |
| 112 | +assert.sameValue(datetimeResult, "Wednesday, August 4, 2021 at 12:30:45 AM", "plain datetime, dateStyle = full, timeStyle = medium"); |
| 113 | +datetimeResult = dateTimeFormatterFullShort.format(datetime); |
| 114 | +assert.sameValue(datetimeResult, "Wednesday, August 4, 2021 at 12:30 AM", "plain datetime, dateStyle = full, timeStyle = short"); |
| 115 | +datetimeResult = dateTimeFormatterFullLong.format(datetime); |
| 116 | +assert.sameValue(datetimeResult, "Wednesday, August 4, 2021 at 12:30:45 AM", "plain datetime, dateStyle = full, timeStyle = long"); |
| 117 | + |
| 118 | +// PlainMonthDay |
| 119 | + |
| 120 | +var monthdayResult = dateFormatterShort.format(monthday); |
| 121 | +assert.sameValue(monthdayResult, "8/4", "plain monthday, dateStyle=short"); |
| 122 | +monthdayResult = dateFormatterMedium.format(monthday); |
| 123 | +assert.sameValue(monthdayResult, "Aug 4", "plain monthday, dateStyle=medium"); |
| 124 | +monthdayResult = dateFormatterLong.format(monthday); |
| 125 | +assert.sameValue(monthdayResult, "August 4", "plain monthday, dateStyle=long"); |
| 126 | +monthdayResult = dateFormatterFull.format(monthday); |
| 127 | +assert.sameValue(monthdayResult, "August 4", "plain monthday, dateStyle=full"); |
| 128 | +assert.throws(TypeError, () => timeFormatterShort.format(monthday), "plain monthday, timeStyle=short"); |
| 129 | +assert.throws(TypeError, () => timeFormatterMedium.format(monthday), "plain monthday, timeStyle=medium"); |
| 130 | +assert.throws(TypeError, () => timeFormatterLong.format(monthday), "plain monthday, timeStyle=long"); |
| 131 | +assert.throws(TypeError, () => timeFormatterFull.format(monthday), "plain monthday, timeStyle=full"); |
| 132 | +monthdayResult = dateTimeFormatterShort.format(monthday); |
| 133 | +assert.sameValue(monthdayResult, "8/4", "plain monthday, dateStyle = timeStyle = short"); |
| 134 | +monthdayResult = dateTimeFormatterMedium.format(monthday); |
| 135 | +assert.sameValue(monthdayResult, "Aug 4", "plain monthday, dateStyle = timeStyle = medium"); |
| 136 | +monthdayResult = dateTimeFormatterLong.format(monthday); |
| 137 | +assert.sameValue(monthdayResult, "August 4", "plain monthday, dateStyle = timeStyle = long"); |
| 138 | +monthdayResult = dateTimeFormatterFull.format(monthday); |
| 139 | +assert.sameValue(monthdayResult, "August 4", "plain monthday, dateStyle = timeStyle = full"); |
| 140 | +monthdayResult = dateTimeFormatterShortLong.format(monthday); |
| 141 | +assert.sameValue(monthdayResult, "8/4", "plain monthday, dateStyle = short, timeStyle = long"); |
| 142 | + |
| 143 | +// PlainYearMonth |
| 144 | + |
| 145 | +var yearmonthResult = dateFormatterShort.format(yearmonth); |
| 146 | +assert.sameValue(yearmonthResult, "8/21", "plain yearmonth, dateStyle=short"); |
| 147 | +yearmonthResult = dateFormatterMedium.format(yearmonth); |
| 148 | +assert.sameValue(yearmonthResult, "Aug 2021", "plain yearmonth, dateStyle=medium"); |
| 149 | +yearmonthResult = dateFormatterLong.format(yearmonth); |
| 150 | +assert.sameValue(yearmonthResult, "August 2021", "plain yearmonth, dateStyle=long"); |
| 151 | +yearmonthResult = dateFormatterFull.format(yearmonth); |
| 152 | +assert.sameValue(yearmonthResult, "August 2021", "plain yearmonth, dateStyle=full"); |
| 153 | +assert.throws(TypeError, () => timeFormatterShort.format(yearmonth), "plain yearmonth, timeStyle=short"); |
| 154 | +assert.throws(TypeError, () => timeFormatterMedium.format(yearmonth), "plain yearmonth, timeStyle=medium"); |
| 155 | +assert.throws(TypeError, () => timeFormatterLong.format(yearmonth), "plain yearmonth, timeStyle=long"); |
| 156 | +assert.throws(TypeError, () => timeFormatterFull.format(yearmonth), "plain yearmonth, timeStyle=full"); |
| 157 | +yearmonthResult = dateTimeFormatterShort.format(yearmonth); |
| 158 | +assert.sameValue(yearmonthResult, "8/21", "plain yearmonth, dateStyle = timeStyle = short"); |
| 159 | +yearmonthResult = dateTimeFormatterMedium.format(yearmonth); |
| 160 | +assert.sameValue(yearmonthResult, "Aug 2021", "plain yearmonth, dateStyle = timeStyle = medium"); |
| 161 | +yearmonthResult = dateTimeFormatterLong.format(yearmonth); |
| 162 | +assert.sameValue(yearmonthResult, "August 2021", "plain yearmonth, dateStyle = timeStyle = long"); |
| 163 | +yearmonthResult = dateTimeFormatterFull.format(yearmonth); |
| 164 | +assert.sameValue(yearmonthResult, "August 2021", "plain yearmonth, dateStyle = timeStyle = full"); |
| 165 | +yearmonthResult = dateTimeFormatterShortLong.format(yearmonth); |
| 166 | +assert.sameValue(yearmonthResult, "8/21", "plain yearmonth, dateStyle = short, timeStyle = long"); |
| 167 | + |
| 168 | +// PlainTime |
| 169 | + |
| 170 | +assert.throws(TypeError, () => dateFormatterShort.format(time), "plain time, dateStyle=short"); |
| 171 | +assert.throws(TypeError, () => dateFormatterMedium.format(time), "plain time, dateStyle=medium"); |
| 172 | +assert.throws(TypeError, () => dateFormatterLong.format(time), "plain time, dateStyle=long"); |
| 173 | +var timeResult = timeFormatterShort.format(time); |
| 174 | +assert.sameValue(timeResult, "12:30 AM", "plain time, dateStyle=short"); |
| 175 | +timeResult = timeFormatterMedium.format(time); |
| 176 | +assert.sameValue(timeResult, "12:30:45 AM", "plain time, dateStyle=medium"); |
| 177 | +timeResult = timeFormatterLong.format(time); |
| 178 | +assert.sameValue(timeResult, "12:30:45 AM", "plain time, dateStyle=long"); |
| 179 | +timeResult = dateTimeFormatterShort.format(time); |
| 180 | +assert.sameValue(timeResult, "12:30 AM", "plain time, dateStyle = timeStyle = short"); |
| 181 | +timeResult = dateTimeFormatterMedium.format(time); |
| 182 | +assert.sameValue(timeResult, "12:30:45 AM", "plain time, dateStyle = timeStyle = medium"); |
| 183 | +timeResult = dateTimeFormatterLong.format(time); |
| 184 | +assert.sameValue(timeResult, "12:30:45 AM", "plain time, dateStyle = timeStyle = long"); |
| 185 | +timeResult = dateTimeFormatterFull.format(time); |
| 186 | +assert.sameValue(timeResult, "12:30:45 AM", "plain time, dateStyle = timeStyle = full"); |
| 187 | +timeResult = dateTimeFormatterShortLong.format(time); |
| 188 | +assert.sameValue(timeResult, "12:30:45 AM", "plain time, dateStyle = short, timeStyle = long"); |
0 commit comments