Debian Codegraph @

js/src/tests/non262/Date/parse-milliseconds.js

raw
1 /* -*- indent-tabs-mode: nil; js-indent-level: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 const accepted = {
7 "Sep 26 1995 12:34:56.789": "1995-09-26T12:34:56.789",
8 "Sep 26 1995 12:34:56.7": "1995-09-26T12:34:56.700",
9 "Sep 26 1995 12:34:56.78": "1995-09-26T12:34:56.780",
10 "Sep 26 1995 12:34:56.001": "1995-09-26T12:34:56.001",
11
12 "Sep 26 1995 12:34:56.789Z": "1995-09-26T12:34:56.789Z",
13 "Sep 26 1995 12:34:56.789 -0500": "1995-09-26T12:34:56.789-0500",
14 "Sep 26 1995 12:34:56.789-0500": "1995-09-26T12:34:56.789-0500",
15
16 // Note that the trailing '.' without milliseconds is rejected.
17 // Rejecting this case could be done trivially, but Chrome allows it,
18 // so we will allow it for parity.
19 "Sep 26 1995 12:34:56.789.": "1995-09-26T12:34:56.789",
20
21 // Truncate after 3 digits
22 "Sep 26 1995 12:34:56.0001": "1995-09-26T12:34:56",
23 "Sep 26 1995 12:34:56.0009": "1995-09-26T12:34:56",
24 "Sep 26 1995 12:34:56.999": "1995-09-26T12:34:56.999",
25 "Sep 26 1995 12:34:56.9990": "1995-09-26T12:34:56.999",
26 "Sep 26 1995 12:34:56.9999": "1995-09-26T12:34:56.999",
27 // Before bug 746529, it would have rounded up here:
28 "Sep 26 1995 12:34:56.99999999999999996": "1995-09-26T12:34:56.999",
29 "Sep 26 1995 12:34:56.50099999999999996": "1995-09-26T12:34:56.500",
30 "Sep 26 1995 12:34:56.00099999999999996": "1995-09-26T12:34:56",
31 "Sep 26 1995 12:34:56.000999999999999999999": "1995-09-26T12:34:56",
32 };
33 const rejected = [
34 "Sep 26 1995 12:34:56.",
35 "Sep 26 1995 12:34:56:789",
36 "Sep 26 1995 12:34:56..789",
37 ];
38
39 // Sanity check to make sure these are being parsed to the right precision
40 // Otherwise, the comparisons in the above object could still pass
41 assertEq(Date.parse("1970-01-01T00:00:00.99999999999999996Z"), 999);
42
43 for (const [test, expected] of Object.entries(accepted)) {
44 const testDate = new Date(test);
45 const expectedDate = new Date(expected);
46
47 assertEq(
48 false, isNaN(testDate),
49 `${test} should be accepted.`
50 );
51
52 assertEq(
53 testDate.getTime(), expectedDate.getTime(),
54 `"${test}" should be ${expectedDate} (got ${testDate}).`
55 );
56 }
57
58 for (const reject of rejected) {
59 assertEq(
60 true, isNaN(new Date(reject)),
61 `"${reject}" should be rejected.`
62 );
63 }
64
65 if (typeof reportCompare === "function")
66 reportCompare(true, true);