date_test.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  1. package goja
  2. import (
  3. "testing"
  4. "time"
  5. )
  6. func TestDateUTC(t *testing.T) {
  7. const SCRIPT = `
  8. assert.sameValue(Date.UTC(1970, 0), 0, '1970, 0');
  9. assert.sameValue(Date.UTC(2016, 0), 1451606400000, '2016, 0');
  10. assert.sameValue(Date.UTC(2016, 6), 1467331200000, '2016, 6');
  11. assert.sameValue(Date.UTC(2016, 6, 1), 1467331200000, '2016, 6, 1');
  12. assert.sameValue(Date.UTC(2016, 6, 5), 1467676800000, '2016, 6, 5');
  13. assert.sameValue(Date.UTC(2016, 6, 5, 0), 1467676800000, '2016, 6, 5, 0');
  14. assert.sameValue(Date.UTC(2016, 6, 5, 15), 1467730800000, '2016, 6, 5, 15');
  15. assert.sameValue(
  16. Date.UTC(2016, 6, 5, 15, 0), 1467730800000, '2016, 6, 5, 15, 0'
  17. );
  18. assert.sameValue(
  19. Date.UTC(2016, 6, 5, 15, 34), 1467732840000, '2016, 6, 5, 15, 34'
  20. );
  21. assert.sameValue(
  22. Date.UTC(2016, 6, 5, 15, 34, 0), 1467732840000, '2016, 6, 5, 15, 34, 0'
  23. );
  24. assert.sameValue(
  25. Date.UTC(2016, 6, 5, 15, 34, 45), 1467732885000, '2016, 6, 5, 15, 34, 45'
  26. );
  27. `
  28. testScriptWithTestLib(SCRIPT, _undefined, t)
  29. }
  30. func TestNewDate(t *testing.T) {
  31. const SCRIPT = `
  32. var d1 = new Date("2016-09-01T12:34:56Z");
  33. d1.getUTCHours() === 12;
  34. `
  35. testScript(SCRIPT, valueTrue, t)
  36. }
  37. func TestNewDate0(t *testing.T) {
  38. const SCRIPT = `
  39. (new Date(0)).toUTCString();
  40. `
  41. testScript(SCRIPT, asciiString("Thu, 01 Jan 1970 00:00:00 GMT"), t)
  42. }
  43. func TestSetHour(t *testing.T) {
  44. l := time.Local
  45. defer func() {
  46. time.Local = l
  47. }()
  48. var err error
  49. time.Local, err = time.LoadLocation("America/New_York")
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. const SCRIPT = `
  54. var d = new Date(2016, 8, 1, 12, 23, 45)
  55. assert.sameValue(d.getHours(), 12);
  56. assert.sameValue(d.getUTCHours(), 16);
  57. d.setHours(13);
  58. assert.sameValue(d.getHours(), 13);
  59. assert.sameValue(d.getMinutes(), 23);
  60. assert.sameValue(d.getSeconds(), 45);
  61. d.setUTCHours(13);
  62. assert.sameValue(d.getHours(), 9);
  63. assert.sameValue(d.getMinutes(), 23);
  64. assert.sameValue(d.getSeconds(), 45);
  65. `
  66. testScriptWithTestLib(SCRIPT, _undefined, t)
  67. }
  68. func TestSetMinute(t *testing.T) {
  69. l := time.Local
  70. defer func() {
  71. time.Local = l
  72. }()
  73. time.Local = time.FixedZone("Asia/Delhi", 5*60*60+30*60)
  74. const SCRIPT = `
  75. var d = new Date(2016, 8, 1, 12, 23, 45)
  76. assert.sameValue(d.getHours(), 12);
  77. assert.sameValue(d.getUTCHours(), 6);
  78. assert.sameValue(d.getMinutes(), 23);
  79. assert.sameValue(d.getUTCMinutes(), 53);
  80. d.setMinutes(55);
  81. assert.sameValue(d.getMinutes(), 55);
  82. assert.sameValue(d.getSeconds(), 45);
  83. d.setUTCMinutes(52);
  84. assert.sameValue(d.getMinutes(), 22);
  85. assert.sameValue(d.getHours(), 13);
  86. `
  87. testScriptWithTestLib(SCRIPT, _undefined, t)
  88. }
  89. func TestTimezoneOffset(t *testing.T) {
  90. const SCRIPT = `
  91. var d = new Date(0);
  92. d.getTimezoneOffset();
  93. `
  94. l := time.Local
  95. defer func() {
  96. time.Local = l
  97. }()
  98. var err error
  99. time.Local, err = time.LoadLocation("Europe/London")
  100. if err != nil {
  101. t.Fatal(err)
  102. }
  103. testScript(SCRIPT, intToValue(-60), t)
  104. }
  105. func TestDateValueOf(t *testing.T) {
  106. const SCRIPT = `
  107. var d9 = new Date(1.23e15);
  108. d9.valueOf();
  109. `
  110. testScript(SCRIPT, intToValue(1.23e15), t)
  111. }
  112. func TestDateSetters(t *testing.T) {
  113. const SCRIPT = `
  114. assert.sameValue((new Date(0)).setMilliseconds(2345), 2345, "setMilliseconds(2345)");
  115. assert.sameValue(new Date(1000).setMilliseconds(23450000000000), 23450000001000, "setMilliseconds(23450000000000)");
  116. assert.sameValue((new Date(0)).setUTCMilliseconds(2345), 2345, "setUTCMilliseconds()");
  117. assert.sameValue((new Date(0)).setSeconds(12), 12000, "setSeconds()");
  118. assert.sameValue((new Date(0)).setUTCSeconds(12), 12000, "setUTCSeconds()");
  119. assert.sameValue((new Date(0)).setMinutes(12), 12 * 60 * 1000, "setMinutes()");
  120. assert.sameValue((new Date(0)).setUTCMinutes(12), 12 * 60 * 1000, "setUTCMinutes()");
  121. assert.sameValue((new Date("2016-06-01")).setHours(1), 1464739200000, "setHours()");
  122. assert.sameValue((new Date("2016-06-01")).setUTCHours(1), 1464742800000, "setUTCHours()");
  123. assert.sameValue((new Date(0)).setDate(2), 86400000, "setDate()");
  124. assert.sameValue((new Date(0)).setUTCDate(2), 86400000, "setUTCDate()");
  125. assert.sameValue((new Date(0)).setMonth(2), 5097600000, "setMonth()");
  126. assert.sameValue((new Date(0)).setUTCMonth(2), 5097600000, "setUTCMonth()");
  127. assert.sameValue((new Date(0)).setFullYear(1971), 31536000000, "setFullYear()");
  128. assert.sameValue((new Date(0)).setFullYear(1971, 2, 3), 36806400000, "setFullYear(Y,M,D)");
  129. assert.sameValue((new Date(0)).setUTCFullYear(1971), 31536000000, "setUTCFullYear()");
  130. assert.sameValue((new Date(0)).setUTCFullYear(1971, 2, 3), 36806400000, "setUTCFullYear(Y,M,D)");
  131. var d = new Date();
  132. d.setTime(1151877845000);
  133. assert.sameValue(d.getHours(), 23, "d.getHours()");
  134. `
  135. l := time.Local
  136. defer func() {
  137. time.Local = l
  138. }()
  139. var err error
  140. time.Local, err = time.LoadLocation("Europe/London")
  141. if err != nil {
  142. t.Fatal(err)
  143. }
  144. testScriptWithTestLib(SCRIPT, _undefined, t)
  145. }
  146. func TestDateParse(t *testing.T) {
  147. const SCRIPT = `
  148. var zero = new Date(0);
  149. assert.sameValue(zero.valueOf(), Date.parse(zero.toString()),
  150. "Date.parse(zeroDate.toString())");
  151. assert.sameValue(zero.valueOf(), Date.parse(zero.toUTCString()),
  152. "Date.parse(zeroDate.toUTCString())");
  153. assert.sameValue(zero.valueOf(), Date.parse(zero.toISOString()),
  154. "Date.parse(zeroDate.toISOString())");
  155. function testParse(str, expected) {
  156. assert.sameValue(Date.parse(str), expected, str);
  157. }
  158. testParse("Mon, 02 Jan 2006 15:04:05 MST", 1136239445000);
  159. testParse("Tue, 22 Jun 2021 13:54:40 GMT", 1624370080000);
  160. testParse("Tuesday, 22 Jun 2021 13:54:40 GMT", 1624370080000);
  161. testParse("Mon, 02 Jan 2006 15:04:05 GMT-07:00 (MST)", 1136239445000);
  162. testParse("Mon, 02 Jan 2006 15:04:05 -07:00 (MST)", 1136239445000);
  163. testParse("Monday, 02 Jan 2006 15:04:05 -0700 (MST)", 1136239445000);
  164. testParse("Mon Jan 02 2006 15:04:05 GMT-0700 (GMT Standard Time)", 1136239445000);
  165. testParse("Mon Jan 2 15:04:05 MST 2006", 1136239445000);
  166. testParse("Mon Jan 02 15:04:05 MST 2006", 1136239445000);
  167. testParse("Mon Jan 2 15:04:05 2006", 1136232245000);
  168. testParse("Mon Jan 02 15:04:05 -0700 2006", 1136239445000);
  169. testParse("Mon Jan 02 3:4 PM -0700 2006", 1136239440000);
  170. testParse("December 04, 1986", 534056400000);
  171. testParse("Dec 04, 1986", 534056400000);
  172. testParse("Dec 4, 1986", 534056400000);
  173. testParse("2006-01-02T15:04:05.000Z", 1136214245000);
  174. testParse("2006-06-02T15:04:05.000", 1149275045000);
  175. testParse("2006-01-02T15:04:05", 1136232245000);
  176. testParse("2006-01-02 15:04:05.123", 1136232245123);
  177. testParse("2006-01-02", 1136160000000);
  178. testParse("2006T15:04-0700", 1136153040000);
  179. testParse("2006T15:04+07:00", 1136102640000);
  180. testParse("2006T15:04Z", 1136127840000);
  181. testParse("2019-01-01T12:00:00.52Z", 1546344000520);
  182. testParse("2019-01T12:00:00.52Z", 1546344000520);
  183. testParse("+002019-01-01T12:00:00.52Z", 1546344000520);
  184. var d = new Date("Mon, 02 Jan 2006 15:04:05 MST");
  185. assert.sameValue(d.getUTCHours(), 22,
  186. "new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getUTCHours()");
  187. assert.sameValue(d.getHours(), 17,
  188. "new Date(\"Mon, 02 Jan 2006 15:04:05 MST\").getHours()");
  189. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 zzz"), NaN,
  190. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 zzz\")");
  191. assert.sameValue(Date.parse("Mon, 02 Jan 2006 15:04:05 ZZZ"), NaN,
  192. "Date.parse(\"Mon, 02 Jan 2006 15:04:05 ZZZ\")");
  193. var minDateStr = "-271821-04-20T00:00:00.000Z";
  194. var minDate = new Date(-8640000000000000);
  195. assert.sameValue(minDate.toISOString(), minDateStr, "minDateStr");
  196. assert.sameValue(Date.parse(minDateStr), minDate.valueOf(), "parse minDateStr");
  197. var maxDateStr = "+275760-09-13T00:00:00.000Z";
  198. var maxDate = new Date(8640000000000000);
  199. assert.sameValue(maxDate.toISOString(), maxDateStr, "maxDateStr");
  200. assert.sameValue(Date.parse(maxDateStr), maxDate.valueOf(), "parse maxDateStr");
  201. var belowRange = "-271821-04-19T23:59:59.999Z";
  202. var aboveRange = "+275760-09-13T00:00:00.001Z";
  203. assert.sameValue(Date.parse(belowRange), NaN, "parse below minimum time value");
  204. assert.sameValue(Date.parse(aboveRange), NaN, "parse above maximum time value");
  205. `
  206. l := time.Local
  207. defer func() {
  208. time.Local = l
  209. }()
  210. var err error
  211. time.Local, err = time.LoadLocation("America/New_York")
  212. if err != nil {
  213. t.Fatal(err)
  214. }
  215. testScriptWithTestLib(SCRIPT, _undefined, t)
  216. }
  217. func TestDateMaxValues(t *testing.T) {
  218. const SCRIPT = `
  219. assert.sameValue((new Date(0)).setUTCMilliseconds(8.64e15), 8.64e15);
  220. assert.sameValue((new Date(0)).setUTCSeconds(8640000000000), 8.64e15);
  221. assert.sameValue((new Date(0)).setUTCMilliseconds(-8.64e15), -8.64e15);
  222. assert.sameValue((new Date(0)).setUTCSeconds(-8640000000000), -8.64e15);
  223. `
  224. testScriptWithTestLib(SCRIPT, _undefined, t)
  225. }
  226. func TestDateExport(t *testing.T) {
  227. vm := New()
  228. res, err := vm.RunString(`new Date(1000)`)
  229. if err != nil {
  230. t.Fatal(err)
  231. }
  232. exp := res.Export()
  233. if d, ok := exp.(time.Time); ok {
  234. if d.UnixNano()/1e6 != 1000 {
  235. t.Fatalf("Invalid exported date: %v", d)
  236. }
  237. if loc := d.Location(); loc != time.Local {
  238. t.Fatalf("Invalid timezone: %v", loc)
  239. }
  240. } else {
  241. t.Fatalf("Invalid export type: %T", exp)
  242. }
  243. }
  244. func TestDateToJSON(t *testing.T) {
  245. const SCRIPT = `
  246. Date.prototype.toJSON.call({ toISOString: function () { return 1; } })
  247. `
  248. testScript(SCRIPT, intToValue(1), t)
  249. }
  250. func TestDateExportType(t *testing.T) {
  251. vm := New()
  252. v, err := vm.RunString(`new Date()`)
  253. if err != nil {
  254. t.Fatal(err)
  255. }
  256. if typ := v.ExportType(); typ != typeTime {
  257. t.Fatal(typ)
  258. }
  259. }
  260. func TestDateParseV8(t *testing.T) {
  261. // Taken from https://chromium.googlesource.com/v8/v8/+/refs/heads/main/test/mjsunit/date-parse.js
  262. const SCRIPT = `
  263. const assertEquals = assert.sameValue,
  264. assertTrue = assert;
  265. // Copyright 2008 the V8 project authors. All rights reserved.
  266. // Redistribution and use in source and binary forms, with or without
  267. // modification, are permitted provided that the following conditions are
  268. // met:
  269. //
  270. // * Redistributions of source code must retain the above copyright
  271. // notice, this list of conditions and the following disclaimer.
  272. // * Redistributions in binary form must reproduce the above
  273. // copyright notice, this list of conditions and the following
  274. // disclaimer in the documentation and/or other materials provided
  275. // with the distribution.
  276. // * Neither the name of Google Inc. nor the names of its
  277. // contributors may be used to endorse or promote products derived
  278. // from this software without specific prior written permission.
  279. //
  280. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  281. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  282. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  283. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  284. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  285. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  286. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  287. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  288. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  289. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  290. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  291. // Test that we can parse dates in all the different formats that we
  292. // have to support.
  293. //
  294. // These formats are all supported by KJS but a lot of them are not
  295. // supported by Spidermonkey.
  296. function testDateParse(string) {
  297. var d = Date.parse(string);
  298. assertEquals(946713600000, d, "parse: " + string);
  299. };
  300. // For local time we just test that parsing returns non-NaN positive
  301. // number of milliseconds to make it timezone independent.
  302. function testDateParseLocalTime(string) {
  303. var d = Date.parse("parse-local-time:" + string);
  304. assertTrue(!isNaN(d), "parse-local-time: " + string + " is NaN.");
  305. assertTrue(d > 0, "parse-local-time: " + string + " <= 0.");
  306. };
  307. function testDateParseMisc(array) {
  308. assertEquals(2, array.length, "array [" + array + "] length != 2.");
  309. var string = array[0];
  310. var expected = array[1];
  311. var d = Date.parse(string);
  312. assertEquals(expected, d, "parse-misc: " + string);
  313. }
  314. //
  315. // Test all the formats in UT timezone.
  316. //
  317. var testCasesUT = [
  318. 'Sat, 01-Jan-2000 08:00:00 UT',
  319. 'Sat, 01 Jan 2000 08:00:00 UT',
  320. 'Jan 01 2000 08:00:00 UT',
  321. 'Jan 01 08:00:00 UT 2000',
  322. 'Saturday, 01-Jan-00 08:00:00 UT',
  323. '01 Jan 00 08:00 +0000',
  324. // Ignore weekdays.
  325. 'Mon, 01 Jan 2000 08:00:00 UT',
  326. 'Tue, 01 Jan 2000 08:00:00 UT',
  327. // Ignore prefix that is not part of a date.
  328. '[Saturday] Jan 01 08:00:00 UT 2000',
  329. 'Ignore all of this stuff because it is annoying 01 Jan 2000 08:00:00 UT',
  330. '[Saturday] Jan 01 2000 08:00:00 UT',
  331. 'All of this stuff is really annoying, so it will be ignored Jan 01 2000 08:00:00 UT',
  332. // If the three first letters of the month is a
  333. // month name we are happy - ignore the rest.
  334. 'Sat, 01-Janisamonth-2000 08:00:00 UT',
  335. 'Sat, 01 Janisamonth 2000 08:00:00 UT',
  336. 'Janisamonth 01 2000 08:00:00 UT',
  337. 'Janisamonth 01 08:00:00 UT 2000',
  338. 'Saturday, 01-Janisamonth-00 08:00:00 UT',
  339. '01 Janisamonth 00 08:00 +0000',
  340. // Allow missing space between month and day.
  341. 'Janisamonthandtherestisignored01 2000 08:00:00 UT',
  342. 'Jan01 2000 08:00:00 UT',
  343. // Allow year/month/day format.
  344. 'Sat, 2000/01/01 08:00:00 UT',
  345. // Allow month/day/year format.
  346. 'Sat, 01/01/2000 08:00:00 UT',
  347. // Allow month/day year format.
  348. 'Sat, 01/01 2000 08:00:00 UT',
  349. // Allow comma instead of space after day, month and year.
  350. 'Sat, 01,Jan,2000,08:00:00 UT',
  351. // Seconds are optional.
  352. 'Sat, 01-Jan-2000 08:00 UT',
  353. 'Sat, 01 Jan 2000 08:00 UT',
  354. 'Jan 01 2000 08:00 UT',
  355. 'Jan 01 08:00 UT 2000',
  356. 'Saturday, 01-Jan-00 08:00 UT',
  357. '01 Jan 00 08:00 +0000',
  358. // Allow AM/PM after the time.
  359. 'Sat, 01-Jan-2000 08:00 AM UT',
  360. 'Sat, 01 Jan 2000 08:00 AM UT',
  361. 'Jan 01 2000 08:00 AM UT',
  362. 'Jan 01 08:00 AM UT 2000',
  363. 'Saturday, 01-Jan-00 08:00 AM UT',
  364. '01 Jan 00 08:00 AM +0000',
  365. // White space and stuff in parenthesis is
  366. // apparently allowed in most places where white
  367. // space is allowed.
  368. ' Sat, 01-Jan-2000 08:00:00 UT ',
  369. ' Sat, 01 Jan 2000 08:00:00 UT ',
  370. ' Saturday, 01-Jan-00 08:00:00 UT ',
  371. ' 01 Jan 00 08:00 +0000 ',
  372. ' ()(Sat, 01-Jan-2000) Sat, 01-Jan-2000 08:00:00 UT ',
  373. ' Sat()(Sat, 01-Jan-2000)01 Jan 2000 08:00:00 UT ',
  374. ' Sat,(02)01 Jan 2000 08:00:00 UT ',
  375. ' Sat, 01(02)Jan 2000 08:00:00 UT ',
  376. ' Sat, 01 Jan 2000 (2001)08:00:00 UT ',
  377. ' Sat, 01 Jan 2000 (01)08:00:00 UT ',
  378. ' Sat, 01 Jan 2000 (01:00:00)08:00:00 UT ',
  379. ' Sat, 01 Jan 2000 08:00:00 (CDT)UT ',
  380. ' Sat, 01 Jan 2000 08:00:00 UT((((CDT))))',
  381. ' Saturday, 01-Jan-00 ()(((asfd)))(Sat, 01-Jan-2000)08:00:00 UT ',
  382. ' 01 Jan 00 08:00 ()(((asdf)))(Sat, 01-Jan-2000)+0000 ',
  383. ' 01 Jan 00 08:00 +0000()((asfd)(Sat, 01-Jan-2000)) '];
  384. //
  385. // Test that we do the right correction for different time zones.
  386. // I'll assume that we can handle the same formats as for UT and only
  387. // test a few formats for each of the timezones.
  388. //
  389. // GMT = UT
  390. var testCasesGMT = [
  391. 'Sat, 01-Jan-2000 08:00:00 GMT',
  392. 'Sat, 01-Jan-2000 08:00:00 GMT+0',
  393. 'Sat, 01-Jan-2000 08:00:00 GMT+00',
  394. 'Sat, 01-Jan-2000 08:00:00 GMT+000',
  395. 'Sat, 01-Jan-2000 08:00:00 GMT+0000',
  396. 'Sat, 01-Jan-2000 08:00:00 GMT+00:00', // Interestingly, KJS cannot handle this.
  397. 'Sat, 01 Jan 2000 08:00:00 GMT',
  398. 'Saturday, 01-Jan-00 08:00:00 GMT',
  399. '01 Jan 00 08:00 -0000',
  400. '01 Jan 00 08:00 +0000'];
  401. // EST = UT minus 5 hours.
  402. var testCasesEST = [
  403. 'Sat, 01-Jan-2000 03:00:00 UTC-0500',
  404. 'Sat, 01-Jan-2000 03:00:00 UTC-05:00', // Interestingly, KJS cannot handle this.
  405. 'Sat, 01-Jan-2000 03:00:00 EST',
  406. 'Sat, 01 Jan 2000 03:00:00 EST',
  407. 'Saturday, 01-Jan-00 03:00:00 EST',
  408. '01 Jan 00 03:00 -0500'];
  409. // EDT = UT minus 4 hours.
  410. var testCasesEDT = [
  411. 'Sat, 01-Jan-2000 04:00:00 EDT',
  412. 'Sat, 01 Jan 2000 04:00:00 EDT',
  413. 'Saturday, 01-Jan-00 04:00:00 EDT',
  414. '01 Jan 00 04:00 -0400'];
  415. // CST = UT minus 6 hours.
  416. var testCasesCST = [
  417. 'Sat, 01-Jan-2000 02:00:00 CST',
  418. 'Sat, 01 Jan 2000 02:00:00 CST',
  419. 'Saturday, 01-Jan-00 02:00:00 CST',
  420. '01 Jan 00 02:00 -0600'];
  421. // CDT = UT minus 5 hours.
  422. var testCasesCDT = [
  423. 'Sat, 01-Jan-2000 03:00:00 CDT',
  424. 'Sat, 01 Jan 2000 03:00:00 CDT',
  425. 'Saturday, 01-Jan-00 03:00:00 CDT',
  426. '01 Jan 00 03:00 -0500'];
  427. // MST = UT minus 7 hours.
  428. var testCasesMST = [
  429. 'Sat, 01-Jan-2000 01:00:00 MST',
  430. 'Sat, 01 Jan 2000 01:00:00 MST',
  431. 'Saturday, 01-Jan-00 01:00:00 MST',
  432. '01 Jan 00 01:00 -0700'];
  433. // MDT = UT minus 6 hours.
  434. var testCasesMDT = [
  435. 'Sat, 01-Jan-2000 02:00:00 MDT',
  436. 'Sat, 01 Jan 2000 02:00:00 MDT',
  437. 'Saturday, 01-Jan-00 02:00:00 MDT',
  438. '01 Jan 00 02:00 -0600'];
  439. // PST = UT minus 8 hours.
  440. var testCasesPST = [
  441. 'Sat, 01-Jan-2000 00:00:00 PST',
  442. 'Sat, 01 Jan 2000 00:00:00 PST',
  443. 'Saturday, 01-Jan-00 00:00:00 PST',
  444. '01 Jan 00 00:00 -0800',
  445. // Allow missing time.
  446. 'Sat, 01-Jan-2000 PST'];
  447. // PDT = UT minus 7 hours.
  448. var testCasesPDT = [
  449. 'Sat, 01-Jan-2000 01:00:00 PDT',
  450. 'Sat, 01 Jan 2000 01:00:00 PDT',
  451. 'Saturday, 01-Jan-00 01:00:00 PDT',
  452. '01 Jan 00 01:00 -0700'];
  453. // Local time cases.
  454. var testCasesLocalTime = [
  455. // Allow timezone omission.
  456. 'Sat, 01-Jan-2000 08:00:00',
  457. 'Sat, 01 Jan 2000 08:00:00',
  458. 'Jan 01 2000 08:00:00',
  459. 'Jan 01 08:00:00 2000',
  460. 'Saturday, 01-Jan-00 08:00:00',
  461. '01 Jan 00 08:00'];
  462. // Misc. test cases that result in a different time value.
  463. var testCasesMisc = [
  464. // Special handling for years in the [0, 100) range.
  465. ['Sat, 01 Jan 0 08:00:00 UT', 946713600000], // year 2000
  466. ['Sat, 01 Jan 49 08:00:00 UT', 2493100800000], // year 2049
  467. ['Sat, 01 Jan 50 08:00:00 UT', -631123200000], // year 1950
  468. ['Sat, 01 Jan 99 08:00:00 UT', 915177600000], // year 1999
  469. ['Sat, 01 Jan 100 08:00:00 UT', -59011430400000], // year 100
  470. // Test PM after time.
  471. ['Sat, 01-Jan-2000 08:00 PM UT', 946756800000],
  472. ['Sat, 01 Jan 2000 08:00 PM UT', 946756800000],
  473. ['Jan 01 2000 08:00 PM UT', 946756800000],
  474. ['Jan 01 08:00 PM UT 2000', 946756800000],
  475. ['Saturday, 01-Jan-00 08:00 PM UT', 946756800000],
  476. ['01 Jan 00 08:00 PM +0000', 946756800000]];
  477. // Test different version of the ES5 date time string format.
  478. var testCasesES5Misc = [
  479. ['2000-01-01T08:00:00.000Z', 946713600000],
  480. ['2000-01-01T08:00:00Z', 946713600000],
  481. ['2000-01-01T08:00Z', 946713600000],
  482. ['2000-01T08:00:00.000Z', 946713600000],
  483. ['2000T08:00:00.000Z', 946713600000],
  484. ['2000T08:00Z', 946713600000],
  485. ['2000-01T00:00:00.000-08:00', 946713600000],
  486. ['2000-01T08:00:00.001Z', 946713600001],
  487. ['2000-01T08:00:00.099Z', 946713600099],
  488. ['2000-01T08:00:00.999Z', 946713600999],
  489. ['2000-01T00:00:00.001-08:00', 946713600001],
  490. ['2000-01-01T24:00Z', 946771200000],
  491. ['2000-01-01T24:00:00Z', 946771200000],
  492. ['2000-01-01T24:00:00.000Z', 946771200000],
  493. ['2000-01-01T24:00:00.000Z', 946771200000]];
  494. var testCasesES5MiscNegative = [
  495. '2000-01-01TZ',
  496. '2000-01-01T60Z',
  497. '2000-01-01T60:60Z',
  498. '2000-01-0108:00Z',
  499. '2000-01-01T08Z',
  500. '2000-01-01T24:01',
  501. '2000-01-01T24:00:01',
  502. '2000-01-01T24:00:00.001',
  503. '2000-01-01T24:00:00.999Z'];
  504. // TODO(littledan): This is an hack that could break in historically
  505. // changing timezones that happened on this day, but allows us to
  506. // check the date value for local times.
  507. var localOffset = new Date('2000-01-01').getTimezoneOffset()*1000*60;
  508. // Sanity check which is even more of a hack: in the timezones where
  509. // these tests are likely to be run, the offset is nonzero because
  510. // dates which don't include Z are in the local timezone.
  511. if (this.Intl &&
  512. ["America/Los_Angeles", "Europe/Berlin", "Europe/Madrid"].indexOf(
  513. Intl.DateTimeFormat().resolvedOptions().timeZone) != -1) {
  514. assertTrue(localOffset != 0);
  515. }
  516. var testCasesES2016TZ = [
  517. // If the timezone is absent and time is present, use local time
  518. ['2000-01-02T00:00', 946771200000 + localOffset],
  519. ['2000-01-02T00:00:00', 946771200000 + localOffset],
  520. ['2000-01-02T00:00:00.000', 946771200000 + localOffset],
  521. // If timezone is absent and time is absent, use UTC
  522. ['2000-01-02', 946771200000],
  523. ['2000-01-02', 946771200000],
  524. ['2000-01-02', 946771200000],
  525. ];
  526. // Run all the tests.
  527. testCasesUT.forEach(testDateParse);
  528. testCasesGMT.forEach(testDateParse);
  529. testCasesEST.forEach(testDateParse);
  530. testCasesEDT.forEach(testDateParse);
  531. testCasesCST.forEach(testDateParse);
  532. testCasesCDT.forEach(testDateParse);
  533. testCasesMST.forEach(testDateParse);
  534. testCasesMDT.forEach(testDateParse);
  535. testCasesPST.forEach(testDateParse);
  536. testCasesPDT.forEach(testDateParse);
  537. testCasesLocalTime.forEach(testDateParseLocalTime);
  538. testCasesMisc.forEach(testDateParseMisc);
  539. // ES5 date time string format compliance.
  540. testCasesES5Misc.forEach(testDateParseMisc);
  541. testCasesES5MiscNegative.forEach(function (s) {
  542. assertTrue(isNaN(Date.parse(s)), s + " is not NaN.");
  543. });
  544. testCasesES2016TZ.forEach(testDateParseMisc);
  545. // Test that we can parse our own date format.
  546. // (Dates from 1970 to ~2070 with 150h steps.)
  547. for (var i = 0; i < 24 * 365 * 100; i += 150) {
  548. var ms = i * (3600 * 1000);
  549. var s = (new Date(ms)).toString();
  550. assertEquals(ms, Date.parse(s), "parse own: " + s);
  551. }
  552. // Negative tests.
  553. var testCasesNegative = [
  554. 'May 25 2008 1:30 (PM)) UTC', // Bad unmatched ')' after number.
  555. 'May 25 2008 1:30( )AM (PM)', //
  556. 'a1', // Issue 126448, 53209.
  557. 'nasfdjklsfjoaifg1',
  558. 'x_2',
  559. 'May 25 2008 AAA (GMT)']; // Unknown word after number.
  560. testCasesNegative.forEach(function (s) {
  561. assertTrue(isNaN(Date.parse(s)), s + " is not NaN.");
  562. });
  563. `
  564. testScriptWithTestLib(SCRIPT, _undefined, t)
  565. }