From 70d733760e4fbe64b2bd257c0a106e788e552dbb Mon Sep 17 00:00:00 2001 From: Xotic750 Date: Thu, 10 Dec 2015 13:59:02 +0100 Subject: [PATCH] Fix date methods --- es5-shim.js | 196 +++++++++++++++++++++++ tests/spec/s-date.js | 365 +++++++++++++++++++++++++++++++++++++------ 2 files changed, 511 insertions(+), 50 deletions(-) diff --git a/es5-shim.js b/es5-shim.js index 6022eee8..575e541f 100644 --- a/es5-shim.js +++ b/es5-shim.js @@ -1076,6 +1076,202 @@ defineProperties($Object, { // ==== // +var hasNegativeMonthYearBug = new Date(-3509827329600292).getUTCMonth() !== 0; +var aNegativeTestDate = new Date(-1509842289600292); +var aPositiveTestDate = new Date(1449662400000); +var hasToUTCStringFormatBug = aNegativeTestDate.toUTCString() !== 'Mon, 01 Jan -45875 11:59:59 GMT'; +var hasToDateStringFormatBug; +var hasToStringFormatBug; +var timeZoneOffset = aNegativeTestDate.getTimezoneOffset(); +if (timeZoneOffset < -720) { + hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Tue Jan 02 -45875'; + hasToStringFormatBug = !(/^Thu Dec 10 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/).test(aPositiveTestDate.toString()); +} else { + hasToDateStringFormatBug = aNegativeTestDate.toDateString() !== 'Mon Jan 01 -45875'; + hasToStringFormatBug = !(/^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/).test(aPositiveTestDate.toString()); +} + +var originalGetFullYear; +var originalGetMonth; +var originalGetDate; +var originalGetUTCFullYear; +var originalGetUTCMonth; +var originalGetUTCDate; +var originalToUTCString; +var originalToDateString; +var originalToString; +var dayName; +var monthName; +var daysInMonth; +if (hasNegativeMonthYearBug || hasToDateStringFormatBug || hasToUTCStringFormatBug || hasToStringFormatBug) { + originalGetFullYear = Date.prototype.getFullYear; + originalGetMonth = Date.prototype.getMonth; + originalGetDate = Date.prototype.getDate; + originalGetUTCFullYear = Date.prototype.getUTCFullYear; + originalGetUTCMonth = Date.prototype.getUTCMonth; + originalGetUTCDate = Date.prototype.getUTCDate; + originalToUTCString = Date.prototype.toUTCString; + originalToDateString = Date.prototype.toDateString; + originalToString = Date.prototype.toString; + dayName = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri']; + monthName = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']; + daysInMonth = function dim(month, year) { + return originalGetDate.call(new Date(year, month, 0)); + }; +} + +defineProperties(Date.prototype, { + getFullYear: function getFullYear() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var year = originalGetFullYear.call(this); + if (year < 0 && originalGetMonth.call(this) > 11) { + return year + 1; + } + return year; + }, + getMonth: function getMonth() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var year = originalGetFullYear.call(this); + var month = originalGetMonth.call(this); + if (year < 0 && month > 11) { + return 0; + } + return month; + }, + getDate: function getDate() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var year = originalGetFullYear.call(this); + var month = originalGetMonth.call(this); + var date = originalGetDate.call(this); + if (year < 0 && month > 11) { + if (month === 12) { + return date; + } + var days = daysInMonth(0, year + 1); + return (days - date) + 1; + } + return date; + }, + getUTCFullYear: function getUTCFullYear() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var year = originalGetUTCFullYear.call(this); + if (year < 0 && originalGetUTCMonth.call(this) > 11) { + return year + 1; + } + return year; + }, + getUTCMonth: function getUTCMonth() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var year = originalGetUTCFullYear.call(this); + var month = originalGetUTCMonth.call(this); + if (year < 0 && month > 11) { + return 0; + } + return month; + }, + getUTCDate: function getUTCDate() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var year = originalGetUTCFullYear.call(this); + var month = originalGetUTCMonth.call(this); + var date = originalGetUTCDate.call(this); + if (year < 0 && month > 11) { + if (month === 12) { + return date; + } + var days = daysInMonth(0, year + 1); + return (days - date) + 1; + } + return date; + } +}, hasNegativeMonthYearBug); + +defineProperties(Date.prototype, { + toUTCString: function toUTCString() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var day = this.getUTCDay(); + var date = this.getUTCDate(); + var month = this.getUTCMonth(); + var year = this.getUTCFullYear(); + var hour = this.getUTCHours(); + var minute = this.getUTCMinutes(); + var second = this.getUTCSeconds(); + return dayName[day] + ', ' + + (date < 10 ? '0' + date : date) + ' ' + + monthName[month] + ' ' + + year + ' ' + + (hour < 10 ? '0' + hour : hour) + ':' + + (minute < 10 ? '0' + minute : minute) + ':' + + (second < 10 ? '0' + second : second) + ' GMT'; + } +}, hasNegativeMonthYearBug || hasToUTCStringFormatBug); + +// Opera 12 has `,` +defineProperties(Date.prototype, { + toDateString: function toDateString() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var day = this.getDay(); + var date = this.getDate(); + var month = this.getMonth(); + var year = this.getFullYear(); + return dayName[day] + ' ' + + monthName[month] + ' ' + + (date < 10 ? '0' + date : date) + ' ' + + year; + } +}, hasNegativeMonthYearBug || hasToDateStringFormatBug); + +// can't use defineProperties here because of toString enumeration issue in IE <= 8 +if (hasNegativeMonthYearBug || hasToStringFormatBug) { + Date.prototype.toString = function toString() { + if (!this || !(this instanceof Date)) { + throw new TypeError('this is not a Date object.'); + } + var day = this.getDay(); + var date = this.getDate(); + var month = this.getMonth(); + var year = this.getFullYear(); + var hour = this.getHours(); + var minute = this.getMinutes(); + var second = this.getSeconds(); + var timezoneOffset = this.getTimezoneOffset(); + var hoursOffset = Math.floor(Math.abs(timezoneOffset) / 60); + var minutesOffset = Math.floor(Math.abs(timezoneOffset) % 60); + return dayName[day] + ' ' + + monthName[month] + ' ' + + (date < 10 ? '0' + date : date) + ' ' + + year + ' ' + + (hour < 10 ? '0' + hour : hour) + ':' + + (minute < 10 ? '0' + minute : minute) + ':' + + (second < 10 ? '0' + second : second) + ' GMT' + + (timezoneOffset > 0 ? '-' : '+') + + (hoursOffset < 10 ? '0' + hoursOffset : hoursOffset) + + (minutesOffset < 10 ? '0' + minutesOffset : minutesOffset); + }; + if (supportsDescriptors) { + $Object.defineProperty(Date.prototype, 'toString', { + configurable: true, + enumerable: false, + writable: true + }); + } +} + // ES5 15.9.5.43 // http://es5.github.com/#x15.9.5.43 // This function returns a String value represent the instance in time diff --git a/tests/spec/s-date.js b/tests/spec/s-date.js index c43d64bb..7b559f3c 100644 --- a/tests/spec/s-date.js +++ b/tests/spec/s-date.js @@ -6,8 +6,13 @@ describe('Date', function () { var supportsDescriptors = Object.defineProperty && (function () { try { var obj = {}; - Object.defineProperty(obj, 'x', { enumerable: false, value: obj }); - for (var _ in obj) { return false; } + Object.defineProperty(obj, 'x', { + enumerable: false, + value: obj + }); + for (var _ in obj) { + return false; + } return obj.x === obj; } catch (e) { /* this is ES3 */ return false; @@ -16,6 +21,85 @@ describe('Date', function () { var ifSupportsDescriptorsIt = supportsDescriptors ? it : xit; var has = Object.prototype.hasOwnProperty; + var timeZoneOffset; + var negativeDate; + beforeEach(function () + timeZoneOffset = new Date().getTimezoneOffset(); + var negativeCanned = [ + { + getTime: -3509827329600292, + getUTCDay: 4, + getDay: 4, + dim: 31 + }, { + getTime: -3509824651200292, + getUTCDay: 0, + getDay: 0, + dim: 29 + }, { + getTime: -3509822145600292, + getUTCDay: 1, + getDay: 1, + dim: 31 + }, { + getTime: -3509819467200292, + getUTCDay: 4, + getDay: 4, + dim: 30 + }, { + getTime: -3509816875200292, + getUTCDay: 6, + getDay: 6, + dim: 31 + }, { + getTime: -3509814196800292, + getUTCDay: 2, + getDay: 2, + dim: 30 + }, { + getTime: -3509811604800292, + getUTCDay: 4, + getDay: 4, + dim: 31 + }, { + getTime: -3509808926400292, + getUTCDay: 0, + getDay: 0, + dim: 31 + }, { + getTime: -3509806248000292, + getUTCDay: 3, + getDay: 3, + dim: 30 + }, { + getTime: -3509803656000292, + getUTCDay: 5, + getDay: 5, + dim: 31 + }, { + getTime: -3509800977600292, + getUTCDay: 1, + getDay: 1, + dim: 30 + }, { + getTime: -3509798385600292, + getUTCDay: 3, + getDay: 3, + dim: 31 + } + ]; + negativeDate = negativeCanned.map(function (item) { + var dateFirst = new Date(item.getTime); + var dateLast = new Date(item.getTime + ((item.dim - 1) * 86400000)); + return { + dates: [dateFirst, dateLast], + days: [1, item.dim], + getUTCDay: [item.getUTCDay, (item.getUTCDay + item.dim - 1) % 7], + getDay: [item.getDay, (item.getDay + item.dim - 1) % 7] + }; + }); + }); + describe('.now()', function () { it('should be the current time', function () { var before = (new Date()).getTime(); @@ -28,7 +112,7 @@ describe('Date', function () { describe('constructor', function () { it('works with standard formats', function () { - // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 + // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 expect(+new Date('2012-12-31T23:59:59.000Z')).toBe(1356998399000); // 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000 expect(+new Date('2012-04-04T05:02:02.170Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 expect(+new Date('2012-04-04T05:02:02.170999Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 @@ -40,39 +124,39 @@ describe('Date', function () { // https://github.com/es-shims/es5-shim/issues/80 Safari bug with leap day expect(new Date('2034-03-01T00:00:00.000Z') - - new Date('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1 + new Date('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1 }); ifSupportsDescriptorsIt('is not enumerable', function () { - expect(Object.keys(new Date())).not.toContain('constructor'); + expect(Object.keys(new Date())).not.toContain('constructor'); }); it('works as a function', function () { - var zeroDate = Date(0); - expect(zeroDate).toBe(String(zeroDate)); - var value = Date(1441705534578); - expect(value).toBe(String(value)); + var zeroDate = Date(0); + expect(zeroDate).toBe(String(zeroDate)); + var value = Date(1441705534578); + expect(value).toBe(String(value)); }); it('fixes this Safari 8/9 bug', function () { - var offset = new Date(1970).getTimezoneOffset() * 60e3; - - var timestamp = 2147483647; // Math.pow(2, 31) - 1 - var date = new Date(1970, 0, 1, 0, 0, 0, timestamp); - var expectedTimestamp = timestamp + offset; - expect(date.getTime()).toBe(expectedTimestamp); - - var brokenTimestamp = 2147483648; // Math.pow(2, 31) - var brokenDate = new Date(1970, 0, 1, 0, 0, 0, brokenTimestamp); - var expectedBrokenTimestamp = brokenTimestamp + offset; - expect(brokenDate.getTime()).toBe(expectedBrokenTimestamp); // NaN in Safari 8/9 - - var veryBrokenTS = 1435734000000; - var veryBrokenDate = new Date(1970, 0, 1, 0, 0, 0, veryBrokenTS); - var largeDate = new Date('Wed Jul 01 2015 07:00:00 GMT-0700 (PDT)'); - var expectedVeryBrokenTS = veryBrokenTS + (largeDate.getTimezoneOffset() * 60e3); - expect(veryBrokenDate.getTime()).toBe(expectedVeryBrokenTS); // NaN in Safari 8/9 + var offset = new Date(1970).getTimezoneOffset() * 60e3; + + var timestamp = 2147483647; // Math.pow(2, 31) - 1 + var date = new Date(1970, 0, 1, 0, 0, 0, timestamp); + var expectedTimestamp = timestamp + offset; + expect(date.getTime()).toBe(expectedTimestamp); + + var brokenTimestamp = 2147483648; // Math.pow(2, 31) + var brokenDate = new Date(1970, 0, 1, 0, 0, 0, brokenTimestamp); + var expectedBrokenTimestamp = brokenTimestamp + offset; + expect(brokenDate.getTime()).toBe(expectedBrokenTimestamp); // NaN in Safari 8/9 + + var veryBrokenTS = 1435734000000; + var veryBrokenDate = new Date(1970, 0, 1, 0, 0, 0, veryBrokenTS); + var largeDate = new Date('Wed Jul 01 2015 07:00:00 GMT-0700 (PDT)'); + var expectedVeryBrokenTS = veryBrokenTS + (largeDate.getTimezoneOffset() * 60e3); + expect(veryBrokenDate.getTime()).toBe(expectedVeryBrokenTS); // NaN in Safari 8/9 }); }); @@ -80,11 +164,11 @@ describe('Date', function () { // TODO: Write the rest of the test. ifSupportsDescriptorsIt('is not enumerable', function () { - expect(Object.getOwnPropertyDescriptor(Date, 'parse').enumerable).toBe(false); + expect(Object.getOwnPropertyDescriptor(Date, 'parse').enumerable).toBe(false); }); it('should be an invalid date', function () { - // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 + // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 expect(Date.parse('2012-11-31T23:59:59.000Z')).toBeFalsy(); // 1354406399000 NaN NaN 1354406399000 NaN expect(Date.parse('2012-12-31T23:59:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356998400000 expect(Date.parse('2012-04-04T24:00:00.500Z')).toBeFalsy(); // NaN NaN 1333584000500 1333584000500 NaN @@ -102,7 +186,7 @@ describe('Date', function () { it('should work', function () { var dates = { - // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 Safari 8 + // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 Safari 8 '2012-12-31T23:59:59.000Z': 1356998399000, // 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000 '2012-04-04T05:02:02.170Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 '2012-04-04T05:02:02.170999Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170.999 @@ -120,7 +204,7 @@ describe('Date', function () { // https://github.com/es-shims/es5-shim/issues/80 Safari bug with leap day expect(Date.parse('2034-03-01T00:00:00.000Z') - - Date.parse('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1 + Date.parse('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1 }); @@ -130,7 +214,7 @@ describe('Date', function () { }); it('should support extended years', function () { - // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 + // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 expect(Date.parse('0000-01-01T00:00:00.000Z')).toBe(-621672192e5); // -621672192e5 -621672192e5 -621672192e5 -621672192e5 -621672192e5 expect(Date.parse('0001-01-01T00:00:00Z')).toBe(-621355968e5); // -621355968e5 -621355968e5 -621355968e5 8.64e15 -621355968e5 expect(Date.parse('+275760-09-13T00:00:00.000Z')).toBe(8.64e15); // 8.64e15 NaN 8.64e15 8.64e15 8.64e15 @@ -143,7 +227,7 @@ describe('Date', function () { }); it('works with timezone offsets', function () { - // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 + // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 expect(Date.parse('2012-01-29T12:00:00.000+01:00')).toBe(132783480e4); // 132783480e4 132783480e4 132783480e4 132783480e4 132783480e4 expect(Date.parse('2012-01-29T12:00:00.000-00:00')).toBe(132783840e4); // 132783840e4 132783840e4 132783840e4 132783840e4 132783840e4 expect(Date.parse('2012-01-29T12:00:00.000+00:00')).toBe(132783840e4); // 132783840e4 132783840e4 132783840e4 132783840e4 132783840e4 @@ -211,14 +295,43 @@ describe('Date', function () { describe('#getUTCDate()', function () { it('should return the right value for negative dates', function () { // Opera 10.6/11.61/Opera 12 bug - expect(new Date(-3509827334573292).getUTCDate()).toBe(1); + negativeDate.forEach(function (item) { + item.dates.forEach(function (date, index) { + expect(date.getUTCDate()).toBe(item.days[index], date); + }); + }); + }); + }); + + describe('#getUTCDay()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date, index) { + expect(date.getUTCDay()).toBe(item.getUTCDay[index]); + }); + }); + }); + }); + + describe('#getUTCFullYear()', function () { + it('should return the right value for negative dates', function () { + // Opera 10.6/11.61/Opera 12 bug + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getUTCFullYear()).toBe(-109252); + }); + }); }); }); describe('#getUTCMonth()', function () { it('should return the right value for negative dates', function () { // Opera 10.6/11.61/Opera 12 bug - expect(new Date(-3509827334573292).getUTCMonth()).toBe(0); + negativeDate.forEach(function (item, index) { + item.dates.forEach(function (date) { + expect(date.getUTCMonth()).toBe(index); + }); + }); }); it('should return correct values', function () { @@ -227,6 +340,132 @@ describe('Date', function () { }); }); + describe('#getUTCHours()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getUTCHours()).toBe(11); + }); + }); + }); + }); + + describe('#getUTCMinutes()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getUTCMinutes()).toBe(59); + }); + }); + }); + }); + + describe('#getUTCSeconds()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getUTCSeconds()).toBe(59); + }); + }); + }); + }); + + describe('#getUTCMilliseconds()', function () { + it('should return the right value for negative dates', function () { + // Opera 10.6/11.61/Opera 12 bug + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getUTCMilliseconds()).toBe(708); + }); + }); + }); + }); + + describe('#getDate()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date, index) { + expect(date.getDate()).toBe(item.days[index]); + }); + }); + }); + }); + + describe('#getDay()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date, index) { + expect(date.getDay()).toBe(item.getDay[index]); + }); + }); + }); + }); + + describe('#getFullYear()', function () { + it('should return the right value for negative dates', function () { + // Opera 10.6/11.61/Opera 12 bug + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getFullYear()).toBe(-109252); + }); + }); + }); + }); + + describe('#getMonth()', function () { + it('should return the right value for negative dates', function () { + // Opera 10.6/11.61/Opera 12 bug + negativeDate.forEach(function (item, index) { + item.dates.forEach(function (date) { + expect(date.getMonth()).toBe(index); + }); + }); + }); + }); + + describe('#getHours()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getHours() + Math.floor(timeZoneOffset / 60)).toBe(11); + }); + }); + }); + }); + + describe('#getMinutes()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + var offHours = Math.floor(timeZoneOffset / 60); + var offMins = timeZoneOffset - offHours * 60; + expect(date.getMinutes() + offMins).toBe(59); + }); + }); + }); + }); + + describe('#getSeconds()', function () { + it('should return the right value for negative dates', function () { + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getSeconds()).toBe(59); + }); + }); + }); + }); + + describe('#getMilliseconds()', function () { + it('should return the right value for negative dates', function () { + // Opera 10.6/11.61/Opera 12 bug + negativeDate.forEach(function (item) { + item.dates.forEach(function (date) { + expect(date.getMilliseconds()).toBe(708); + }); + }); + }); + }); + describe('#toISOString()', function () { // TODO: write the rest of the test. @@ -236,33 +475,59 @@ describe('Date', function () { }); it('should return correct dates', function () { - expect(new Date(-1).toISOString()).toBe('1969-12-31T23:59:59.999Z');// Safari 5.1.5 "1969-12-31T23:59:59.-01Z" - expect(new Date(-3509827334573292).toISOString()).toBe('-109252-01-01T10:37:06.708Z'); // Opera 11.61/Opera 12 bug with Date#getUTCMonth + expect(new Date(-1).toISOString()).toBe('1969-12-31T23:59:59.999Z'); // Safari 5.1.5 "1969-12-31T23:59:59.-01Z" + negativeDate.forEach(function (item, index) { + var m = index + 1; + item.dates.forEach(function (date, idx) { + var d = item.days[idx]; + expect(date.toISOString()).toBe('-109252-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d) + 'T11:59:59.708Z'); // Opera 11.61/Opera 12 bug with Date#getUTCMonth + }); + }); }); }); + describe('#toUTCString()', function () { + it('should return correct dates', function () { + expect(new Date(-1509842289600292).toUTCString()).toBe('Mon, 01 Jan -45875 11:59:59 GMT'); + }); + }); + + describe('#toDateString()', function () { + it('should return correct dates', function () { + expect(new Date(-1509842289600292).toDateString()).toBe('Mon Jan 01 -45875'); + }); + }); + + describe('#toString()', function () { + it('should return correct dates', function () { + var actual = new Date(1449662400000).toString(); + var re = /^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-\+]\d\d\d\d(?: |$)/; + expect(re.test(actual)).toBe(true, actual); + }); + }); + describe('#toJSON()', function () { // Opera 11.6x/12 bug it('should call toISOString', function () { - var date = new Date(0); - date.toISOString = function () { - return 1; - }; - expect(date.toJSON()).toBe(1); + var date = new Date(0); + date.toISOString = function () { + return 1; + }; + expect(date.toJSON()).toBe(1); }); it('should return null for not finite dates', function () { - var date = new Date(NaN), - json; - try { - json = date.toJSON(); - } catch (e) { - /* invalid json */ - expect(e).not.toEqual(jasmine.any(Error)); - } - expect(json).toBe(null); + var date = new Date(NaN), + json; + try { + json = date.toJSON(); + } catch (e) { + /* invalid json */ + expect(e).not.toEqual(jasmine.any(Error)); + } + expect(json).toBe(null); }); it('should return the isoString when stringified', function () {