parse-dashed-numeric-date.js (4947B)
1 // |reftest| skip-if(winWidget) -- Windows doesn't accept IANA names for the TZ env variable 2 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 inTimeZone("America/Phoenix", () => { 8 // The upper limit is from the TimeClip algorithm: 9 // https://tc39.es/ecma262/#sec-timeclip 10 // https://tc39.es/ecma262/#sec-utc-t 11 // 12 // The result is in localtime but the limit is processed in GMT, which 13 // is why these are offset from the actual limit of 275760-09-13T00:00:00 14 // (in this case by 7h, because we're testing in MST) 15 const accepted = { 16 "19999-09-12": new Date(19999, Month.September, 12), 17 "19999-9-1": new Date(19999, Month.September, 1), 18 "19999-9-1 ": new Date(19999, Month.September, 1), 19 "275760-09-12": new Date(275760, Month.September, 12), 20 "275760-09-12 17:00:00": new Date(275760, Month.September, 12, 17), 21 "19999-09-12 (comment) 23:00:00": new Date(19999, Month.September, 12, 23), 22 "19999-09-12 22:00:00 GMT-0800": new Date(19999, Month.September, 12, 23), 23 24 // Single digit mon or mday 25 "2021-09-1": new Date(2021, Month.September, 1), 26 "2021-9-01": new Date(2021, Month.September, 1), 27 "2021-9-1": new Date(2021, Month.September, 1), 28 29 // 1-12 for first number should be month 30 "1-09-12": new Date(2012, Month.January, 9), 31 "1-09-0012": new Date(2012, Month.January, 9), 32 "1-09-2012": new Date(2012, Month.January, 9), 33 "12-09-12": new Date(2012, Month.December, 9), 34 35 // 32-99 for first number is the year... 36 // 32-49 are 2032-2049, 50-99 are 1950-1999 37 "32-09-12": new Date(2032, Month.September, 12), 38 "49-09-12": new Date(2049, Month.September, 12), 39 "50-09-12": new Date(1950, Month.September, 12), 40 "99-09-12": new Date(1999, Month.September, 12), 41 42 // Year 0 is 2000 43 "0-9-12": new Date(2000, Month.September, 12), 44 "9-12-0": new Date(2000, Month.September, 12), 45 46 // 3-digit year is also fine 47 "999-09-12": new Date(999, Month.September, 12), 48 49 // Bug 1617258 (result is -7 hours) 50 "2020-03-24 12:54:40 AM +00:00": new Date(2020, Month.March, 23, 17, 54, 40), 51 52 // Oddball time formats for Chrome parity 53 "9999-09-12 :00:01": new Date(9999, Month.September, 12, 0, 1), 54 // TODO: This one still does not have parity- bug 1858595 55 "9999-09-12 01::01": new Date(9999, Month.September, 12, 1, 1, 0), 56 57 // mday > # of days in mon rolls over to the next month 58 // as long as mday <= 31 (2022 is not a leap year) 59 // These are processed as ISO dates, therefore are in GMT 60 "2022-02-29": new Date(2022, Month.February, 28, 17), 61 "2022-02-30": new Date(2022, Month.March, 1, 17), 62 "2022-02-31": new Date(2022, Month.March, 2, 17), 63 64 // No space before time zone 65 "19999-9-1MST": new Date(19999, Month.September, 1), 66 "19999-9-1GMT-07": new Date(19999, Month.September, 1), 67 68 // Delimiter other than space after prefix 69 "19999-9-1.10:13:14": new Date(19999, Month.September, 1, 10, 13, 14), 70 "19999-9-1,10:13:14": new Date(19999, Month.September, 1, 10, 13, 14), 71 "19999-9-1-10:13:14": new Date(19999, Month.September, 1, 10, 13, 14), 72 "19999-9-1-4:30": new Date(19999, Month.September, 1, 4, 30), 73 "19999-9-1/10:13:14": new Date(19999, Month.September, 1, 10, 13, 14), 74 "19999-9-1()10:13:14": new Date(19999, Month.September, 1, 10, 13, 14), 75 // Open paren only comments out the time 76 "19999-9-1(10:13:14": new Date(19999, Month.September, 1), 77 }; 78 const rejected = [ 79 "275760-09-12 17:00:01", 80 "275760-09-13", 81 82 // Rejected delimiters after prefix 83 "19999-09-12T00:00:00", 84 "19999-09-12:00:00:00", 85 "19999-09-12^00:00:00", 86 "19999-09-12|00:00:00", 87 "19999-09-12~00:00:00", 88 "19999-09-12+00:00:00", 89 "19999-09-12=00:00:00", 90 "19999-09-12?00:00:00", 91 92 // 13-31 for first number is invalid (after 31 can be parsed as YY-MM-DD), 93 // but 32 is still no good if the last number is a YYYY 94 "13-09-12", 95 "13-09-2012", 96 "31-09-12", 97 "31-09-2012", 98 "32-09-2012", 99 100 // mday > 31 is invalid, does not roll over to the next month 101 "2022-02-32", 102 // month > 12 103 "2022-13-30", 104 105 // 00 for mon or mday 106 "0000-00-00", 107 "0000-01-00", 108 "0000-00-01", 109 ]; 110 111 for (const [test, dateObject] of Object.entries(accepted)) { 112 const testDate = new Date(test); 113 114 assertEq( 115 false, isNaN(testDate), 116 `${test} should be accepted.` 117 ); 118 119 assertEq( 120 testDate.getTime(), dateObject.getTime(), 121 `"${test}" should be ${dateObject} (got ${testDate}).` 122 ); 123 } 124 125 for (const reject of rejected) { 126 assertEq( 127 true, isNaN(new Date(reject)), 128 `"${reject}" should be rejected.` 129 ); 130 } 131 }); 132 133 if (typeof reportCompare === "function") 134 reportCompare(true, true);