summaryrefslogtreecommitdiff
path: root/Userland
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2021-11-07 20:06:28 +0000
committerLinus Groh <mail@linusgroh.de>2021-11-07 20:06:28 +0000
commite93ce1ff691678cb07f50170adcc975398cf86ff (patch)
tree79d6ddfe25e8f05210aa27a902d5dfddfdf590b1 /Userland
parent68d80d239b09db3f4e6d1a2d90e3e9405078b930 (diff)
downloadserenity-e93ce1ff691678cb07f50170adcc975398cf86ff.zip
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.
Diffstat (limited to 'Userland')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp2
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/TimeZone/TimeZone.prototype.toString.js16
2 files changed, 15 insertions, 3 deletions
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);
+ }
});
});