s-date.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. describe('Date', function () {
  2. 'use strict';
  3. var supportsDescriptors = Object.defineProperty && (function () {
  4. try {
  5. var obj = {};
  6. Object.defineProperty(obj, 'x', {
  7. enumerable: false,
  8. value: obj
  9. });
  10. // eslint-disable-next-line no-unreachable-loop
  11. for (var _ in obj) { return false; } // jscs:ignore disallowUnusedVariables
  12. return obj.x === obj;
  13. } catch (e) { /* this is ES3 */
  14. return false;
  15. }
  16. }());
  17. var ifSupportsDescriptorsIt = supportsDescriptors ? it : xit;
  18. var has = Object.prototype.hasOwnProperty;
  19. var negativeDate;
  20. beforeEach(function () {
  21. var negativeCanned = [
  22. {
  23. getTime: -3509827329600292,
  24. getUTCDay: 4,
  25. getDay: 4,
  26. dim: 31
  27. },
  28. {
  29. getTime: -3509824651200292,
  30. getUTCDay: 0,
  31. getDay: 0,
  32. dim: 29
  33. },
  34. {
  35. getTime: -3509822145600292,
  36. getUTCDay: 1,
  37. getDay: 1,
  38. dim: 31
  39. },
  40. {
  41. getTime: -3509819467200292,
  42. getUTCDay: 4,
  43. getDay: 4,
  44. dim: 30
  45. },
  46. {
  47. getTime: -3509816875200292,
  48. getUTCDay: 6,
  49. getDay: 6,
  50. dim: 31
  51. },
  52. {
  53. getTime: -3509814196800292,
  54. getUTCDay: 2,
  55. getDay: 2,
  56. dim: 30
  57. },
  58. {
  59. getTime: -3509811604800292,
  60. getUTCDay: 4,
  61. getDay: 4,
  62. dim: 31
  63. },
  64. {
  65. getTime: -3509808926400292,
  66. getUTCDay: 0,
  67. getDay: 0,
  68. dim: 31
  69. },
  70. {
  71. getTime: -3509806248000292,
  72. getUTCDay: 3,
  73. getDay: 3,
  74. dim: 30
  75. },
  76. {
  77. getTime: -3509803656000292,
  78. getUTCDay: 5,
  79. getDay: 5,
  80. dim: 31
  81. },
  82. {
  83. getTime: -3509800977600292,
  84. getUTCDay: 1,
  85. getDay: 1,
  86. dim: 30
  87. },
  88. {
  89. getTime: -3509798385600292,
  90. getUTCDay: 3,
  91. getDay: 3,
  92. dim: 31
  93. }
  94. ];
  95. negativeDate = negativeCanned.map(function (item) {
  96. var dateFirst = new Date(item.getTime);
  97. var dateLast = new Date(item.getTime + ((item.dim - 1) * 86400000));
  98. return {
  99. dates: [dateFirst, dateLast],
  100. days: [1, item.dim],
  101. getUTCDay: [item.getUTCDay, (item.getUTCDay + item.dim - 1) % 7],
  102. getDay: [item.getDay, (item.getDay + item.dim - 1) % 7]
  103. };
  104. });
  105. });
  106. describe('.now()', function () {
  107. it('should be the current time', function () {
  108. var before = (new Date()).getTime();
  109. var middle = Date.now();
  110. var after = (new Date()).getTime();
  111. expect(middle).not.toBeLessThan(before);
  112. expect(middle).not.toBeGreaterThan(after);
  113. });
  114. });
  115. describe('constructor', function () {
  116. it('works with standard formats', function () {
  117. // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
  118. expect(+new Date('2012-12-31T23:59:59.000Z')).toBe(1356998399000); // 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000
  119. expect(+new Date('2012-04-04T05:02:02.170Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  120. expect(+new Date('2012-04-04T05:02:02.170999Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  121. expect(+new Date('2012-04-04T05:02:02.17Z')).toBe(1333515722170); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  122. expect(+new Date('2012-04-04T05:02:02.1Z')).toBe(1333515722100); // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  123. expect(+new Date('2012-04-04T24:00:00.000Z')).toBe(1333584000000); // NaN 1333584000000 1333584000000 1333584000000 1333584000000
  124. expect(+new Date('2012-02-29T12:00:00.000Z')).toBe(1330516800000); // 1330516800000 1330516800000 1330516800000 1330516800000 1330516800000
  125. expect(+new Date('2011-03-01T12:00:00.000Z')).toBe(1298980800000); // 1298980800000 1298980800000 1298980800000 1298980800000 1298980800000
  126. // https://github.com/es-shims/es5-shim/issues/80 Safari bug with leap day
  127. expect(new Date('2034-03-01T00:00:00.000Z')
  128. - new Date('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1
  129. });
  130. ifSupportsDescriptorsIt('is not enumerable', function () {
  131. expect(Object.keys(new Date())).not.toContain('constructor');
  132. });
  133. it('works as a function', function () {
  134. var zeroDate = Date(0);
  135. expect(zeroDate).toBe(String(zeroDate));
  136. var value = Date(1441705534578);
  137. expect(value).toBe(String(value));
  138. });
  139. it('fixes this Safari 8/9 bug', function () {
  140. var offset = new Date(1970).getTimezoneOffset() * 60e3;
  141. var timestamp = 2147483647; // Math.pow(2, 31) - 1
  142. var date = new Date(1970, 0, 1, 0, 0, 0, timestamp);
  143. var expectedTimestamp = timestamp + offset;
  144. expect(date.getTime()).toBe(expectedTimestamp);
  145. var brokenTimestamp = 2147483648; // Math.pow(2, 31)
  146. var brokenDate = new Date(1970, 0, 1, 0, 0, 0, brokenTimestamp);
  147. var expectedBrokenTimestamp = brokenTimestamp + offset;
  148. expect(brokenDate.getTime()).toBe(expectedBrokenTimestamp); // NaN in Safari 8/9
  149. var veryBrokenTS = 1435734000000;
  150. var veryBrokenDate = new Date(1970, 0, 1, 0, 0, 0, veryBrokenTS);
  151. var largeDate = new Date('Wed Jul 01 2015 07:00:00 GMT-0700 (PDT)');
  152. var expectedVeryBrokenTS = veryBrokenTS + (largeDate.getTimezoneOffset() * 60e3);
  153. expect(veryBrokenDate.getTime()).toBe(expectedVeryBrokenTS); // NaN in Safari 8/9
  154. });
  155. it('works with a Date object', function () {
  156. var date = new Date(1456297712984);
  157. var copiedDate = new Date(date);
  158. expect(date).not.toBe(copiedDate);
  159. expect(copiedDate.getTime()).toBe(date.getTime());
  160. expect(+copiedDate).toBe(+date);
  161. expect(String(copiedDate)).toBe(String(date));
  162. });
  163. });
  164. describe('.parse()', function () {
  165. // TODO: Write the rest of the test.
  166. ifSupportsDescriptorsIt('is not enumerable', function () {
  167. expect(Object.getOwnPropertyDescriptor(Date, 'parse').enumerable).toBe(false);
  168. });
  169. it('should be an invalid date', function () {
  170. // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
  171. expect(Date.parse('2012-11-31T23:59:59.000Z')).toBeFalsy(); // 1354406399000 NaN NaN 1354406399000 NaN
  172. expect(Date.parse('2012-12-31T23:59:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356998400000
  173. expect(Date.parse('2012-04-04T24:00:00.500Z')).toBeFalsy(); // NaN NaN 1333584000500 1333584000500 NaN
  174. expect(Date.parse('2012-12-31T10:08:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356948540000
  175. expect(Date.parse('2012-13-01T12:00:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
  176. expect(Date.parse('2012-12-32T12:00:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
  177. expect(Date.parse('2012-12-31T25:00:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
  178. expect(Date.parse('2012-12-31T24:01:00.000Z')).toBeFalsy(); // NaN NaN NaN 1356998460000 NaN
  179. expect(Date.parse('2012-12-31T12:60:00.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
  180. expect(Date.parse('2012-12-31T12:00:60.000Z')).toBeFalsy(); // NaN NaN NaN NaN 1356955260000
  181. expect(Date.parse('2012-00-31T23:59:59.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
  182. expect(Date.parse('2012-12-00T23:59:59.000Z')).toBeFalsy(); // NaN NaN NaN NaN NaN
  183. expect(Date.parse('2011-02-29T12:00:00.000Z')).toBeFalsy(); // 1298980800000 NaN NaN 1298980800000 NaN
  184. });
  185. it('should work', function () {
  186. var dates = {
  187. // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1 Safari 8
  188. '2012-12-31T23:59:59.000Z': 1356998399000, // 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000 1356998399000
  189. '2012-04-04T05:02:02.170Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  190. '2012-04-04T05:02:02.170999Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170.999
  191. '2012-04-04T05:02:02.17Z': 1333515722170, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  192. '2012-04-04T05:02:02.1Z': 1333515722100, // 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  193. '2012-04-04T24:00:00.000Z': 1333584000000, // NaN 1333584000000 1333584000000 1333584000000 1333584000000 1333584000000
  194. '2012-02-29T12:00:00.000Z': 1330516800000, // 1330516800000 1330516800000 1330516800000 1330516800000 1330516800000 1330516800000
  195. '2011-03-01T12:00:00.000Z': 1298980800000 // 1298980800000 1298980800000 1298980800000 1298980800000 1298980800000 1298980800000
  196. };
  197. for (var str in dates) {
  198. if (has.call(dates, str)) {
  199. expect(Math.floor(Date.parse(str))).toBe(dates[str]);
  200. }
  201. }
  202. // https://github.com/es-shims/es5-shim/issues/80 Safari bug with leap day
  203. expect(Date.parse('2034-03-01T00:00:00.000Z')
  204. - Date.parse('2034-02-27T23:59:59.999Z')).toBe(86400001); // 86400001 86400001 86400001 86400001 1
  205. });
  206. it('fixes a Safari 8/9 bug with parsing in UTC instead of local time', function () {
  207. var offset = new Date('2015-07-01').getTimezoneOffset() * 60e3;
  208. expect(Date.parse('2015-07-01T00:00:00')).toBe(1435708800000 + offset); // Safari 8/9 give NaN
  209. });
  210. it('should support extended years', function () {
  211. // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
  212. expect(Date.parse('0000-01-01T00:00:00.000Z')).toBe(-621672192e5); // -621672192e5 -621672192e5 -621672192e5 -621672192e5 -621672192e5
  213. expect(Date.parse('0001-01-01T00:00:00Z')).toBe(-621355968e5); // -621355968e5 -621355968e5 -621355968e5 8.64e15 -621355968e5
  214. expect(Date.parse('+275760-09-13T00:00:00.000Z')).toBe(8.64e15); // 8.64e15 NaN 8.64e15 8.64e15 8.64e15
  215. expect(Date.parse('-271821-04-20T00:00:00.000Z')).toBe(-8.64e15); // -8.64e15 NaN -8.64e15 -8.64e15 -8.6400000864e15
  216. expect(Date.parse('+275760-09-13T00:00:00.001Z')).toBeFalsy(); // NaN NaN NaN 8.64e15 + 1 8.64e15 + 1
  217. expect(Date.parse('-271821-04-19T23:59:59.999Z')).toBeFalsy(); // NaN NaN NaN -8.64e15 - 1 -8.6400000864e15 - 1
  218. expect(Date.parse('+033658-09-27T01:46:40.000Z')).toBe(1e15); // 1e15 NaN 1e15 1e15 9999999136e5
  219. expect(Date.parse('-000001-01-01T00:00:00Z')).toBe(-621987552e5); // -621987552e5 NaN -621987552e5 -621987552e5 -621987552e5
  220. expect(Date.parse('+002009-12-15T00:00:00Z')).toBe(12608352e5); // 12608352e5 NaN 12608352e5 12608352e5 12608352e5
  221. });
  222. it('works with timezone offsets', function () {
  223. // Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
  224. expect(Date.parse('2012-01-29T12:00:00.000+01:00')).toBe(132783480e4); // 132783480e4 132783480e4 132783480e4 132783480e4 132783480e4
  225. expect(Date.parse('2012-01-29T12:00:00.000-00:00')).toBe(132783840e4); // 132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
  226. expect(Date.parse('2012-01-29T12:00:00.000+00:00')).toBe(132783840e4); // 132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
  227. expect(Date.parse('2012-01-29T12:00:00.000+23:59')).toBe(132775206e4); // 132775206e4 132775206e4 132775206e4 132775206e4 132775206e4
  228. expect(Date.parse('2012-01-29T12:00:00.000-23:59')).toBe(132792474e4); // 132792474e4 132792474e4 132792474e4 132792474e4 132792474e4
  229. expect(Date.parse('2012-01-29T12:00:00.000+24:00')).toBeFalsy(); // NaN 1327752e6 NaN 1327752000000 1327752000000
  230. expect(Date.parse('2012-01-29T12:00:00.000+24:01')).toBeFalsy(); // NaN NaN NaN 1327751940000 1327751940000
  231. expect(Date.parse('2012-01-29T12:00:00.000+24:59')).toBeFalsy(); // NaN NaN NaN 1327748460000 1327748460000
  232. expect(Date.parse('2012-01-29T12:00:00.000+25:00')).toBeFalsy(); // NaN NaN NaN NaN NaN
  233. expect(Date.parse('2012-01-29T12:00:00.000+00:60')).toBeFalsy(); // NaN NaN NaN NaN NaN
  234. expect(Date.parse('-271821-04-20T00:00:00.000+00:01')).toBeFalsy(); // NaN NaN NaN -864000000006e4 -864000008646e4
  235. expect(Date.parse('-271821-04-20T00:01:00.000+00:01')).toBe(-8.64e15); // -8.64e15 NaN -8.64e15 -8.64e15 -864000008640e4
  236. // When time zone is missed, local offset should be used (ES 5.1 bug)
  237. // see https://bugs.ecmascript.org/show_bug.cgi?id=112
  238. var tzOffset = Number(new Date(1970, 0));
  239. // same as (new Date().getTimezoneOffset() * 60000)
  240. expect(Date.parse('1970-01-01T00:00:00')).toBe(tzOffset); // tzOffset 0 0 0 NaN
  241. });
  242. it('should be able to coerce to a number', function () {
  243. var actual = Number(new Date(1970, 0));
  244. var expected = parseInt(actual, 10);
  245. expect(actual).toBeDefined();
  246. expect(actual).toBe(expected);
  247. expect(isNaN(actual)).toBeFalsy();
  248. });
  249. it('matches web reality', function () {
  250. var actual = Number(new Date('1900-01-01T00:00:00.000'));
  251. var upperBound = -2208988800000; // safari 6.2 - 13.1
  252. var expected = -2208960000000;
  253. expect(actual).toBeDefined();
  254. expect(actual).toBeGreaterThan(upperBound - 1);
  255. expect(actual).toBeLessThan(expected + 1);
  256. // expect(actual).toBe(expected); // TODO: figure out if `upperBound` is a bug or just a difference
  257. expect(isNaN(actual)).toBeFalsy();
  258. });
  259. });
  260. describe('#toString()', function () {
  261. var actual;
  262. beforeEach(function () {
  263. actual = (new Date(1970, 0)).toString();
  264. });
  265. it('should show correct date info for ' + actual, function () {
  266. expect(actual).toMatch(/1970/);
  267. expect(actual).toMatch(/jan/i);
  268. expect(actual).toMatch(/thu/i);
  269. expect(actual).toMatch(/00:00:00/);
  270. });
  271. });
  272. describe('#valueOf()', function () {
  273. // Note that new Date(1970, 0).valueOf() is 0 in UTC timezone.
  274. // Check check that it's a number (and an int), not that it's "truthy".
  275. var actual;
  276. beforeEach(function () {
  277. actual = (new Date(1970, 0)).valueOf();
  278. });
  279. it('should give a numeric value', function () {
  280. expect(typeof actual).toBe('number');
  281. });
  282. it('should not be NaN', function () {
  283. expect(isNaN(actual)).toBe(false);
  284. });
  285. it('should give an int value', function () {
  286. expect(actual).toBe(Math.floor(actual));
  287. });
  288. });
  289. describe('#getUTCDate()', function () {
  290. it('should return the right value for negative dates', function () {
  291. // Opera 10.6/11.61/Opera 12 bug
  292. negativeDate.forEach(function (item) {
  293. item.dates.forEach(function (date, index) {
  294. expect(date.getUTCDate()).toBe(item.days[index], date);
  295. });
  296. });
  297. });
  298. });
  299. describe('#getUTCDay()', function () {
  300. it('should return the right value for negative dates', function () {
  301. negativeDate.forEach(function (item) {
  302. item.dates.forEach(function (date, index) {
  303. expect(date.getUTCDay()).toBe(item.getUTCDay[index]);
  304. });
  305. });
  306. });
  307. });
  308. describe('#getUTCFullYear()', function () {
  309. it('should return the right value for negative dates', function () {
  310. // Opera 10.6/11.61/Opera 12 bug
  311. negativeDate.forEach(function (item) {
  312. item.dates.forEach(function (date) {
  313. expect(date.getUTCFullYear()).toBe(-109252);
  314. });
  315. });
  316. });
  317. });
  318. describe('#getUTCMonth()', function () {
  319. it('should return the right value for negative dates', function () {
  320. // Opera 10.6/11.61/Opera 12 bug
  321. negativeDate.forEach(function (item, index) {
  322. item.dates.forEach(function (date) {
  323. expect(date.getUTCMonth()).toBe(index);
  324. });
  325. });
  326. });
  327. it('should return correct values', function () {
  328. expect(new Date(8.64e15).getUTCMonth()).toBe(8);
  329. expect(new Date(0).getUTCMonth()).toBe(0);
  330. });
  331. });
  332. describe('#getUTCHours()', function () {
  333. it('should return the right value for negative dates', function () {
  334. negativeDate.forEach(function (item) {
  335. item.dates.forEach(function (date) {
  336. expect(date.getUTCHours()).toBe(11);
  337. });
  338. });
  339. });
  340. });
  341. describe('#getUTCMinutes()', function () {
  342. it('should return the right value for negative dates', function () {
  343. negativeDate.forEach(function (item) {
  344. item.dates.forEach(function (date) {
  345. expect(date.getUTCMinutes()).toBe(59);
  346. });
  347. });
  348. });
  349. });
  350. describe('#getUTCSeconds()', function () {
  351. it('should return the right value for negative dates', function () {
  352. negativeDate.forEach(function (item) {
  353. item.dates.forEach(function (date) {
  354. expect(date.getUTCSeconds()).toBe(59);
  355. });
  356. });
  357. });
  358. });
  359. describe('#getUTCMilliseconds()', function () {
  360. it('should return the right value for negative dates', function () {
  361. // Opera 10.6/11.61/Opera 12 bug
  362. negativeDate.forEach(function (item) {
  363. item.dates.forEach(function (date) {
  364. expect(date.getUTCMilliseconds()).toBe(708);
  365. });
  366. });
  367. });
  368. });
  369. describe('#getDate()', function () {
  370. it('should return the right value for negative dates', function () {
  371. negativeDate.forEach(function (item) {
  372. item.dates.forEach(function (date, index) {
  373. expect(date.getDate()).toBe(item.days[index]);
  374. });
  375. });
  376. });
  377. });
  378. describe('#getDay()', function () {
  379. it('should return the right value for negative dates', function () {
  380. negativeDate.forEach(function (item) {
  381. item.dates.forEach(function (date, index) {
  382. expect(date.getDay()).toBe(item.getDay[index]);
  383. });
  384. });
  385. });
  386. });
  387. describe('#getFullYear()', function () {
  388. it('should return the right value for negative dates', function () {
  389. // Opera 10.6/11.61/Opera 12 bug
  390. negativeDate.forEach(function (item) {
  391. item.dates.forEach(function (date) {
  392. expect(date.getFullYear()).toBe(-109252);
  393. });
  394. });
  395. });
  396. });
  397. describe('#getMonth()', function () {
  398. it('should return the right value for negative dates', function () {
  399. // Opera 10.6/11.61/Opera 12 bug
  400. negativeDate.forEach(function (item, index) {
  401. item.dates.forEach(function (date) {
  402. expect(date.getMonth()).toBe(index);
  403. });
  404. });
  405. });
  406. });
  407. describe('#getHours()', function () {
  408. it('should return the right value for negative dates', function () {
  409. negativeDate.forEach(function (item) {
  410. item.dates.forEach(function (date) {
  411. expect(date.getHours() + Math.floor(date.getTimezoneOffset() / 60)).toBe(11);
  412. });
  413. });
  414. });
  415. });
  416. describe('#getMinutes()', function () {
  417. it('should return the right value for negative dates', function () {
  418. negativeDate.forEach(function (item) {
  419. item.dates.forEach(function (date) {
  420. var off = date.getTimezoneOffset();
  421. var offHours = Math.floor(off / 60);
  422. var offMins = off - (offHours * 60);
  423. var result = date.getMinutes() + offMins;
  424. // ceil/floor is for Firefox
  425. expect(result < 0 ? Math.ceil(result) : Math.floor(result)).toBe(59);
  426. });
  427. });
  428. });
  429. });
  430. describe('#getSeconds()', function () {
  431. it('should return the right value for negative dates', function () {
  432. negativeDate.forEach(function (item) {
  433. item.dates.forEach(function (date, i) {
  434. // the regex here is because in UTC, it's 59, but with TZData applied,
  435. // which can have fractional hour offsets, it'll be 1.
  436. expect(i + ':' + date.getSeconds()).toMatch(new RegExp(i + ':(?:' + 59 + '|' + 1 + ')'));
  437. });
  438. });
  439. });
  440. });
  441. describe('#getMilliseconds()', function () {
  442. it('should return the right value for negative dates', function () {
  443. // Opera 10.6/11.61/Opera 12 bug
  444. negativeDate.forEach(function (item) {
  445. item.dates.forEach(function (date) {
  446. expect(date.getMilliseconds()).toBe(708);
  447. });
  448. });
  449. });
  450. });
  451. describe('#toISOString()', function () {
  452. // TODO: write the rest of the test.
  453. it('should support extended years', function () {
  454. expect(new Date(-62198755200000).toISOString().indexOf('-000001-01-01')).toBe(0);
  455. expect(new Date(8.64e15).toISOString().indexOf('+275760-09-13')).toBe(0);
  456. });
  457. it('should return correct dates', function () {
  458. expect(new Date(-1).toISOString()).toBe('1969-12-31T23:59:59.999Z'); // Safari 5.1.5 "1969-12-31T23:59:59.-01Z"
  459. negativeDate.forEach(function (item, index) {
  460. var m = index + 1;
  461. item.dates.forEach(function (date, idx) {
  462. var d = item.days[idx];
  463. 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
  464. });
  465. });
  466. });
  467. });
  468. describe('#toUTCString()', function () {
  469. it('should return correct dates', function () {
  470. expect(new Date(-1509842289600292).toUTCString()).toBe('Mon, 01 Jan -45875 11:59:59 GMT');
  471. });
  472. });
  473. describe('#toDateString()', function () {
  474. it('should return correct dates', function () {
  475. expect(new Date(-1509842289600292).toDateString()).toBe('Mon Jan 01 -45875');
  476. });
  477. });
  478. describe('#toString()', function () {
  479. it('should return correct dates', function () {
  480. var actual = new Date(1449662400000).toString();
  481. var re = /^Wed Dec 09 2015 \d\d:\d\d:\d\d GMT[-+]\d\d\d\d(?: |$)/;
  482. expect(re.test(actual)).toBe(true, actual);
  483. });
  484. });
  485. describe('#toJSON()', function () {
  486. // Opera 11.6x/12 bug
  487. it('should call toISOString', function () {
  488. var date = new Date(0);
  489. date.toISOString = function () {
  490. return 1;
  491. };
  492. expect(date.toJSON()).toBe(1);
  493. });
  494. it('should return null for not finite dates', function () {
  495. var date = new Date(NaN),
  496. json;
  497. try {
  498. json = date.toJSON();
  499. } catch (e) {
  500. /* invalid json */
  501. expect(e).not.toEqual(jasmine.any(Error));
  502. }
  503. expect(json).toBe(null);
  504. });
  505. it('should return the isoString when stringified', function () {
  506. var date = new Date();
  507. expect(JSON.stringify(date.toISOString())).toBe(JSON.stringify(date));
  508. });
  509. });
  510. });