Date.parse.js 10 KB

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