| 1 |
// |reftest| skip-if(!this.hasOwnProperty("Intl")) |
| 2 |
// Any copyright is dedicated to the Public Domain. |
| 3 |
// http://creativecommons.org/licenses/publicdomain/ |
| 4 |
|
| 5 |
// Tests the format function with a diverse set of locales and options. |
| 6 |
// Always use UTC to avoid dependencies on test environment. |
| 7 |
|
| 8 |
const { |
| 9 |
Era, Year, Month, Weekday, Day, DayPeriod, Hour, Minute, Second, Literal |
| 10 |
} = DateTimeFormatParts |
| 11 |
|
| 12 |
var format; |
| 13 |
var date = Date.UTC(2012, 11, 17, 3, 0, 42); |
| 14 |
|
| 15 |
// Locale en-US; default options. |
| 16 |
format = new Intl.DateTimeFormat("en-us", {timeZone: "UTC"}); |
| 17 |
assertParts(format, date, [ |
| 18 |
Month("12"), Literal("/"), Day("17"), Literal("/"), Year("2012"), |
| 19 |
]); |
| 20 |
|
| 21 |
// Just date |
| 22 |
format = new Intl.DateTimeFormat("en-us", { |
| 23 |
year: 'numeric', |
| 24 |
month: 'numeric', |
| 25 |
day: 'numeric', |
| 26 |
timeZone: "UTC"}); |
| 27 |
assertParts(format, date, [ |
| 28 |
Month("12"), Literal("/"), Day("17"), Literal("/"), Year("2012"), |
| 29 |
]); |
| 30 |
|
| 31 |
// Just time in hour24 |
| 32 |
format = new Intl.DateTimeFormat("en-us", { |
| 33 |
hour: 'numeric', |
| 34 |
minute: 'numeric', |
| 35 |
second: 'numeric', |
| 36 |
hour12: false, |
| 37 |
timeZone: "UTC"}); |
| 38 |
assertParts(format, date, [ |
| 39 |
Hour("03"), Literal(":"), Minute("00"), Literal(":"), Second("42"), |
| 40 |
]); |
| 41 |
|
| 42 |
// Just time in hour12 |
| 43 |
format = new Intl.DateTimeFormat("en-us", { |
| 44 |
hour: 'numeric', |
| 45 |
minute: 'numeric', |
| 46 |
second: 'numeric', |
| 47 |
hour12: true, |
| 48 |
timeZone: "UTC"}); |
| 49 |
assertParts(format, date, [ |
| 50 |
Hour("3"), Literal(":"), Minute("00"), Literal(":"), Second("42"), Literal(" "), DayPeriod("AM"), |
| 51 |
]); |
| 52 |
|
| 53 |
// Just month. |
| 54 |
format = new Intl.DateTimeFormat("en-us", { |
| 55 |
month: "narrow", |
| 56 |
timeZone: "UTC"}); |
| 57 |
assertParts(format, date, [ |
| 58 |
Month("D"), |
| 59 |
]); |
| 60 |
|
| 61 |
// Just weekday. |
| 62 |
format = new Intl.DateTimeFormat("en-us", { |
| 63 |
weekday: "narrow", |
| 64 |
timeZone: "UTC"}); |
| 65 |
assertParts(format, date, [ |
| 66 |
Weekday("M"), |
| 67 |
]); |
| 68 |
|
| 69 |
// Year and era. |
| 70 |
format = new Intl.DateTimeFormat("en-us", { |
| 71 |
year: "numeric", |
| 72 |
era: "short", |
| 73 |
timeZone: "UTC"}); |
| 74 |
assertParts(format, date, [ |
| 75 |
Year("2012"), Literal(" "), Era("AD"), |
| 76 |
]); |
| 77 |
|
| 78 |
// Time and date |
| 79 |
format = new Intl.DateTimeFormat("en-us", { |
| 80 |
weekday: 'long', |
| 81 |
year: 'numeric', |
| 82 |
month: 'numeric', |
| 83 |
day: 'numeric', |
| 84 |
hour: 'numeric', |
| 85 |
minute: 'numeric', |
| 86 |
second: 'numeric', |
| 87 |
hour12: true, |
| 88 |
timeZone: "UTC"}); |
| 89 |
assertParts(format, date, [ |
| 90 |
Weekday("Monday"), Literal(", "), Month("12"), Literal("/"), Day("17"), Literal("/"), Year("2012"), |
| 91 |
Literal(", "), |
| 92 |
Hour("3"), Literal(":"), Minute("00"), Literal(":"), Second("42"), Literal(" "), DayPeriod("AM"), |
| 93 |
]); |
| 94 |
|
| 95 |
if (typeof reportCompare === "function") |
| 96 |
reportCompare(0, 0, 'ok'); |