summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/Intl/DateTimeFormat/formatToParts.js
blob: 62f0d694062e2184391e4d9514b1b0a86bdb5376 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
// |reftest| skip-if(!this.hasOwnProperty("Intl"))
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/licenses/publicdomain/

// Tests the format function with a diverse set of locales and options.
// Always use UTC to avoid dependencies on test environment.

const {
  Era, Year, Month, Weekday, Day, DayPeriod, Hour, Minute, Second, Literal
} = DateTimeFormatParts

var format;
var date = Date.UTC(2012, 11, 17, 3, 0, 42);

// Locale en-US; default options.
format = new Intl.DateTimeFormat("en-us", {timeZone: "UTC"});
assertParts(format, date, [
  Month("12"), Literal("/"), Day("17"), Literal("/"), Year("2012"),
]);

// Just date
format = new Intl.DateTimeFormat("en-us", {
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  timeZone: "UTC"});
assertParts(format, date, [
  Month("12"), Literal("/"), Day("17"), Literal("/"), Year("2012"),
]);

// Just time in hour24
format = new Intl.DateTimeFormat("en-us", {
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: false,
  timeZone: "UTC"});
assertParts(format, date, [
  Hour("03"), Literal(":"), Minute("00"), Literal(":"), Second("42"),
]);

// Just time in hour12
format = new Intl.DateTimeFormat("en-us", {
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: true,
  timeZone: "UTC"});
assertParts(format, date, [
  Hour("3"), Literal(":"), Minute("00"), Literal(":"), Second("42"), Literal(" "), DayPeriod("AM"),
]);

// Just month.
format = new Intl.DateTimeFormat("en-us", {
  month: "narrow",
  timeZone: "UTC"});
assertParts(format, date, [
  Month("D"),
]);

// Just weekday.
format = new Intl.DateTimeFormat("en-us", {
  weekday: "narrow",
  timeZone: "UTC"});
assertParts(format, date, [
  Weekday("M"),
]);

// Year and era.
format = new Intl.DateTimeFormat("en-us", {
  year: "numeric",
  era: "short",
  timeZone: "UTC"});
assertParts(format, date, [
  Year("2012"), Literal(" "), Era("AD"),
]);

// Time and date
format = new Intl.DateTimeFormat("en-us", {
  weekday: 'long',
  year: 'numeric',
  month: 'numeric',
  day: 'numeric',
  hour: 'numeric',
  minute: 'numeric',
  second: 'numeric',
  hour12: true,
  timeZone: "UTC"});
assertParts(format, date, [
  Weekday("Monday"), Literal(", "), Month("12"), Literal("/"), Day("17"), Literal("/"), Year("2012"),
  Literal(", "),
  Hour("3"), Literal(":"), Minute("00"), Literal(":"), Second("42"), Literal(" "), DayPeriod("AM"),
]);

if (typeof reportCompare === "function")
    reportCompare(0, 0, 'ok');