js/src/builtin/Date.js

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/js/src/builtin/Date.js	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,119 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*global intl_DateTimeFormat: false, */
     1.9 +
    1.10 +
    1.11 +var dateTimeFormatCache = new Record();
    1.12 +
    1.13 +
    1.14 +/**
    1.15 + * Format this Date object into a date and time string, using the locale and
    1.16 + * formatting options provided.
    1.17 + *
    1.18 + * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.5.
    1.19 + * Spec: ECMAScript Internationalization API Specification, 13.3.1.
    1.20 + */
    1.21 +function Date_toLocaleString() {
    1.22 +    // Steps 1-2.  Note that valueOf enforces "this time value" restrictions.
    1.23 +    var x = callFunction(std_Date_valueOf, this);
    1.24 +    if (std_isNaN(x))
    1.25 +        return "Invalid Date";
    1.26 +
    1.27 +    // Steps 3-4.
    1.28 +    var locales = arguments.length > 0 ? arguments[0] : undefined;
    1.29 +    var options = arguments.length > 1 ? arguments[1] : undefined;
    1.30 +
    1.31 +    // Step 5-6.
    1.32 +    var dateTimeFormat;
    1.33 +    if (locales === undefined && options === undefined) {
    1.34 +        // This cache only optimizes for the old ES5 toLocaleString without
    1.35 +        // locales and options.
    1.36 +        if (dateTimeFormatCache.dateTimeFormat === undefined) {
    1.37 +            options = ToDateTimeOptions(options, "any", "all");
    1.38 +            dateTimeFormatCache.dateTimeFormat = intl_DateTimeFormat(locales, options);
    1.39 +        }
    1.40 +        dateTimeFormat = dateTimeFormatCache.dateTimeFormat;
    1.41 +    } else {
    1.42 +        options = ToDateTimeOptions(options, "any", "all");
    1.43 +        dateTimeFormat = intl_DateTimeFormat(locales, options);
    1.44 +    }
    1.45 +
    1.46 +    // Step 7.
    1.47 +    return intl_FormatDateTime(dateTimeFormat, x);
    1.48 +}
    1.49 +
    1.50 +
    1.51 +/**
    1.52 + * Format this Date object into a date string, using the locale and formatting
    1.53 + * options provided.
    1.54 + *
    1.55 + * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.6.
    1.56 + * Spec: ECMAScript Internationalization API Specification, 13.3.2.
    1.57 + */
    1.58 +function Date_toLocaleDateString() {
    1.59 +    // Steps 1-2.  Note that valueOf enforces "this time value" restrictions.
    1.60 +    var x = callFunction(std_Date_valueOf, this);
    1.61 +    if (std_isNaN(x))
    1.62 +        return "Invalid Date";
    1.63 +
    1.64 +    // Steps 3-4.
    1.65 +    var locales = arguments.length > 0 ? arguments[0] : undefined;
    1.66 +    var options = arguments.length > 1 ? arguments[1] : undefined;
    1.67 +
    1.68 +    // Step 5-6.
    1.69 +    var dateTimeFormat;
    1.70 +    if (locales === undefined && options === undefined) {
    1.71 +        // This cache only optimizes for the old ES5 toLocaleDateString without
    1.72 +        // locales and options.
    1.73 +        if (dateTimeFormatCache.dateFormat === undefined) {
    1.74 +            options = ToDateTimeOptions(options, "date", "date");
    1.75 +            dateTimeFormatCache.dateFormat = intl_DateTimeFormat(locales, options);
    1.76 +        }
    1.77 +        dateTimeFormat = dateTimeFormatCache.dateFormat;
    1.78 +    } else {
    1.79 +        options = ToDateTimeOptions(options, "date", "date");
    1.80 +        dateTimeFormat = intl_DateTimeFormat(locales, options);
    1.81 +    }
    1.82 +
    1.83 +    // Step 7.
    1.84 +    return intl_FormatDateTime(dateTimeFormat, x);
    1.85 +}
    1.86 +
    1.87 +
    1.88 +/**
    1.89 + * Format this Date object into a time string, using the locale and formatting
    1.90 + * options provided.
    1.91 + *
    1.92 + * Spec: ECMAScript Language Specification, 5.1 edition, 15.9.5.7.
    1.93 + * Spec: ECMAScript Internationalization API Specification, 13.3.3.
    1.94 + */
    1.95 +function Date_toLocaleTimeString() {
    1.96 +    // Steps 1-2.  Note that valueOf enforces "this time value" restrictions.
    1.97 +    var x = callFunction(std_Date_valueOf, this);
    1.98 +    if (std_isNaN(x))
    1.99 +        return "Invalid Date";
   1.100 +
   1.101 +    // Steps 3-4.
   1.102 +    var locales = arguments.length > 0 ? arguments[0] : undefined;
   1.103 +    var options = arguments.length > 1 ? arguments[1] : undefined;
   1.104 +
   1.105 +    // Step 5-6.
   1.106 +    var dateTimeFormat;
   1.107 +    if (locales === undefined && options === undefined) {
   1.108 +        // This cache only optimizes for the old ES5 toLocaleTimeString without
   1.109 +        // locales and options.
   1.110 +        if (dateTimeFormatCache.timeFormat === undefined) {
   1.111 +            options = ToDateTimeOptions(options, "time", "time");
   1.112 +            dateTimeFormatCache.timeFormat = intl_DateTimeFormat(locales, options);
   1.113 +        }
   1.114 +        dateTimeFormat = dateTimeFormatCache.timeFormat;
   1.115 +    } else {
   1.116 +        options = ToDateTimeOptions(options, "time", "time");
   1.117 +        dateTimeFormat = intl_DateTimeFormat(locales, options);
   1.118 +    }
   1.119 +
   1.120 +    // Step 7.
   1.121 +    return intl_FormatDateTime(dateTimeFormat, x);
   1.122 +}

mercurial