Selaa lähdekoodia

LibJS: Accept and ignore calendar annotation in Instant strings

This is a normative change in the Temporal spec.

See: https://github.com/tc39/proposal-temporal/commit/3cd9669
Luke Wilde 2 vuotta sitten
vanhempi
commit
9643a5c63f

+ 2 - 1
Userland/Libraries/LibJS/Runtime/Temporal/ISO8601.cpp

@@ -1592,13 +1592,14 @@ bool ISO8601Parser::parse_duration()
 bool ISO8601Parser::parse_temporal_instant_string()
 {
     // TemporalInstantString :
-    //     Date TimeSpecSeparator[opt] TimeZoneOffsetRequired
+    //     Date TimeSpecSeparator[opt] TimeZoneOffsetRequired Calendar[opt]
     StateTransaction transaction { *this };
     if (!parse_date())
         return false;
     (void)parse_time_spec_separator();
     if (!parse_time_zone_offset_required())
         return false;
+    (void)parse_calendar();
     transaction.commit();
     return true;
 }

+ 25 - 0
Userland/Libraries/LibJS/Tests/builtins/Temporal/Instant/Instant.from.js

@@ -23,6 +23,22 @@ describe("correct behavior", () => {
             Temporal.Instant.from("1975-02-02T14:25:36.123456789Z[Custom/TimeZone]")
                 .epochNanoseconds
         ).toBe(160583136123456789n);
+
+        // Accepts but ignores the calendar.
+        let result = null;
+        expect(() => {
+            result = Temporal.Instant.from("1970-01-01T00:00Z[u-ca=UTC]");
+        }).not.toThrow();
+        expect(result).toBeInstanceOf(Temporal.Instant);
+        expect(result.epochNanoseconds).toBe(0n);
+
+        // Does not validate calendar name, it only checks that the calendar name matches the grammar.
+        result = null;
+        expect(() => {
+            result = Temporal.Instant.from("1970-01-01T00:00Z[u-ca=aAaAaAaA-bBbBbBb]");
+        }).not.toThrow();
+        expect(result).toBeInstanceOf(Temporal.Instant);
+        expect(result.epochNanoseconds).toBe(0n);
     });
 });
 
@@ -48,4 +64,13 @@ describe("errors", () => {
             "Invalid epoch nanoseconds value, must be in range -86400 * 10^17 to 86400 * 10^17"
         );
     });
+
+    test("calendar annotation must match calendar grammar even though it's ignored", () => {
+        expect(() => {
+            Temporal.Instant.from("1970-01-01T00:00Z[u-ca=SerenityOS]");
+        }).toThrowWithMessage(
+            RangeError,
+            "Invalid instant string '1970-01-01T00:00Z[u-ca=SerenityOS]'"
+        );
+    });
 });