Changeset 195381 in webkit


Ignore:
Timestamp:
Jan 20, 2016 2:49:32 PM (8 years ago)
Author:
commit-queue@webkit.org
Message:

[INTL] Implement Date.prototype.toLocaleTimeString in ECMA-402
https://bugs.webkit.org/show_bug.cgi?id=147613

Patch by Andy VanWagoner <thetalecrafter@gmail.com> on 2016-01-20
Reviewed by Darin Adler.

Source/JavaScriptCore:

Implement toLocaleTimeString in builtin JavaScript.

  • builtins/DatePrototype.js:

(toLocaleTimeString.toDateTimeOptionsTimeTime):
(toLocaleTimeString):

  • runtime/DatePrototype.cpp:

(JSC::DatePrototype::finishCreation):

LayoutTests:

Added tests for toLocaleTimeString.

  • js/date-toLocaleString-expected.txt:
  • js/script-tests/date-toLocaleString.js:
Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r195380 r195381  
     12016-01-20  Andy VanWagoner  <thetalecrafter@gmail.com>
     2
     3        [INTL] Implement Date.prototype.toLocaleTimeString in ECMA-402
     4        https://bugs.webkit.org/show_bug.cgi?id=147613
     5
     6        Reviewed by Darin Adler.
     7
     8        Added tests for toLocaleTimeString.
     9
     10        * js/date-toLocaleString-expected.txt:
     11        * js/script-tests/date-toLocaleString.js:
     12
    1132016-01-20  Brady Eidson  <beidson@apple.com>
    214
  • trunk/LayoutTests/js/date-toLocaleString-expected.txt

    r195330 r195381  
    5050PASS new Date(0).toLocaleDateString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' }) is "1/1/1970, 12:00 AM"
    5151PASS new Date(0).toLocaleDateString('en', { timeZone: 'UTC', year:'numeric', month:'long' }) is "January 1970"
     52PASS Date.prototype.toLocaleTimeString.length is 0
     53PASS Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').enumerable is false
     54PASS Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').configurable is true
     55PASS Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').writable is true
     56PASS Date.prototype.toLocaleTimeString.call(new Date) did not throw exception.
     57PASS Date.prototype.toLocaleTimeString.call() threw exception TypeError: Type error.
     58PASS Date.prototype.toLocaleTimeString.call(undefined) threw exception TypeError: Type error.
     59PASS Date.prototype.toLocaleTimeString.call(null) threw exception TypeError: Type error.
     60PASS Date.prototype.toLocaleTimeString.call(0) threw exception TypeError: Type error.
     61PASS Date.prototype.toLocaleTimeString.call(NaN) threw exception TypeError: Type error.
     62PASS Date.prototype.toLocaleTimeString.call(Infinity) threw exception TypeError: Type error.
     63PASS Date.prototype.toLocaleTimeString.call('1') threw exception TypeError: Type error.
     64PASS Date.prototype.toLocaleTimeString.call({}) threw exception TypeError: Type error.
     65PASS Date.prototype.toLocaleTimeString.call([]) threw exception TypeError: Type error.
     66PASS Date.prototype.toLocaleTimeString.call(Symbol()) threw exception TypeError: Type error.
     67PASS typeof new Date().toLocaleTimeString() === 'string' is true
     68PASS new Date(NaN).toLocaleTimeString() is "Invalid Date"
     69PASS new Date().toLocaleTimeString('i') threw exception RangeError: invalid language tag: i.
     70PASS new Date(0).toLocaleTimeString('zh-Hans-CN-u-nu-hanidec', { timeZone: 'UTC' }) is "上午一二:〇〇:〇〇"
     71PASS new Date(0).toLocaleTimeString('en', { timeZone: 'UTC' }) is "12:00:00 AM"
     72PASS new Date(0).toLocaleTimeString('en', null) threw exception TypeError: null is not an object.
     73PASS new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' }) is "12:00 AM"
     74PASS new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', year:'numeric', month:'long' }) is "January 1970, 12:00:00 AM"
    5275PASS successfullyParsed is true
    5376
  • trunk/LayoutTests/js/script-tests/date-toLocaleString.js

    r195330 r195381  
    7373// If any date formats specified, just use them.
    7474shouldBeEqualToString("new Date(0).toLocaleDateString('en', { timeZone: 'UTC', year:'numeric', month:'long' })", "January 1970");
     75
     76// Test toLocaleTimeString ()
     77shouldBe("Date.prototype.toLocaleTimeString.length", "0");
     78shouldBeFalse("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').enumerable");
     79shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').configurable");
     80shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').writable");
     81
     82// Test thisTimeValue abrupt completion.
     83shouldNotThrow("Date.prototype.toLocaleTimeString.call(new Date)");
     84shouldThrow("Date.prototype.toLocaleTimeString.call()");
     85shouldThrow("Date.prototype.toLocaleTimeString.call(undefined)");
     86shouldThrow("Date.prototype.toLocaleTimeString.call(null)");
     87shouldThrow("Date.prototype.toLocaleTimeString.call(0)");
     88shouldThrow("Date.prototype.toLocaleTimeString.call(NaN)");
     89shouldThrow("Date.prototype.toLocaleTimeString.call(Infinity)");
     90shouldThrow("Date.prototype.toLocaleTimeString.call('1')");
     91shouldThrow("Date.prototype.toLocaleTimeString.call({})");
     92shouldThrow("Date.prototype.toLocaleTimeString.call([])");
     93shouldThrow("Date.prototype.toLocaleTimeString.call(Symbol())");
     94
     95shouldBeTrue("typeof new Date().toLocaleTimeString() === 'string'");
     96
     97shouldBeEqualToString("new Date(NaN).toLocaleTimeString()", "Invalid Date");
     98
     99// Test for DateTimeFormat behavior.
     100// Test that locale parameter is passed through properly.
     101shouldThrow("new Date().toLocaleTimeString('i')");
     102shouldBeEqualToString("new Date(0).toLocaleTimeString('zh-Hans-CN-u-nu-hanidec', { timeZone: 'UTC' })", "上午一二:〇〇:〇〇");
     103
     104// Defaults to hms
     105shouldBeEqualToString("new Date(0).toLocaleTimeString('en', { timeZone: 'UTC' })", "12:00:00 AM");
     106
     107// Test that options parameter is passed through properly.
     108shouldThrow("new Date(0).toLocaleTimeString('en', null)", "'TypeError: null is not an object'");
     109// If time formats specifed, just use them.
     110shouldBeEqualToString("new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' })", "12:00 AM");
     111// Adds hms if no time formats specified.
     112shouldBeEqualToString("new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', year:'numeric', month:'long' })", "January 1970, 12:00:00 AM");
  • trunk/Source/JavaScriptCore/ChangeLog

    r195376 r195381  
     12016-01-20  Andy VanWagoner  <thetalecrafter@gmail.com>
     2
     3        [INTL] Implement Date.prototype.toLocaleTimeString in ECMA-402
     4        https://bugs.webkit.org/show_bug.cgi?id=147613
     5
     6        Reviewed by Darin Adler.
     7
     8        Implement toLocaleTimeString in builtin JavaScript.
     9
     10        * builtins/DatePrototype.js:
     11        (toLocaleTimeString.toDateTimeOptionsTimeTime):
     12        (toLocaleTimeString):
     13        * runtime/DatePrototype.cpp:
     14        (JSC::DatePrototype::finishCreation):
     15
    1162016-01-20  Saam barati  <sbarati@apple.com>
    217
  • trunk/Source/JavaScriptCore/builtins/DatePrototype.js

    r195330 r195381  
    132132    return dateFormat.format(value);
    133133}
     134
     135function toLocaleTimeString(/* locales, options */)
     136{
     137    "use strict";
     138
     139    function toDateTimeOptionsTimeTime(opts)
     140    {
     141        // ToDateTimeOptions(options, "time", "time")
     142        // http://www.ecma-international.org/ecma-402/2.0/#sec-InitializeDateTimeFormat
     143
     144        var options;
     145        if (opts === undefined)
     146            options = null;
     147        else if (opts === null)
     148            throw new @TypeError("null is not an object");
     149        else
     150            options = @Object(opts);
     151
     152        // Check original instead of descendant to reduce lookups up the prototype chain.
     153        var needsDefaults = !options || (
     154            options.hour === undefined &&
     155            options.minute === undefined &&
     156            options.second === undefined
     157        );
     158
     159        // Only create descendant if it will have own properties.
     160        if (needsDefaults) {
     161            options = @Object.create(options)
     162            options.hour = "numeric";
     163            options.minute = "numeric";
     164            options.second = "numeric";
     165        }
     166
     167        return options;
     168    }
     169
     170    // 13.3.3 Date.prototype.toLocaleTimeString ([locales [, options ]]) (ECMA-402 2.0)
     171    // http://www.ecma-international.org/ecma-402/2.0/#sec-Date.prototype.toLocaleTimeString
     172
     173    var value = @thisTimeValue.@call(this);
     174    if (@isNaN(value))
     175        return "Invalid Date";
     176
     177    var options = toDateTimeOptionsTimeTime(arguments[1]);
     178    var locales = arguments[0];
     179
     180    var dateFormat = new @DateTimeFormat(locales, options);
     181    return dateFormat.format(value);
     182}
  • trunk/Source/JavaScriptCore/runtime/DatePrototype.cpp

    r195330 r195381  
    495495    JSC_BUILTIN_FUNCTION("toLocaleString", datePrototypeToLocaleStringCodeGenerator, DontEnum);
    496496    JSC_BUILTIN_FUNCTION("toLocaleDateString", datePrototypeToLocaleDateStringCodeGenerator, DontEnum);
     497    JSC_BUILTIN_FUNCTION("toLocaleTimeString", datePrototypeToLocaleTimeStringCodeGenerator, DontEnum);
    497498#else
    498499    UNUSED_PARAM(globalObject);
Note: See TracChangeset for help on using the changeset viewer.