Date.parse.js 10 KB

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