Skip to content
Snippets Groups Projects
Commit 8918e860 authored by Mike Pennisi's avatar Mike Pennisi
Browse files

Add tests for Date.UTC

parent 87917712
No related branches found
No related tags found
No related merge requests found
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Abrupt completions from coercing input values
info: |
1. Let y be ? ToNumber(year).
2. Let m be ? ToNumber(month).
3. If date is supplied, let dt be ? ToNumber(date); else let dt be 1.
4. If hours is supplied, let h be ? ToNumber(hours); else let h be 0.
5. If minutes is supplied, let min be ? ToNumber(minutes); else let min be 0.
6. If seconds is supplied, let s be ? ToNumber(seconds); else let s be 0.
7. If ms is supplied, let milli be ? ToNumber(ms); else let milli be 0.
8. If y is not NaN and 0 ≤ ToInteger(y) ≤ 99, let yr be 1900+ToInteger(y);
otherwise, let yr be y.
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
---*/
var thrower = { toString: function() { throw new Test262Error(); } };
var counter = { toString: function() { callCount += 1; } };
var callCount = 0;
assert.throws(Test262Error, function() {
new Date(thrower, counter);
}, 'year');
assert.sameValue(callCount, 0, 'coercion halts following error from "year"');
assert.throws(Test262Error, function() {
new Date(0, thrower, counter);
}, 'month');
assert.sameValue(callCount, 0, 'coercion halts following error from "month"');
assert.throws(Test262Error, function() {
new Date(0, 0, thrower, counter);
}, 'date');
assert.sameValue(callCount, 0, 'coercion halts following error from "date"');
assert.throws(Test262Error, function() {
new Date(0, 0, 1, thrower, counter);
}, 'hours');
assert.sameValue(callCount, 0, 'coercion halts following error from "hours"');
assert.throws(Test262Error, function() {
new Date(0, 0, 1, 0, thrower, counter);
}, 'minutes');
assert.sameValue(
callCount, 0, 'coercion halts following error from "minutes"'
);
assert.throws(Test262Error, function() {
new Date(0, 0, 1, 0, 0, thrower, counter);
}, 'seconds');
assert.sameValue(
callCount, 0, 'coercion halts following error from "seconds"'
);
assert.throws(Test262Error, function() {
new Date(0, 0, 1, 0, 0, 0, thrower);
}, 'ms');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Order of input coercion
info: |
1. Let y be ? ToNumber(year).
2. Let m be ? ToNumber(month).
3. If date is supplied, let dt be ? ToNumber(date); else let dt be 1.
4. If hours is supplied, let h be ? ToNumber(hours); else let h be 0.
5. If minutes is supplied, let min be ? ToNumber(minutes); else let min be 0.
6. If seconds is supplied, let s be ? ToNumber(seconds); else let s be 0.
7. If ms is supplied, let milli be ? ToNumber(ms); else let milli be 0.
8. If y is not NaN and 0 ≤ ToInteger(y) ≤ 99, let yr be 1900+ToInteger(y);
otherwise, let yr be y.
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
---*/
var log = '';
var year = { toString: function() { log += 'year'; return 0; } };
var month = { toString: function() { log += 'month'; return 0; } };
var date = { toString: function() { log += 'date'; return 1; } };
var hours = { toString: function() { log += 'hours'; return 0; } };
var minutes = { toString: function() { log += 'minutes'; return 0; } };
var seconds = { toString: function() { log += 'seconds'; return 0; } };
var ms = { toString: function() { log += 'ms'; return 0; } };
new Date(year, month, date, hours,minutes, seconds, ms);
assert.sameValue(log, 'yearmonthdatehoursminutessecondsms');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Infinite values specified to MakeDay produce NaN
info: |
[...]
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
MakeDay (year, month, date)
1. If year is not finite or month is not finite or date is not finite, return
NaN.
---*/
assert.sameValue(Date.UTC(Infinity, 0), NaN, 'year: Infinity');
assert.sameValue(Date.UTC(-Infinity, 0), NaN, 'year: -Infinity');
assert.sameValue(Date.UTC(0, Infinity), NaN, 'month: Infinity');
assert.sameValue(Date.UTC(0, -Infinity), NaN, 'month: -Infinity');
assert.sameValue(Date.UTC(0, 0, Infinity), NaN, 'date: Infinity');
assert.sameValue(Date.UTC(0, 0, -Infinity), NaN, 'date: -Infinity');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Infinite values specified to MakeTime produce NaN
info: |
[...]
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
MakeTime (hour, min, sec, ms)
1. If hour is not finite or min is not finite or sec is not finite or ms is
not finite, return NaN.
---*/
assert.sameValue(Date.UTC(0, 0, 1, Infinity), NaN, 'hour: Infinity');
assert.sameValue(Date.UTC(0, 0, 1, -Infinity), NaN, 'hour: -Infinity');
assert.sameValue(Date.UTC(0, 0, 1, 0, Infinity), NaN, 'minute: Infinity');
assert.sameValue(Date.UTC(0, 0, 1, 0, -Infinity), NaN, 'minute: -Infinity');
assert.sameValue(Date.UTC(0, 0, 1, 0, 0, Infinity), NaN, 'second: Infinity');
assert.sameValue(Date.UTC(0, 0, 1, 0, 0, -Infinity), NaN, 'second: -Infinity');
assert.sameValue(Date.UTC(0, 0, 1, 0, 0, 0, Infinity), NaN, 'ms: Infinity');
assert.sameValue(Date.UTC(0, 0, 1, 0, 0, 0, -Infinity), NaN, 'ms: -Infinity');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: NaN value handling
info: |
1. Let y be ? ToNumber(year).
2. Let m be ? ToNumber(month).
3. If date is supplied, let dt be ? ToNumber(date); else let dt be 1.
4. If hours is supplied, let h be ? ToNumber(hours); else let h be 0.
5. If minutes is supplied, let min be ? ToNumber(minutes); else let min be 0.
6. If seconds is supplied, let s be ? ToNumber(seconds); else let s be 0.
7. If ms is supplied, let milli be ? ToNumber(ms); else let milli be 0.
8. If y is not NaN and 0 ≤ ToInteger(y) ≤ 99, let yr be 1900+ToInteger(y);
otherwise, let yr be y.
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
---*/
assert.sameValue(new Date(NaN, 0).getTime(), NaN, 'year');
assert.sameValue(new Date(1970, NaN).getTime(), NaN, 'month');
assert.sameValue(new Date(1970, 0, NaN).getTime(), NaN, 'date');
assert.sameValue(new Date(1970, 0, 1, NaN).getTime(), NaN, 'hours');
assert.sameValue(new Date(1970, 0, 1, 0, NaN).getTime(), NaN, 'minutes');
assert.sameValue(new Date(1970, 0, 1, 0, 0, NaN).getTime(), NaN, 'seconds');
assert.sameValue(new Date(1970, 0, 1, 0, 0, 0, NaN).getTime(), NaN, 'ms');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Values specified to MakeDay exceed their calendar boundaries
info: |
[...]
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
MakeDay (year, month, date)
[...]
5. Let ym be y + floor(m / 12).
[...]
7. Find a value t such that YearFromTime(t) is ym and MonthFromTime(t) is mn
and DateFromTime(t) is 1; but if this is not possible (because some
argument is out of range), return NaN.
8. Return Day(t) + dt - 1.
---*/
assert.sameValue(Date.UTC(2016, 12), 1483228800000, 'month: 12');
assert.sameValue(Date.UTC(2016, 13), 1485907200000, 'month: 13');
assert.sameValue(Date.UTC(2016, 144), 1830297600000, 'month: 144');
assert.sameValue(Date.UTC(2016, 0, 33), 1454371200000, 'day greater than month');
assert.sameValue(Date.UTC(2016, 2, -27), 1454371200000, 'day negative value');
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Values specified to MakeTime exceed their time boundaries
info: |
[...]
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
MakeTime (hour, min, sec, ms)
1. If hour is not finite or min is not finite or sec is not finite or ms is
not finite, return NaN.
2. Let h be ToInteger(hour).
3. Let m be ToInteger(min).
4. Let s be ToInteger(sec).
5. Let milli be ToInteger(ms).
6. Let t be h * msPerHour + m * msPerMinute + s * msPerSecond + milli,
performing the arithmetic according to IEEE 754-2008 rules (that is, as if
using the ECMAScript operators * and +).
7. Return t.
---*/
assert.sameValue(Date.UTC(2016, 6, 5, -1), 1467673200000, 'hour: -1');
assert.sameValue(Date.UTC(2016, 6, 5, 24), 1467763200000, 'hour: 24');
assert.sameValue(Date.UTC(2016, 6, 5, 0, -1), 1467676740000, 'minute: -1');
assert.sameValue(Date.UTC(2016, 6, 5, 0, 60), 1467680400000, 'minute: 60');
assert.sameValue(Date.UTC(2016, 6, 5, 0, 0, -1), 1467676799000, 'second: -1');
assert.sameValue(Date.UTC(2016, 6, 5, 0, 0, 60), 1467676860000, 'second: 60');
assert.sameValue(
Date.UTC(2016, 6, 5, 0, 0, 0, -1), 1467676799999, 'millisecond: -1'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 0, 0, 0, 1000), 1467676801000, 'millisecond: 1000'
);
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Return value of `Date.UTC`
info: |
1. Let y be ? ToNumber(year).
2. Let m be ? ToNumber(month).
3. If date is supplied, let dt be ? ToNumber(date); else let dt be 1.
4. If hours is supplied, let h be ? ToNumber(hours); else let h be 0.
5. If minutes is supplied, let min be ? ToNumber(minutes); else let min be 0.
6. If seconds is supplied, let s be ? ToNumber(seconds); else let s be 0.
7. If ms is supplied, let milli be ? ToNumber(ms); else let milli be 0.
8. If y is not NaN and 0 ≤ ToInteger(y) ≤ 99, let yr be 1900+ToInteger(y);
otherwise, let yr be y.
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
---*/
assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34, 45, 0),
1467732885000,
'2016, 6, 5, 15, 34, 45, 0'
);
assert.sameValue(
Date.UTC(2016, 6, 5, 15, 34, 45, 876),
1467732885876,
'2016, 6, 5, 15, 34, 45, 0'
);
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Time clipping
info: |
[...]
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
TimeClip (time)
1. If time is not finite, return NaN.
2. If abs(time) > 8.64 × 1015, return NaN.
---*/
assert.notSameValue(Date.UTC(275760, 8, 13, 0, 0, 0, 0), NaN);
assert.sameValue(Date.UTC(275760, 8, 13, 0, 0, 0, 1), NaN);
// Copyright (C) 2016 the V8 project authors. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.
/*---
esid: sec-date.utc
es6id: 20.3.3.4
description: Conditional offset of provided `year` value
info: |
1. Let y be ? ToNumber(year).
[...]
8. If y is not NaN and 0 ≤ ToInteger(y) ≤ 99, let yr be 1900+ToInteger(y);
otherwise, let yr be y.
9. Return TimeClip(MakeDate(MakeDay(yr, m, dt), MakeTime(h, min, s, milli))).
---*/
assert.sameValue(Date.UTC(-1, 0), -62198755200000, '-1 (no offset)');
assert.sameValue(Date.UTC(0, 0), -2208988800000, '+0');
assert.sameValue(Date.UTC(-0, 0), -2208988800000, '-0');
assert.sameValue(Date.UTC(-0.999999, 0), -2208988800000, '-0.999999');
assert.sameValue(Date.UTC(70, 0), 0, '70');
assert.sameValue(Date.UTC(70, 0), 0, '70.999999');
assert.sameValue(Date.UTC(99, 0), 915148800000, '99');
assert.sameValue(Date.UTC(99.999999, 0), 915148800000, '99.999999');
assert.sameValue(Date.UTC(100, 0), -59011459200000, '100 (no offset)');
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment