2
0

s-date.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. describe('Date', function () {
  2. describe('now', function () {
  3. it('should be the current time', function () {
  4. expect(Date.now() === new Date().getTime()).toBe(true);
  5. });
  6. });
  7. describe("parse", function () {
  8. // TODO: Write the rest of the test.
  9. it('should support extended years', function () {
  10. expect(Date.parse('0001-01-01T00:00:00Z')).toBe(-62135596800000);
  11. expect(Date.parse('+275760-09-13T00:00:00.000Z')).toBe(8.64e15);
  12. expect(Date.parse('+033658-09-27T01:46:40.000Z')).toBe(1e15);
  13. expect(Date.parse('-000001-01-01T00:00:00Z')).toBe(-62198755200000);
  14. expect(Date.parse('+002009-12-15T00:00:00Z')).toBe(1260835200000);
  15. });
  16. it('should work', function () {
  17. //Chrome 19 Opera 12 Firefox 11 IE 9 Safari 5.1.1
  18. expect(Date.parse("2012-11-31T23:59:59.000Z")).toBeFalsy(); //1354406399000 NaN NaN 1354406399000 NaN
  19. expect(Date.parse("2012-12-31T23:59:59.000Z")).toBe(1356998399000); //1356998399000 1356998399000 1356998399000 1356998399000 1356998399000
  20. expect(Date.parse("2012-12-31T23:59:60.000Z")).toBeFalsy(); //NaN NaN NaN NaN 1356998400000
  21. expect(Date.parse("2012-04-04T05:02:02.170Z")).toBe(1333515722170); //1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  22. expect(Date.parse("2012-04-04T05:02:02.170999Z")).toBe(1333515722170); //1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  23. expect(Date.parse("2012-04-04T05:02:02.17Z")).toBe(1333515722170); //1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  24. expect(Date.parse("2012-04-04T05:02:02.1Z")).toBe(1333515722100); //1333515722170 1333515722170 1333515722170 1333515722170 1333515722170
  25. expect(Date.parse("2012-04-04T24:00:00.000Z")).toBe(1333584000000); //NaN 1333584000000 1333584000000 1333584000000 1333584000000
  26. expect(Date.parse("2012-04-04T24:00:00.500Z")).toBeFalsy(); //NaN NaN 1333584000500 1333584000500 NaN
  27. expect(Date.parse("2012-12-31T10:08:60.000Z")).toBeFalsy(); //NaN NaN NaN NaN 1356948540000
  28. expect(Date.parse("2012-13-01T12:00:00.000Z")).toBeFalsy(); //NaN NaN NaN NaN NaN
  29. expect(Date.parse("2012-12-32T12:00:00.000Z")).toBeFalsy(); //NaN NaN NaN NaN NaN
  30. expect(Date.parse("2012-12-31T25:00:00.000Z")).toBeFalsy(); //NaN NaN NaN NaN NaN
  31. expect(Date.parse("2012-12-31T24:01:00.000Z")).toBeFalsy(); //NaN NaN NaN 1356998460000 NaN
  32. expect(Date.parse("2012-12-31T12:60:00.000Z")).toBeFalsy(); //NaN NaN NaN NaN NaN
  33. expect(Date.parse("2012-12-31T12:00:60.000Z")).toBeFalsy(); //NaN NaN NaN NaN 1356955260000
  34. expect(Date.parse("2012-00-31T23:59:59.000Z")).toBeFalsy(); //NaN NaN NaN NaN NaN
  35. expect(Date.parse("2012-12-00T23:59:59.000Z")).toBeFalsy(); //NaN NaN NaN NaN NaN
  36. expect(Date.parse("2012-02-29T12:00:00.000Z")).toBe(1330516800000); //1330516800000 1330516800000 1330516800000 1330516800000 1330516800000
  37. expect(Date.parse("2011-02-29T12:00:00.000Z")).toBeFalsy(); //1298980800000 NaN NaN 1298980800000 NaN
  38. expect(Date.parse("2011-03-01T12:00:00.000Z")).toBe(1298980800000); //1298980800000 1298980800000 1298980800000 1298980800000 1298980800000
  39. // extended years:
  40. expect(Date.parse("0000-01-01T00:00:00.000Z")).toBe(-621672192e5); //-621672192e5 -621672192e5 -621672192e5 -621672192e5 -621672192e5
  41. expect(Date.parse("+275760-09-13T00:00:00.000Z")).toBe(8.64e15); //8.64e15 NaN 8.64e15 8.64e15 8.64e15
  42. expect(Date.parse("-271821-04-20T00:00:00.000Z")).toBe(-8.64e15); //-8.64e15 NaN -8.64e15 -8.64e15 -8.6400000864e15
  43. expect(Date.parse("+275760-09-13T00:00:00.001Z")).toBeFalsy(); //NaN NaN NaN 8.64e15 + 1 8.64e15 + 1
  44. expect(Date.parse("-271821-04-19T23:59:59.999Z")).toBeFalsy(); //NaN NaN NaN -8.64e15 - 1 -8.6400000864e15 - 1
  45. // https://github.com/kriskowal/es5-shim/issues/80 Safari bug with leap day
  46. expect(Date.parse("2034-03-01T00:00:00.000Z") -
  47. Date.parse("2034-02-27T23:59:59.999Z")).toBe(86400001); //86400001 86400001 86400001 86400001 1
  48. // Time Zone Offset
  49. expect(Date.parse("2012-01-29T12:00:00.000+01:00")).toBe(132783480e4);//132783480e4 132783480e4 132783480e4 132783480e4 132783480e4
  50. expect(Date.parse("2012-01-29T12:00:00.000-00:00")).toBe(132783840e4);//132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
  51. expect(Date.parse("2012-01-29T12:00:00.000+00:00")).toBe(132783840e4);//132783840e4 132783840e4 132783840e4 132783840e4 132783840e4
  52. expect(Date.parse("2012-01-29T12:00:00.000+23:59")).toBe(132775206e4);//132775206e4 132775206e4 132775206e4 132775206e4 132775206e4
  53. expect(Date.parse("2012-01-29T12:00:00.000-23:59")).toBe(132792474e4);//132792474e4 132792474e4 132792474e4 132792474e4 132792474e4
  54. expect(Date.parse("2012-01-29T12:00:00.000+24:00")).toBeFalsy(); //NaN 1327752e6 NaN 1327752000000 1327752000000
  55. expect(Date.parse("2012-01-29T12:00:00.000+24:01")).toBeFalsy(); //NaN NaN NaN 1327751940000 1327751940000
  56. expect(Date.parse("2012-01-29T12:00:00.000+24:59")).toBeFalsy(); //NaN NaN NaN 1327748460000 1327748460000
  57. expect(Date.parse("2012-01-29T12:00:00.000+25:00")).toBeFalsy(); //NaN NaN NaN NaN NaN
  58. expect(Date.parse("2012-01-29T12:00:00.000+00:60")).toBeFalsy(); //NaN NaN NaN NaN NaN
  59. expect(Date.parse("-271821-04-20T00:00:00.000+00:01")).toBeFalsy(); //NaN NaN NaN -864000000006e4 -864000008646e4
  60. expect(Date.parse("-271821-04-20T00:01:00.000+00:01")).toBe(-8.64e15);//-8.64e15 NaN -8.64e15 -8.64e15 -864000008640e4
  61. // When time zone is missed, local offset should be used (ES 5.1 bug)
  62. // see https://bugs.ecmascript.org/show_bug.cgi?id=112
  63. var tzOffset = Number(new Date(1970, 0));
  64. // same as (new Date().getTimezoneOffset() * 60000)
  65. expect(Date.parse('1970-01-01T00:00:00')).toBe(tzOffset); //tzOffset 0 0 0 NaN
  66. });
  67. it("should be able to coerce to a number", function(){
  68. var actual = Number(new Date(1970, 0));
  69. var expected = parseInt(actual, 10);
  70. expect(actual).toBeDefined();
  71. expect(actual).toEqual(expected);
  72. expect(isNaN(actual)).toBeFalsy();
  73. });
  74. });
  75. describe("toString", function(){
  76. var actual = (new Date(1970, 0)).toString();
  77. beforeEach(function(){
  78. actual = (new Date(1970, 0)).toString();
  79. });
  80. it("should show correct date info for "+actual, function(){
  81. expect(actual).toMatch(/1970/);
  82. expect(actual).toMatch(/jan/i);
  83. expect(actual).toMatch(/thu/i);
  84. expect(actual).toMatch(/00:00:00/);
  85. });
  86. });
  87. describe("valueOf", function(){
  88. var actual = (new Date(1970, 0));
  89. beforeEach(function(){
  90. actual = (new Date(1970, 0)).valueOf();
  91. });
  92. it("should give an int value", function(){
  93. expect(parseInt(actual, 10)).toBeTruthy();
  94. });
  95. });
  96. describe("toISOString", function () {
  97. // TODO: write the rest of the test.
  98. it('should support extended years', function () {
  99. expect(new Date(-62198755200000).toISOString().indexOf('-000001-01-01')).toBe(0);
  100. expect(new Date(8.64e15).toISOString().indexOf('+275760-09-13')).toBe(0);
  101. });
  102. it('should return correct dates', function () {
  103. expect(new Date(-1).toISOString()).toBe('1969-12-31T23:59:59.999Z');// Safari 5.1.5 "1969-12-31T23:59:59.-01Z"
  104. expect(new Date(-3509827334573292).toISOString()).toBe('-109252-01-01T10:37:06.708Z'); // Opera 11.61/Opera 12 bug with Date#getUTCMonth
  105. });
  106. });
  107. describe("toJSON", function () {
  108. // Opera 11.6x/12 bug
  109. it('should call toISOString', function () {
  110. var date = new Date(0);
  111. date.toISOString = function () {
  112. return 1;
  113. };
  114. expect(date.toJSON()).toBe(1);
  115. });
  116. it('should return null for not finite dates', function () {
  117. var date = new Date(NaN),
  118. json;
  119. try {
  120. json = date.toJSON();
  121. } catch (e) {}
  122. expect(json).toBe(null);
  123. });
  124. it('should return the isoString when stringified', function () {
  125. var date = new Date();
  126. expect(JSON.stringify(date.toISOString())).toBe(JSON.stringify(date));
  127. })
  128. });
  129. });