Skip to content

Commit

Permalink
[MERGE #1672 @dilijev] Update Date.UTC to not return NaN for 1 argume…
Browse files Browse the repository at this point in the history
…nt. Fixes #1665.

Merge pull request #1672 from dilijev:dateutc

Fixes #1665: Date.UTC(x) [one-argument] spec change.
  • Loading branch information
dilijev committed Oct 3, 2016
2 parents dcaf859 + 416e54c commit f815ff0
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
6 changes: 3 additions & 3 deletions lib/Runtime/Library/DateImplementation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1582,9 +1582,9 @@ namespace Js {
uint ivar;

// See: https://github.com/Microsoft/ChakraCore/issues/1318
// Date.UTC should return NaN with < 2 arguments.
// args.Info.Count includes an implicit first parameter, so we check for Count <= 2.
if (args.Info.Count <= 2)
// Date.UTC should return NaN with 0 arguments.
// args.Info.Count includes an implicit first parameter, so we check for Count <= 1.
if (args.Info.Count <= 1)
{
return JavascriptNumber::NaN;
}
Expand Down
14 changes: 12 additions & 2 deletions test/Date/dateutc.baseline
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
NaN
NaN
126230400000
126230400000
149817600000
151804800000
151804800000
Expand All @@ -11,3 +11,13 @@ NaN
151806030040
NaN
NaN
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
PASS
53 changes: 53 additions & 0 deletions test/Date/dateutc.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,56 @@ d = Date.UTC(1, 9, 24, 0, 20, 30, 40); write(d);
d = Date.UTC(74, 9, 24, 0, 20, 30, 40, 50); write(d);
d = Date.UTC("hello"); write(d);
d = Date.UTC(); write(d);

function assert(p, message) {
if (!message) {
message = 'assert(true)';
}

if (p) {
console.log('PASS');
} else {
console.log(`FAIL: ${message}`);
}
}

assert(isNaN(Date.UTC()), "expected: Date.UTC() is NaN");
assert(!isNaN(Date.UTC(2017)), "expected: Date.UTC(2017) is not NaN");
assert(!isNaN(Date.UTC(2017, 0)), "expected: Date.UTC(2017, 0) is not NaN;");
assert(!isNaN(Date.UTC(2017, 0, 1)), "expected: Date.UTC(2017, 0, 1) is not NaN;");
assert(isNaN(Date.UTC(2017, undefined)), "expected: Date.UTC(2017, undefined) is NaN");
assert(Date.UTC(2017) === Date.UTC(2017, 0), "expected: Date.UTC(2017) === Date.UTC(2017, 0)");
assert(Date.UTC(2017, 0) === Date.UTC(2017, 0, 1), "expected: Date.UTC(2017, 0) === Date.UTC(2017, 0, 1)");

try {
Date.UTC({ valueOf: function() { throw "hey!" } });
console.log("FAIL: expected: throws");
} catch (e) {
if (e.toString() === "hey!") {
console.log("PASS");
} else {
console.log("expected: e.toString() === 'hey!'");
}
}

try {
Date.UTC({ valueOf: function() { throw "hey!" } }, 0);
console.log("FAIL: expected: throws");
} catch (e) {
if (e.toString() === "hey!") {
console.log("PASS");
} else {
console.log("expected: e.toString() === 'hey!'");
}
}

try {
Date.UTC({ valueOf: function() { throw "hey!" } }, 0, 1);
console.log("FAIL: expected: throws");
} catch (e) {
if (e.toString() === "hey!") {
console.log("PASS");
} else {
console.log("expected: e.toString() === 'hey!'");
}
}

0 comments on commit f815ff0

Please sign in to comment.