Debian Codegraph @

js/src/builtin/Date.js

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 #if JS_HAS_INTL_API
6 // This cache, once primed, has these properties:
7 //
8 // locale:
9 // The requested locale, either |undefined| or a string. Not necessarily a
10 // valid and supported DateTimeFormat locale.
11 // runtimeDefaultLocale:
12 // Locale information provided by the embedding, guiding SpiderMonkey's
13 // selection of a default locale. See intl_RuntimeDefaultLocale(), whose
14 // value controls the value returned by DefaultLocale() that's what's
15 // *actually* used.
16 // icuDefaultTimeZone:
17 // Time zone information provided by ICU. See intl_defaultTimeZone(),
18 // whose value controls the value returned by DefaultTimeZone() that's
19 // what's *actually* used.
20 // formatters:
21 // A Record storing formatters consistent with the above locale and time
22 // zone values, for use with the appropriate toLocale*String Date method
23 // when called with the |locales| argument either |undefined| or a string,
24 // and the |options| argument having the value |undefined|.
25 //
26 // The "formatters" Record has (some subset of) these properties, as determined
27 // by all values of the first argument passed to |GetCachedFormat|:
28 //
29 // dateTimeFormat: for Date's toLocaleString operation
30 // dateFormat: for Date's toLocaleDateString operation
31 // timeFormat: for Date's toLocaleTimeString operation
32 //
33 // Using this cache, then, requires
34 // 1) verifying the requested locale is consistent with the cached value, then
35 // 2) verifying the current runtimeDefaultLocale/icuDefaultTimeZone are
36 // consistent with cached values, then
37 // 3) seeing if the desired formatter is cached and returning it if so, or else
38 // 4) create the desired formatter and store and return it.
39 var dateTimeFormatCache = new_Record();
40
41 /**
42 * Get a cached DateTimeFormat formatter object, created like so:
43 *
44 * CreateDateTimeFormat(locale, undefined, required, defaults);
45 *
46 * |format| must be a key from the "formatters" Record described above.
47 */
48 function GetCachedFormat(locale, format, required, defaults) {
49 assert(
50 locale === undefined || typeof locale === "string",
51 "bad locale type"
52 );
53 assert(
54 format === "dateTimeFormat" ||
55 format === "dateFormat" ||
56 format === "timeFormat",
57 "unexpected format key: please update the comment by dateTimeFormatCache"
58 );
59
60 var formatters;
61 if (
62 locale !== dateTimeFormatCache.locale ||
63 !intl_IsRuntimeDefaultLocale(dateTimeFormatCache.runtimeDefaultLocale) ||
64 !intl_isDefaultTimeZone(dateTimeFormatCache.icuDefaultTimeZone)
65 ) {
66 formatters = dateTimeFormatCache.formatters = new_Record();
67 dateTimeFormatCache.locale = locale;
68 dateTimeFormatCache.runtimeDefaultLocale = intl_RuntimeDefaultLocale();
69 dateTimeFormatCache.icuDefaultTimeZone = intl_defaultTimeZone();
70 } else {
71 formatters = dateTimeFormatCache.formatters;
72 }
73
74 var fmt = formatters[format];
75 if (fmt === undefined) {
76 fmt = formatters[format] = intl_CreateDateTimeFormat(locale, undefined, required, defaults);
77 }
78
79 return fmt;
80 }
81
82 /**
83 * Format this Date object into a date and time string, using the locale and
84 * formatting options provided.
85 *
86 * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.5.
87 * Spec: ECMAScript Internationalization API Specification, 13.3.1.
88 */
89 function Date_toLocaleString() {
90 // Steps 1-2.
91 var x = callFunction(ThisTimeValue, this, DATE_METHOD_LOCALE_STRING);
92 if (Number_isNaN(x)) {
93 return "Invalid Date";
94 }
95
96 // Steps 3-4.
97 var locales = ArgumentsLength() ? GetArgument(0) : undefined;
98 var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
99
100 // Step 5-6.
101 var dateTimeFormat;
102 if (
103 (locales === undefined || typeof locales === "string") &&
104 options === undefined
105 ) {
106 // This cache only optimizes when |options| isn't used.
107 dateTimeFormat = GetCachedFormat(locales, "dateTimeFormat", "any", "all");
108 } else {
109 dateTimeFormat = intl_CreateDateTimeFormat(locales, options, "any", "all");
110 }
111
112 // Step 7.
113 return intl_FormatDateTime(dateTimeFormat, x, /* formatToParts = */ false);
114 }
115
116 /**
117 * Format this Date object into a date string, using the locale and formatting
118 * options provided.
119 *
120 * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.6.
121 * Spec: ECMAScript Internationalization API Specification, 13.3.2.
122 */
123 function Date_toLocaleDateString() {
124 // Steps 1-2.
125 var x = callFunction(ThisTimeValue, this, DATE_METHOD_LOCALE_DATE_STRING);
126 if (Number_isNaN(x)) {
127 return "Invalid Date";
128 }
129
130 // Steps 3-4.
131 var locales = ArgumentsLength() ? GetArgument(0) : undefined;
132 var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
133
134 // Step 5-6.
135 var dateTimeFormat;
136 if (
137 (locales === undefined || typeof locales === "string") &&
138 options === undefined
139 ) {
140 // This cache only optimizes when |options| isn't used.
141 dateTimeFormat = GetCachedFormat(locales, "dateFormat", "date", "date");
142 } else {
143 dateTimeFormat = intl_CreateDateTimeFormat(locales, options, "date", "date");
144 }
145
146 // Step 7.
147 return intl_FormatDateTime(dateTimeFormat, x, /* formatToParts = */ false);
148 }
149
150 /**
151 * Format this Date object into a time string, using the locale and formatting
152 * options provided.
153 *
154 * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.7.
155 * Spec: ECMAScript Internationalization API Specification, 13.3.3.
156 */
157 function Date_toLocaleTimeString() {
158 // Steps 1-2.
159 var x = callFunction(ThisTimeValue, this, DATE_METHOD_LOCALE_TIME_STRING);
160 if (Number_isNaN(x)) {
161 return "Invalid Date";
162 }
163
164 // Steps 3-4.
165 var locales = ArgumentsLength() ? GetArgument(0) : undefined;
166 var options = ArgumentsLength() > 1 ? GetArgument(1) : undefined;
167
168 // Step 5-6.
169 var dateTimeFormat;
170 if (
171 (locales === undefined || typeof locales === "string") &&
172 options === undefined
173 ) {
174 // This cache only optimizes when |options| isn't used.
175 dateTimeFormat = GetCachedFormat(locales, "timeFormat", "time", "time");
176 } else {
177 dateTimeFormat = intl_CreateDateTimeFormat(locales, options, "time", "time");
178 }
179
180 // Step 7.
181 return intl_FormatDateTime(dateTimeFormat, x, /* formatToParts = */ false);
182 }
183 #endif // JS_HAS_INTL_API