Date.parse.js 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. test("basic functionality", () => {
  2. expect(Date.parse("2020")).toBe(1577836800000);
  3. expect(Date.parse("2000-11")).toBe(973036800000);
  4. expect(Date.parse("1980-06-30")).toBe(331171200000);
  5. expect(Date.parse("1970-06-30T13:30Z")).toBe(15600600000);
  6. expect(Date.parse("1970-01-01T00:00:59Z")).toBe(59000);
  7. expect(Date.parse("1970-01-01T00:00:00.999Z")).toBe(999);
  8. expect(Date.parse("2020T13:14+15:16")).toBe(1577829480000);
  9. expect(Date.parse("2020T13:14-15:16")).toBe(1577939400000);
  10. expect(Date.parse("2020T23:59Z")).toBe(1577923140000);
  11. expect(Date.parse("+020000")).toBe(568971820800000);
  12. expect(Date.parse("-000001")).toBe(-62198755200000);
  13. expect(Date.parse("+020000-01")).toBe(568971820800000);
  14. expect(Date.parse("+020000-01T00:00:00.000Z")).toBe(568971820800000);
  15. const originalTimeZone = setTimeZone("UTC");
  16. expect(Date.parse("1980-5-30")).toBe(328492800000);
  17. setTimeZone("America/Chicago");
  18. expect(Date.parse("Jan 01 1970 GMT")).toBe(0);
  19. expect(Date.parse("Wed Apr 17 23:08:53 2019 +0000")).toBe(1555542533000);
  20. expect(Date.parse("2021-07-01 03:00Z")).toBe(1625108400000);
  21. expect(Date.parse("2024-01-08 9:00Z")).toBe(1704704400000);
  22. expect(Date.parse("Wed, 17 Jan 2024 11:36:34 +0000")).toBe(1705491394000);
  23. expect(Date.parse("Sun Jan 21 2024 21:11:31 GMT 0100 (Central European Standard Time)")).toBe(
  24. 1705867891000
  25. );
  26. expect(Date.parse("2024-01-15 00:00:01")).toBe(1705298401000);
  27. expect(Date.parse("Tue Nov 07 2023 10:05:55 UTC")).toBe(1699351555000);
  28. expect(Date.parse("Wed Apr 17 23:08:53 2019")).toBe(1555560533000);
  29. expect(Date.parse("2024-01-26T22:10:11.306+0000")).toBe(1706307011000); // FIXME: support sub-second precision
  30. expect(Date.parse("1/27/2024, 9:28:30 AM")).toBe(1706369310000);
  31. // FIXME: Create a scoped time zone helper when bytecode supports the `using` declaration.
  32. setTimeZone(originalTimeZone);
  33. expect(Date.parse(2020)).toBe(1577836800000);
  34. expect(Date.parse("+1980")).toBe(NaN);
  35. expect(Date.parse("1980-")).toBe(NaN);
  36. expect(Date.parse("1980-05-")).toBe(NaN);
  37. expect(Date.parse("1980-05-00T")).toBe(NaN);
  38. expect(Date.parse("1980-05-00T15:15:")).toBe(NaN);
  39. expect(Date.parse("1980-05-00T15:15:15.")).toBe(NaN);
  40. expect(Date.parse("1980-05-30T13")).toBe(NaN);
  41. expect(Date.parse("1980-05-30T13:4")).toBe(NaN);
  42. expect(Date.parse("1980-05-30T13:40+")).toBe(NaN);
  43. expect(Date.parse("1980-05-30T13:40+1")).toBe(NaN);
  44. expect(Date.parse("1980-05-30T13:40+1:10")).toBe(NaN);
  45. expect(Date.parse("1970-06-30T13:30Zoo")).toBe(NaN);
  46. expect(Date.parse("2020T13:30.40:")).toBe(NaN);
  47. expect(Date.parse("-000000")).toBe(NaN);
  48. });
  49. test("time clip", () => {
  50. expect(Date.parse("+999999")).toBeNaN();
  51. expect(Date.parse("-999999")).toBeNaN();
  52. });
  53. test("extra micro seconds extension", () => {
  54. expect(Date.parse("2021-04-30T15:19:02.937+00:00")).toBe(1619795942937);
  55. expect(Date.parse("2021-04-30T15:19:02.9370+00:00")).toBe(1619795942937);
  56. expect(Date.parse("2021-04-30T15:19:02.93700+00:00")).toBe(1619795942937);
  57. expect(Date.parse("2021-04-30T15:19:02.937000+00:00")).toBe(1619795942937);
  58. expect(Date.parse("2021-04-30T15:19:02.93+00:00")).toBe(1619795942930);
  59. expect(Date.parse("2021-04-30T15:19:02.9+00:00")).toBe(1619795942900);
  60. // These values are just checked against NaN since they don't have a specified timezone.
  61. expect(Date.parse("2021-04-30T15:19:02.93")).not.toBe(NaN);
  62. expect(Date.parse("2021-04-30T15:19:02.9")).not.toBe(NaN);
  63. expect(Date.parse("2021-04-30T15:19:02.+00:00")).toBe(NaN);
  64. expect(Date.parse("2021-04-30T15:19:02.")).toBe(NaN);
  65. expect(Date.parse("2021-04-30T15:19:02.a")).toBe(NaN);
  66. expect(Date.parse("2021-04-30T15:19:02.000a")).toBe(NaN);
  67. expect(Date.parse("2021-04-30T15:19:02.937001+00:00")).toBe(1619795942937);
  68. expect(Date.parse("2021-04-30T15:19:02.937999+00:00")).toBe(1619795942937);
  69. expect(Date.parse("2021-06-26T07:24:40.007000+00:00")).toBe(1624692280007);
  70. expect(Date.parse("2021-06-26T07:24:40.0079999999999999999+00:00")).toBe(1624692280007);
  71. expect(Date.parse("2021-04-15T18:47:25.606000+00:00")).toBe(1618512445606);
  72. });
  73. test("extra date extension", () => {
  74. function expectStringToGiveDate(input, fullYear, month, dayInMonth) {
  75. // Since the timezone is not specified we just say it has to equal the date parts.
  76. const date = new Date(Date.parse(input));
  77. expect(date.getFullYear()).toBe(fullYear);
  78. expect(date.getMonth() + 1).toBe(month);
  79. expect(date.getDate()).toBe(dayInMonth);
  80. }
  81. expectStringToGiveDate("01/30/2021", 2021, 1, 30);
  82. expectStringToGiveDate("10/1/2021", 2021, 10, 1);
  83. expectStringToGiveDate("7/07/1977", 1977, 7, 7);
  84. expectStringToGiveDate("2/27/3058", 3058, 2, 27);
  85. });
  86. test("mm/dd/yy hh:mm timezone-offset extension", () => {
  87. // Examples from Discord's JavaScript for Christmas 2022.
  88. expect(Date.parse("12/05/2022 10:00 -0800")).toBe(1670263200000);
  89. expect(Date.parse("01/03/2023 10:00 -0800")).toBe(1672768800000);
  90. });
  91. test("yy{/,-}mm{/,-}dd hh:mm extension", () => {
  92. function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours, minutes) {
  93. // Since the timezone is not specified we just say it has to equal the date parts.
  94. const date = new Date(Date.parse(input));
  95. expect(date.getFullYear()).toBe(fullYear);
  96. expect(date.getMonth() + 1).toBe(month);
  97. expect(date.getDate()).toBe(dayInMonth);
  98. expect(date.getHours()).toBe(hours);
  99. expect(date.getMinutes()).toBe(minutes);
  100. }
  101. // Example from a UK news website.
  102. expectStringToGiveDate("2014/11/14 13:05", 2014, 11, 14, 13, 5);
  103. expectStringToGiveDate("2014-11-14 13:05", 2014, 11, 14, 13, 5);
  104. });
  105. test("Month dd, yy extension", () => {
  106. function expectStringToGiveDate(input, fullYear, month, dayInMonth) {
  107. const date = new Date(Date.parse(input));
  108. expect(date.getFullYear()).toBe(fullYear);
  109. expect(date.getMonth() + 1).toBe(month);
  110. expect(date.getDate()).toBe(dayInMonth);
  111. }
  112. expectStringToGiveDate("May 15, 2023", 2023, 5, 15);
  113. expectStringToGiveDate("May 22, 2023", 2023, 5, 22);
  114. expectStringToGiveDate("May 30, 2023", 2023, 5, 30);
  115. expectStringToGiveDate("June 5, 2023", 2023, 6, 5);
  116. });
  117. test("Month dd, yy hh:mm:ss extension", () => {
  118. function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours, minutes, seconds) {
  119. // Since the timezone is not specified we just say it has to equal the date parts.
  120. const date = new Date(Date.parse(input));
  121. expect(date.getFullYear()).toBe(fullYear);
  122. expect(date.getMonth() + 1).toBe(month);
  123. expect(date.getDate()).toBe(dayInMonth);
  124. expect(date.getHours()).toBe(hours);
  125. expect(date.getMinutes()).toBe(minutes);
  126. expect(date.getSeconds()).toBe(seconds);
  127. }
  128. // Examples from Discord's Birthday JavaScript for May 2023.
  129. expectStringToGiveDate("May 15, 2023 17:00:00", 2023, 5, 15, 17, 0, 0);
  130. expectStringToGiveDate("May 22, 2023 17:00:00", 2023, 5, 22, 17, 0, 0);
  131. expectStringToGiveDate("May 30, 2023 17:00:00", 2023, 5, 30, 17, 0, 0);
  132. expectStringToGiveDate("June 5, 2023 17:00:00", 2023, 6, 5, 17, 0, 0);
  133. });
  134. test("Date.prototype.toString extension", () => {
  135. function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours) {
  136. const date = new Date(Date.parse(input));
  137. expect(date.getFullYear()).toBe(fullYear);
  138. expect(date.getMonth() + 1).toBe(month);
  139. expect(date.getDate()).toBe(dayInMonth);
  140. expect(date.getHours()).toBe(hours);
  141. }
  142. const time1 = "Tue Nov 07 2023 10:00:00 GMT-0500 (Eastern Standard Time)";
  143. const time2 = "Tue Nov 07 2023 10:00:00 GMT+0100 (Central European Standard Time)";
  144. const time3 = "Tue Nov 07 2023 10:00:00 GMT+0800 (Australian Western Standard Time)";
  145. const originalTimeZone = setTimeZone("UTC");
  146. expectStringToGiveDate(time1, 2023, 11, 7, 15);
  147. expectStringToGiveDate(time2, 2023, 11, 7, 9);
  148. expectStringToGiveDate(time3, 2023, 11, 7, 2);
  149. setTimeZone("America/New_York");
  150. expectStringToGiveDate(time1, 2023, 11, 7, 10);
  151. expectStringToGiveDate(time2, 2023, 11, 7, 4);
  152. expectStringToGiveDate(time3, 2023, 11, 6, 21);
  153. setTimeZone("Australia/Perth");
  154. expectStringToGiveDate(time1, 2023, 11, 7, 23);
  155. expectStringToGiveDate(time2, 2023, 11, 7, 17);
  156. expectStringToGiveDate(time3, 2023, 11, 7, 10);
  157. // FIXME: Create a scoped time zone helper when bytecode supports the `using` declaration.
  158. setTimeZone(originalTimeZone);
  159. });
  160. test("Date.prototype.toUTCString extension", () => {
  161. function expectStringToGiveDate(input, fullYear, month, dayInMonth, hours) {
  162. const date = new Date(Date.parse(input));
  163. expect(date.getFullYear()).toBe(fullYear);
  164. expect(date.getMonth() + 1).toBe(month);
  165. expect(date.getDate()).toBe(dayInMonth);
  166. expect(date.getHours()).toBe(hours);
  167. }
  168. const time = "Tue, 07 Nov 2023 15:00:00 GMT";
  169. const originalTimeZone = setTimeZone("UTC");
  170. expectStringToGiveDate(time, 2023, 11, 7, 15);
  171. setTimeZone("America/New_York");
  172. expectStringToGiveDate(time, 2023, 11, 7, 10);
  173. setTimeZone("Australia/Perth");
  174. expectStringToGiveDate(time, 2023, 11, 7, 23);
  175. // FIXME: Create a scoped time zone helper when bytecode supports the `using` declaration.
  176. setTimeZone(originalTimeZone);
  177. });
  178. test("Round trip Date.prototype.to*String", () => {
  179. const epoch = new Date(0);
  180. expect(Date.parse(epoch.toString())).toBe(epoch.valueOf());
  181. expect(Date.parse(epoch.toISOString())).toBe(epoch.valueOf());
  182. expect(Date.parse(epoch.toUTCString())).toBe(epoch.valueOf());
  183. });