[INTL] Implement Date.prototype.toLocaleTimeString in ECMA-402
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 20 Jan 2016 22:49:32 +0000 (22:49 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 20 Jan 2016 22:49:32 +0000 (22:49 +0000)
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:

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@195381 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/js/date-toLocaleString-expected.txt
LayoutTests/js/script-tests/date-toLocaleString.js
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/builtins/DatePrototype.js
Source/JavaScriptCore/runtime/DatePrototype.cpp

index 0b9925b192e449ce43ba6f96603a6691b849d6e5..02879b75a5dbd0b653620230f9228de22702afd5 100644 (file)
@@ -1,3 +1,15 @@
+2016-01-20  Andy VanWagoner  <thetalecrafter@gmail.com>
+
+        [INTL] Implement Date.prototype.toLocaleTimeString in ECMA-402
+        https://bugs.webkit.org/show_bug.cgi?id=147613
+
+        Reviewed by Darin Adler.
+
+        Added tests for toLocaleTimeString.
+
+        * js/date-toLocaleString-expected.txt:
+        * js/script-tests/date-toLocaleString.js:
+
 2016-01-20  Brady Eidson  <beidson@apple.com>
 
         Modern IDB: Make storage/indexeddb/modern tests more modern.
index a471c61f62e943856c477604c778334b481969d8..2565392d169c534ac3e35894f18f6d94c10f6c89 100644 (file)
@@ -49,6 +49,29 @@ PASS new Date(0).toLocaleDateString('en', { timeZone: 'UTC' }) is "1/1/1970"
 PASS new Date(0).toLocaleDateString('en', null) threw exception TypeError: null is not an object.
 PASS new Date(0).toLocaleDateString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' }) is "1/1/1970, 12:00 AM"
 PASS new Date(0).toLocaleDateString('en', { timeZone: 'UTC', year:'numeric', month:'long' }) is "January 1970"
+PASS Date.prototype.toLocaleTimeString.length is 0
+PASS Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').enumerable is false
+PASS Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').configurable is true
+PASS Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').writable is true
+PASS Date.prototype.toLocaleTimeString.call(new Date) did not throw exception.
+PASS Date.prototype.toLocaleTimeString.call() threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call(undefined) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call(null) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call(0) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call(NaN) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call(Infinity) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call('1') threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call({}) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call([]) threw exception TypeError: Type error.
+PASS Date.prototype.toLocaleTimeString.call(Symbol()) threw exception TypeError: Type error.
+PASS typeof new Date().toLocaleTimeString() === 'string' is true
+PASS new Date(NaN).toLocaleTimeString() is "Invalid Date"
+PASS new Date().toLocaleTimeString('i') threw exception RangeError: invalid language tag: i.
+PASS new Date(0).toLocaleTimeString('zh-Hans-CN-u-nu-hanidec', { timeZone: 'UTC' }) is "上午一二:〇〇:〇〇"
+PASS new Date(0).toLocaleTimeString('en', { timeZone: 'UTC' }) is "12:00:00 AM"
+PASS new Date(0).toLocaleTimeString('en', null) threw exception TypeError: null is not an object.
+PASS new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' }) is "12:00 AM"
+PASS new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', year:'numeric', month:'long' }) is "January 1970, 12:00:00 AM"
 PASS successfullyParsed is true
 
 TEST COMPLETE
index 77113f5f8aa8823b7d2e467b45f16a8191be7ae2..2f72655fe422ef03fcd8fcfc52ebcdb567e39747 100644 (file)
@@ -72,3 +72,41 @@ shouldThrow("new Date(0).toLocaleDateString('en', null)", "'TypeError: null is n
 shouldBeEqualToString("new Date(0).toLocaleDateString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' })", "1/1/1970, 12:00 AM");
 // If any date formats specified, just use them.
 shouldBeEqualToString("new Date(0).toLocaleDateString('en', { timeZone: 'UTC', year:'numeric', month:'long' })", "January 1970");
+
+// Test toLocaleTimeString ()
+shouldBe("Date.prototype.toLocaleTimeString.length", "0");
+shouldBeFalse("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').enumerable");
+shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').configurable");
+shouldBeTrue("Object.getOwnPropertyDescriptor(Date.prototype, 'toLocaleTimeString').writable");
+
+// Test thisTimeValue abrupt completion.
+shouldNotThrow("Date.prototype.toLocaleTimeString.call(new Date)");
+shouldThrow("Date.prototype.toLocaleTimeString.call()");
+shouldThrow("Date.prototype.toLocaleTimeString.call(undefined)");
+shouldThrow("Date.prototype.toLocaleTimeString.call(null)");
+shouldThrow("Date.prototype.toLocaleTimeString.call(0)");
+shouldThrow("Date.prototype.toLocaleTimeString.call(NaN)");
+shouldThrow("Date.prototype.toLocaleTimeString.call(Infinity)");
+shouldThrow("Date.prototype.toLocaleTimeString.call('1')");
+shouldThrow("Date.prototype.toLocaleTimeString.call({})");
+shouldThrow("Date.prototype.toLocaleTimeString.call([])");
+shouldThrow("Date.prototype.toLocaleTimeString.call(Symbol())");
+
+shouldBeTrue("typeof new Date().toLocaleTimeString() === 'string'");
+
+shouldBeEqualToString("new Date(NaN).toLocaleTimeString()", "Invalid Date");
+
+// Test for DateTimeFormat behavior.
+// Test that locale parameter is passed through properly.
+shouldThrow("new Date().toLocaleTimeString('i')");
+shouldBeEqualToString("new Date(0).toLocaleTimeString('zh-Hans-CN-u-nu-hanidec', { timeZone: 'UTC' })", "上午一二:〇〇:〇〇");
+
+// Defaults to hms
+shouldBeEqualToString("new Date(0).toLocaleTimeString('en', { timeZone: 'UTC' })", "12:00:00 AM");
+
+// Test that options parameter is passed through properly.
+shouldThrow("new Date(0).toLocaleTimeString('en', null)", "'TypeError: null is not an object'");
+// If time formats specifed, just use them.
+shouldBeEqualToString("new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', hour:'numeric', minute:'2-digit' })", "12:00 AM");
+// Adds hms if no time formats specified.
+shouldBeEqualToString("new Date(0).toLocaleTimeString('en', { timeZone: 'UTC', year:'numeric', month:'long' })", "January 1970, 12:00:00 AM");
index bd7be44f5515b26007f185af2cb0bf6a3dc34e18..2043654014bb05ab75235ece5f3e731e48a353f3 100644 (file)
@@ -1,3 +1,18 @@
+2016-01-20  Andy VanWagoner  <thetalecrafter@gmail.com>
+
+        [INTL] Implement Date.prototype.toLocaleTimeString in ECMA-402
+        https://bugs.webkit.org/show_bug.cgi?id=147613
+
+        Reviewed by Darin Adler.
+
+        Implement toLocaleTimeString in builtin JavaScript.
+
+        * builtins/DatePrototype.js:
+        (toLocaleTimeString.toDateTimeOptionsTimeTime):
+        (toLocaleTimeString):
+        * runtime/DatePrototype.cpp:
+        (JSC::DatePrototype::finishCreation):
+
 2016-01-20  Saam barati  <sbarati@apple.com>
 
         Web Inspector: Hook the sampling profiler into the Timelines UI
index 683c1d37bba41095ec278bb7515121a7179a1dde..21672a7c521b705c37d970067b4dc14223891c42 100644 (file)
@@ -131,3 +131,52 @@ function toLocaleDateString(/* locales, options */)
     var dateFormat = new @DateTimeFormat(locales, options);
     return dateFormat.format(value);
 }
+
+function toLocaleTimeString(/* locales, options */)
+{
+    "use strict";
+
+    function toDateTimeOptionsTimeTime(opts)
+    {
+        // ToDateTimeOptions(options, "time", "time")
+        // http://www.ecma-international.org/ecma-402/2.0/#sec-InitializeDateTimeFormat
+
+        var options;
+        if (opts === undefined)
+            options = null;
+        else if (opts === null)
+            throw new @TypeError("null is not an object");
+        else
+            options = @Object(opts);
+
+        // Check original instead of descendant to reduce lookups up the prototype chain.
+        var needsDefaults = !options || (
+            options.hour === undefined &&
+            options.minute === undefined &&
+            options.second === undefined
+        );
+
+        // Only create descendant if it will have own properties.
+        if (needsDefaults) {
+            options = @Object.create(options)
+            options.hour = "numeric";
+            options.minute = "numeric";
+            options.second = "numeric";
+        }
+
+        return options;
+    }
+
+    // 13.3.3 Date.prototype.toLocaleTimeString ([locales [, options ]]) (ECMA-402 2.0)
+    // http://www.ecma-international.org/ecma-402/2.0/#sec-Date.prototype.toLocaleTimeString
+
+    var value = @thisTimeValue.@call(this);
+    if (@isNaN(value))
+        return "Invalid Date";
+
+    var options = toDateTimeOptionsTimeTime(arguments[1]);
+    var locales = arguments[0];
+
+    var dateFormat = new @DateTimeFormat(locales, options);
+    return dateFormat.format(value);
+}
index 45af7e0efffc630650bc71d17b43a89fd39ce773..550aca43028951fa986c48166238112958f022b9 100644 (file)
@@ -494,6 +494,7 @@ void DatePrototype::finishCreation(VM& vm, JSGlobalObject* globalObject)
 #if ENABLE(INTL)
     JSC_BUILTIN_FUNCTION("toLocaleString", datePrototypeToLocaleStringCodeGenerator, DontEnum);
     JSC_BUILTIN_FUNCTION("toLocaleDateString", datePrototypeToLocaleDateStringCodeGenerator, DontEnum);
+    JSC_BUILTIN_FUNCTION("toLocaleTimeString", datePrototypeToLocaleTimeStringCodeGenerator, DontEnum);
 #else
     UNUSED_PARAM(globalObject);
 #endif // ENABLE(INTL)