1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 07:28:11 +00:00

LibJS: Respect the user-provided time zone in Intl.DateTimeFormat

Also update some DateTimeFormat tests to explicitly set the time zone
(usually to UTC). This was already done for most tests, but some were
missed.
This commit is contained in:
Timothy Flynn 2022-01-12 08:22:47 -05:00 committed by Linus Groh
parent 8987deb984
commit d64ea13565
4 changed files with 16 additions and 20 deletions

View file

@ -1492,15 +1492,14 @@ ThrowCompletionOr<Array*> format_date_time_range_to_parts(GlobalObject& global_o
}
// 11.1.14 ToLocalTime ( t, calendar, timeZone ), https://tc39.es/ecma402/#sec-tolocaltime
ThrowCompletionOr<LocalTime> to_local_time(GlobalObject& global_object, double time, StringView calendar, [[maybe_unused]] StringView time_zone)
ThrowCompletionOr<LocalTime> to_local_time(GlobalObject& global_object, double time, StringView calendar, StringView time_zone)
{
// 1. Assert: Type(t) is Number.
// 2. If calendar is "gregory", then
if (calendar == "gregory"sv) {
// a. Let timeZoneOffset be the value calculated according to LocalTZA(t, true) where the local time zone is replaced with timezone timeZone.
// FIXME: Implement LocalTZA when timezones other than UTC are supported.
double time_zone_offset = 0;
double time_zone_offset = local_tza(time, true, time_zone);
// b. Let tz be the time value t + timeZoneOffset.
double zoned_time = time + time_zone_offset;

View file

@ -389,13 +389,10 @@ describe("timeZoneName", () => {
{ timeZone: "UTC", timeZoneName: "longOffset", en0: "12/7/2021, 5:40 PM GMT", en1: "1/23/1989, 7:08 AM GMT", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م غرينتش", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص غرينتش" },
{ timeZone: "UTC", timeZoneName: "shortGeneric", en0: "12/7/2021, 5:40 PM GMT", en1: "1/23/1989, 7:08 AM GMT", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م غرينتش", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص غرينتش" },
{ timeZone: "UTC", timeZoneName: "longGeneric", en0: "12/7/2021, 5:40 PM GMT", en1: "1/23/1989, 7:08 AM GMT", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م غرينتش", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص غرينتش" },
// FIXME: The time stamps on the below cases are incorrect as they do not adjust the time based on the GMT offset.
// Update these once the LocalTZA AO is implemented and ToLocalTime uses it.
{ timeZone: "America/New_York", timeZoneName: "shortOffset", en0: "12/7/2021, 5:40 PM GMT-5", en1: "1/23/1989, 7:08 AM GMT-5", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م غرينتش-٥", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص غرينتش-٥" },
{ timeZone: "America/New_York", timeZoneName: "longOffset", en0: "12/7/2021, 5:40 PM GMT-05:00", en1: "1/23/1989, 7:08 AM GMT-05:00", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م غرينتش-٠٥:٠٠", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص غرينتش-٠٥:٠٠" },
{ timeZone: "America/New_York", timeZoneName: "shortGeneric", en0: "12/7/2021, 5:40 PM ET", en1: "1/23/1989, 7:08 AM ET", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م غرينتش-٥", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص غرينتش-٥" },
{ timeZone: "America/New_York", timeZoneName: "longGeneric", en0: "12/7/2021, 5:40 PM Eastern Time", en1: "1/23/1989, 7:08 AM Eastern Time", ar0: "٧/١٢‏/٢٠٢١, ٥:٤٠ م التوقيت الشرقي لأمريكا الشمالية", ar1: "٢٣‏/١/١٩٨٩, ٧:٠٨ ص التوقيت الشرقي لأمريكا الشمالية" },
{ timeZone: "America/New_York", timeZoneName: "shortOffset", en0: "12/7/2021, 12:40 PM GMT-5", en1: "1/23/1989, 2:08 AM GMT-5", ar0: "٧/١٢‏/٢٠٢١, ١٢:٤٠ م غرينتش-٥", ar1: "٢٣‏/١/١٩٨٩, ٢:٠٨ ص غرينتش-٥" },
{ timeZone: "America/New_York", timeZoneName: "longOffset", en0: "12/7/2021, 12:40 PM GMT-05:00", en1: "1/23/1989, 2:08 AM GMT-05:00", ar0: "٧/١٢‏/٢٠٢١, ١٢:٤٠ م غرينتش-٠٥:٠٠", ar1: "٢٣‏/١/١٩٨٩, ٢:٠٨ ص غرينتش-٠٥:٠٠" },
{ timeZone: "America/New_York", timeZoneName: "shortGeneric", en0: "12/7/2021, 12:40 PM ET", en1: "1/23/1989, 2:08 AM ET", ar0: "٧/١٢‏/٢٠٢١, ١٢:٤٠ م غرينتش-٥", ar1: "٢٣‏/١/١٩٨٩, ٢:٠٨ ص غرينتش-٥" },
{ timeZone: "America/New_York", timeZoneName: "longGeneric", en0: "12/7/2021, 12:40 PM Eastern Time", en1: "1/23/1989, 2:08 AM Eastern Time", ar0: "٧/١٢‏/٢٠٢١, ١٢:٤٠ م التوقيت الشرقي لأمريكا الشمالية", ar1: "٢٣‏/١/١٩٨٩, ٢:٠٨ ص التوقيت الشرقي لأمريكا الشمالية" },
];
test("all", () => {

View file

@ -347,7 +347,7 @@ describe("timeStyle", () => {
// FIXME: These results should include the date, even though it isn't requested, because the start/end dates
// are more than just hours apart. See the FIXME in PartitionDateTimeRangePattern.
test("full", () => {
const en = new Intl.DateTimeFormat("en", { timeStyle: "full" });
const en = new Intl.DateTimeFormat("en", { timeStyle: "full", timeZone: "UTC" });
expect(en.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
@ -370,7 +370,7 @@ describe("timeStyle", () => {
{ type: "timeZoneName", value: "Coordinated Universal Time", source: "endRange" },
]);
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "full" });
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "full", timeZone: "UTC" });
expect(ja.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: "時", source: "startRange" },
@ -391,7 +391,7 @@ describe("timeStyle", () => {
});
test("long", () => {
const en = new Intl.DateTimeFormat("en", { timeStyle: "long" });
const en = new Intl.DateTimeFormat("en", { timeStyle: "long", timeZone: "UTC" });
expect(en.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
@ -414,7 +414,7 @@ describe("timeStyle", () => {
{ type: "timeZoneName", value: "UTC", source: "endRange" },
]);
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "long" });
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "long", timeZone: "UTC" });
expect(ja.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
@ -435,7 +435,7 @@ describe("timeStyle", () => {
});
test("medium", () => {
const en = new Intl.DateTimeFormat("en", { timeStyle: "medium" });
const en = new Intl.DateTimeFormat("en", { timeStyle: "medium", timeZone: "UTC" });
expect(en.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
@ -454,7 +454,7 @@ describe("timeStyle", () => {
{ type: "dayPeriod", value: "PM", source: "endRange" },
]);
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "medium" });
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "medium", timeZone: "UTC" });
expect(ja.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
@ -471,7 +471,7 @@ describe("timeStyle", () => {
});
test("short", () => {
const en = new Intl.DateTimeFormat("en", { timeStyle: "short" });
const en = new Intl.DateTimeFormat("en", { timeStyle: "short", timeZone: "UTC" });
expect(en.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },
@ -486,7 +486,7 @@ describe("timeStyle", () => {
{ type: "dayPeriod", value: "PM", source: "endRange" },
]);
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "short" });
const ja = new Intl.DateTimeFormat("ja", { timeStyle: "short", timeZone: "UTC" });
expect(ja.formatRangeToParts(d0, d1)).toEqual([
{ type: "hour", value: "7", source: "startRange" },
{ type: "literal", value: ":", source: "startRange" },

View file

@ -86,8 +86,8 @@ describe("correct behavior", () => {
});
test("style", () => {
const en = new Intl.DateTimeFormat("en");
expect(en.resolvedOptions().timeZone).toBe("UTC");
const en = new Intl.DateTimeFormat("en", { timeZone: "EST" });
expect(en.resolvedOptions().timeZone).toBe("EST");
const el = new Intl.DateTimeFormat("el", { timeZone: "UTC" });
expect(el.resolvedOptions().timeZone).toBe("UTC");