From e93ce1ff691678cb07f50170adcc975398cf86ff Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 7 Nov 2021 20:06:28 +0000 Subject: LibJS: Fix nanoseconds formatting in format_time_zone_offset_string() Two issues: - The format string said "{:9}", which left-pads with spaces and not zeros as required - Even when correcting that, we were not accounting for step 11 b: "Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO)." We can safely use trim() for that as the formatted string is known to not contain only zeros (which would leave the left-most in place). Also adds tests for "UTC" and various numeric offsets. --- Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp | 2 +- .../Temporal/TimeZone/TimeZone.prototype.toString.js | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'Userland') diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp index 7a4479b927..fb99452481 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp @@ -336,7 +336,7 @@ String format_time_zone_offset_string(double offset_nanoseconds) // a. Let fraction be nanoseconds, formatted as a nine-digit decimal number, padded to the left with zeroes if necessary. // b. Set fraction to the longest possible substring of fraction starting at position 0 and not ending with the code unit 0x0030 (DIGIT ZERO). // c. Let post be the string-concatenation of the code unit 0x003A (COLON), s, the code unit 0x002E (FULL STOP), and fraction. - builder.appendff(":{:02}.{:9}", seconds, nanoseconds); + builder.appendff(":{:02}.{}", seconds, String::formatted("{:09}", nanoseconds).trim("0"sv, TrimMode::Right)); } // 12. Else if seconds ≠ 0, then else if (seconds != 0) { diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toString.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toString.js index bfefb3ac93..2814b13c7e 100644 --- a/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toString.js +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toString.js @@ -4,8 +4,20 @@ describe("correct behavior", () => { }); test("basic functionality", () => { - const timeZone = new Temporal.TimeZone("UTC"); - expect(timeZone.toString()).toBe("UTC"); + const values = [ + ["utc", "UTC"], + ["Utc", "UTC"], + ["UTC", "UTC"], + ["+00:00", "+00:00"], + ["+00:00:00", "+00:00"], + ["+00:00:00.000", "+00:00"], + ["+12:34:56.789", "+12:34:56.789"], + ["+12:34:56.789000", "+12:34:56.789"], + ["-01:00", "-01:00"], + ]; + for (const [arg, expected] of values) { + expect(new Temporal.TimeZone(arg).toString()).toBe(expected); + } }); }); -- cgit v1.2.3