michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /*global intl_DateTimeFormat: false, */ michael@0: michael@0: michael@0: var dateTimeFormatCache = new Record(); michael@0: michael@0: michael@0: /** michael@0: * Format this Date object into a date and time string, using the locale and michael@0: * formatting options provided. michael@0: * michael@0: * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.5. michael@0: * Spec: ECMAScript Internationalization API Specification, 13.3.1. michael@0: */ michael@0: function Date_toLocaleString() { michael@0: // Steps 1-2. Note that valueOf enforces "this time value" restrictions. michael@0: var x = callFunction(std_Date_valueOf, this); michael@0: if (std_isNaN(x)) michael@0: return "Invalid Date"; michael@0: michael@0: // Steps 3-4. michael@0: var locales = arguments.length > 0 ? arguments[0] : undefined; michael@0: var options = arguments.length > 1 ? arguments[1] : undefined; michael@0: michael@0: // Step 5-6. michael@0: var dateTimeFormat; michael@0: if (locales === undefined && options === undefined) { michael@0: // This cache only optimizes for the old ES5 toLocaleString without michael@0: // locales and options. michael@0: if (dateTimeFormatCache.dateTimeFormat === undefined) { michael@0: options = ToDateTimeOptions(options, "any", "all"); michael@0: dateTimeFormatCache.dateTimeFormat = intl_DateTimeFormat(locales, options); michael@0: } michael@0: dateTimeFormat = dateTimeFormatCache.dateTimeFormat; michael@0: } else { michael@0: options = ToDateTimeOptions(options, "any", "all"); michael@0: dateTimeFormat = intl_DateTimeFormat(locales, options); michael@0: } michael@0: michael@0: // Step 7. michael@0: return intl_FormatDateTime(dateTimeFormat, x); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Format this Date object into a date string, using the locale and formatting michael@0: * options provided. michael@0: * michael@0: * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.6. michael@0: * Spec: ECMAScript Internationalization API Specification, 13.3.2. michael@0: */ michael@0: function Date_toLocaleDateString() { michael@0: // Steps 1-2. Note that valueOf enforces "this time value" restrictions. michael@0: var x = callFunction(std_Date_valueOf, this); michael@0: if (std_isNaN(x)) michael@0: return "Invalid Date"; michael@0: michael@0: // Steps 3-4. michael@0: var locales = arguments.length > 0 ? arguments[0] : undefined; michael@0: var options = arguments.length > 1 ? arguments[1] : undefined; michael@0: michael@0: // Step 5-6. michael@0: var dateTimeFormat; michael@0: if (locales === undefined && options === undefined) { michael@0: // This cache only optimizes for the old ES5 toLocaleDateString without michael@0: // locales and options. michael@0: if (dateTimeFormatCache.dateFormat === undefined) { michael@0: options = ToDateTimeOptions(options, "date", "date"); michael@0: dateTimeFormatCache.dateFormat = intl_DateTimeFormat(locales, options); michael@0: } michael@0: dateTimeFormat = dateTimeFormatCache.dateFormat; michael@0: } else { michael@0: options = ToDateTimeOptions(options, "date", "date"); michael@0: dateTimeFormat = intl_DateTimeFormat(locales, options); michael@0: } michael@0: michael@0: // Step 7. michael@0: return intl_FormatDateTime(dateTimeFormat, x); michael@0: } michael@0: michael@0: michael@0: /** michael@0: * Format this Date object into a time string, using the locale and formatting michael@0: * options provided. michael@0: * michael@0: * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.7. michael@0: * Spec: ECMAScript Internationalization API Specification, 13.3.3. michael@0: */ michael@0: function Date_toLocaleTimeString() { michael@0: // Steps 1-2. Note that valueOf enforces "this time value" restrictions. michael@0: var x = callFunction(std_Date_valueOf, this); michael@0: if (std_isNaN(x)) michael@0: return "Invalid Date"; michael@0: michael@0: // Steps 3-4. michael@0: var locales = arguments.length > 0 ? arguments[0] : undefined; michael@0: var options = arguments.length > 1 ? arguments[1] : undefined; michael@0: michael@0: // Step 5-6. michael@0: var dateTimeFormat; michael@0: if (locales === undefined && options === undefined) { michael@0: // This cache only optimizes for the old ES5 toLocaleTimeString without michael@0: // locales and options. michael@0: if (dateTimeFormatCache.timeFormat === undefined) { michael@0: options = ToDateTimeOptions(options, "time", "time"); michael@0: dateTimeFormatCache.timeFormat = intl_DateTimeFormat(locales, options); michael@0: } michael@0: dateTimeFormat = dateTimeFormatCache.timeFormat; michael@0: } else { michael@0: options = ToDateTimeOptions(options, "time", "time"); michael@0: dateTimeFormat = intl_DateTimeFormat(locales, options); michael@0: } michael@0: michael@0: // Step 7. michael@0: return intl_FormatDateTime(dateTimeFormat, x); michael@0: }