Преглед изворни кода

LibJS: Support non-Gregorian calendars for Intl.DateTimeFormat

This almost worked out of the box, but we need to be sure we pass the
full locale (e.g. en-u-ca-hebrew) and not just the data locale (en) to
ICU.
Timothy Flynn пре 1 година
родитељ
комит
638a6c8c00

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Intl/DateTimeFormatConstructor.cpp

@@ -343,7 +343,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
         // d. Let styles be dataLocaleData.[[styles]].[[<resolvedCalendar>]].
         // e. Let bestFormat be DateTimeStyleFormat(dateStyle, timeStyle, styles).
         auto formatter = ::Locale::DateTimeFormat::create_for_date_and_time_style(
-            date_time_format->data_locale(),
+            date_time_format->locale(),
             time_zone,
             format_options.hour_cycle,
             format_options.hour12,
@@ -421,7 +421,7 @@ ThrowCompletionOr<NonnullGCPtr<DateTimeFormat>> create_date_time_format(VM& vm,
         // h. Else,
         //     i. Let bestFormat be BestFitFormatMatcher(formatOptions, formats).
         auto formatter = ::Locale::DateTimeFormat::create_for_pattern_options(
-            date_time_format->data_locale(),
+            date_time_format->locale(),
             time_zone,
             format_options);
         date_time_format->set_formatter(move(formatter));

+ 38 - 0
Userland/Libraries/LibJS/Tests/builtins/Intl/DateTimeFormat/DateTimeFormat.prototype.format.js

@@ -537,3 +537,41 @@ describe("timeZoneName", () => {
         });
     });
 });
+
+describe("non-Gregorian calendars", () => {
+    test("Hebrew", () => {
+        const en = new Intl.DateTimeFormat("en-u-ca-hebrew", {
+            dateStyle: "long",
+            timeStyle: "long",
+            timeZone: "UTC",
+        });
+        expect(en.format(d0)).toBe("3 Tevet 5782 at 5:40:50\u202fPM UTC");
+        expect(en.format(d1)).toBe("17 Shevat 5749 at 7:08:09\u202fAM UTC");
+
+        const ar = new Intl.DateTimeFormat("ar-u-ca-hebrew", {
+            dateStyle: "long",
+            timeStyle: "long",
+            timeZone: "UTC",
+        });
+        expect(ar.format(d0)).toBe("٣ طيفت ٥٧٨٢ ص في ٥:٤٠:٥٠ م UTC");
+        expect(ar.format(d1)).toBe("١٧ شباط ٥٧٤٩ ص في ٧:٠٨:٠٩ ص UTC");
+    });
+
+    test("Chinese", () => {
+        const en = new Intl.DateTimeFormat("en-u-ca-chinese", {
+            dateStyle: "long",
+            timeStyle: "long",
+            timeZone: "UTC",
+        });
+        expect(en.format(d0)).toBe("Eleventh Month 4, 2021(xin-chou) at 5:40:50\u202fPM UTC");
+        expect(en.format(d1)).toBe("Twelfth Month 16, 1988(wu-chen) at 7:08:09\u202fAM UTC");
+
+        const zh = new Intl.DateTimeFormat("zh-u-ca-chinese", {
+            dateStyle: "long",
+            timeStyle: "long",
+            timeZone: "UTC",
+        });
+        expect(zh.format(d0)).toBe("2021辛丑年十一月初四 UTC 17:40:50");
+        expect(zh.format(d1)).toBe("1988戊辰年腊月十六 UTC 07:08:09");
+    });
+});