js/src/builtin/Date.js

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:149ec9dce674
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 /*global intl_DateTimeFormat: false, */
6
7
8 var dateTimeFormatCache = new Record();
9
10
11 /**
12 * Format this Date object into a date and time string, using the locale and
13 * formatting options provided.
14 *
15 * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.5.
16 * Spec: ECMAScript Internationalization API Specification, 13.3.1.
17 */
18 function Date_toLocaleString() {
19 // Steps 1-2. Note that valueOf enforces "this time value" restrictions.
20 var x = callFunction(std_Date_valueOf, this);
21 if (std_isNaN(x))
22 return "Invalid Date";
23
24 // Steps 3-4.
25 var locales = arguments.length > 0 ? arguments[0] : undefined;
26 var options = arguments.length > 1 ? arguments[1] : undefined;
27
28 // Step 5-6.
29 var dateTimeFormat;
30 if (locales === undefined && options === undefined) {
31 // This cache only optimizes for the old ES5 toLocaleString without
32 // locales and options.
33 if (dateTimeFormatCache.dateTimeFormat === undefined) {
34 options = ToDateTimeOptions(options, "any", "all");
35 dateTimeFormatCache.dateTimeFormat = intl_DateTimeFormat(locales, options);
36 }
37 dateTimeFormat = dateTimeFormatCache.dateTimeFormat;
38 } else {
39 options = ToDateTimeOptions(options, "any", "all");
40 dateTimeFormat = intl_DateTimeFormat(locales, options);
41 }
42
43 // Step 7.
44 return intl_FormatDateTime(dateTimeFormat, x);
45 }
46
47
48 /**
49 * Format this Date object into a date string, using the locale and formatting
50 * options provided.
51 *
52 * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.6.
53 * Spec: ECMAScript Internationalization API Specification, 13.3.2.
54 */
55 function Date_toLocaleDateString() {
56 // Steps 1-2. Note that valueOf enforces "this time value" restrictions.
57 var x = callFunction(std_Date_valueOf, this);
58 if (std_isNaN(x))
59 return "Invalid Date";
60
61 // Steps 3-4.
62 var locales = arguments.length > 0 ? arguments[0] : undefined;
63 var options = arguments.length > 1 ? arguments[1] : undefined;
64
65 // Step 5-6.
66 var dateTimeFormat;
67 if (locales === undefined && options === undefined) {
68 // This cache only optimizes for the old ES5 toLocaleDateString without
69 // locales and options.
70 if (dateTimeFormatCache.dateFormat === undefined) {
71 options = ToDateTimeOptions(options, "date", "date");
72 dateTimeFormatCache.dateFormat = intl_DateTimeFormat(locales, options);
73 }
74 dateTimeFormat = dateTimeFormatCache.dateFormat;
75 } else {
76 options = ToDateTimeOptions(options, "date", "date");
77 dateTimeFormat = intl_DateTimeFormat(locales, options);
78 }
79
80 // Step 7.
81 return intl_FormatDateTime(dateTimeFormat, x);
82 }
83
84
85 /**
86 * Format this Date object into a time string, using the locale and formatting
87 * options provided.
88 *
89 * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.7.
90 * Spec: ECMAScript Internationalization API Specification, 13.3.3.
91 */
92 function Date_toLocaleTimeString() {
93 // Steps 1-2. Note that valueOf enforces "this time value" restrictions.
94 var x = callFunction(std_Date_valueOf, this);
95 if (std_isNaN(x))
96 return "Invalid Date";
97
98 // Steps 3-4.
99 var locales = arguments.length > 0 ? arguments[0] : undefined;
100 var options = arguments.length > 1 ? arguments[1] : undefined;
101
102 // Step 5-6.
103 var dateTimeFormat;
104 if (locales === undefined && options === undefined) {
105 // This cache only optimizes for the old ES5 toLocaleTimeString without
106 // locales and options.
107 if (dateTimeFormatCache.timeFormat === undefined) {
108 options = ToDateTimeOptions(options, "time", "time");
109 dateTimeFormatCache.timeFormat = intl_DateTimeFormat(locales, options);
110 }
111 dateTimeFormat = dateTimeFormatCache.timeFormat;
112 } else {
113 options = ToDateTimeOptions(options, "time", "time");
114 dateTimeFormat = intl_DateTimeFormat(locales, options);
115 }
116
117 // Step 7.
118 return intl_FormatDateTime(dateTimeFormat, x);
119 }

mercurial