summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2023-01-26 16:08:10 +0000
committerLinus Groh <mail@linusgroh.de>2023-01-26 20:20:54 +0000
commitc2656f4ee7366bc99f8094b223d6300217f7bd47 (patch)
tree8377a68fad73c6e3d94386a63386165028039b49 /Userland/Libraries/LibJS/Runtime
parent5a2dfc52f8260ddb27e2addef42f2b778e77ca68 (diff)
downloadserenity-c2656f4ee7366bc99f8094b223d6300217f7bd47.zip
LibJS: Port format_time_zone_offset_string() to String
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp11
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h2
2 files changed, 7 insertions, 6 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
index 08bf1ceda7..3ca6fe794b 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.cpp
@@ -74,7 +74,7 @@ ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM& vm, StringView identi
auto offset_nanoseconds_result = parse_time_zone_offset_string(identifier);
// b. Set object.[[Identifier]] to ! FormatTimeZoneOffsetString(offsetNanosecondsResult).
- object->set_identifier(TRY_OR_THROW_OOM(vm, String::from_utf8(format_time_zone_offset_string(offset_nanoseconds_result))));
+ object->set_identifier(MUST_OR_THROW_OOM(format_time_zone_offset_string(vm, offset_nanoseconds_result)));
// c. Set object.[[OffsetNanoseconds]] to offsetNanosecondsResult.
object->set_offset_nanoseconds(offset_nanoseconds_result);
@@ -209,7 +209,7 @@ bool is_valid_time_zone_numeric_utc_offset_syntax(DeprecatedString const& offset
}
// 11.6.5 FormatTimeZoneOffsetString ( offsetNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-formattimezoneoffsetstring
-DeprecatedString format_time_zone_offset_string(double offset_nanoseconds)
+ThrowCompletionOr<String> format_time_zone_offset_string(VM& vm, double offset_nanoseconds)
{
auto offset = static_cast<i64>(offset_nanoseconds);
@@ -248,7 +248,8 @@ DeprecatedString format_time_zone_offset_string(double offset_nanoseconds)
// a. Let fraction be ToZeroPaddedDecimalString(nanoseconds, 9).
// 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}.{}", seconds, DeprecatedString::formatted("{:09}", nanoseconds).trim("0"sv, TrimMode::Right));
+ // FIXME: Add String::trim()
+ builder.appendff(":{:02}.{}", seconds, TRY_OR_THROW_OOM(vm, String::from_utf8(TRY_OR_THROW_OOM(vm, String::formatted("{:09}", nanoseconds)).bytes_as_string_view().trim("0"sv, TrimMode::Right))));
}
// 12. Else if seconds ≠ 0, then
else if (seconds != 0) {
@@ -259,7 +260,7 @@ DeprecatedString format_time_zone_offset_string(double offset_nanoseconds)
// a. Let post be the empty String.
// 14. Return the string-concatenation of sign, h, the code unit 0x003A (COLON), m, and post.
- return builder.to_deprecated_string();
+ return TRY_OR_THROW_OOM(vm, builder.to_string());
}
// 11.6.6 FormatISOTimeZoneOffsetString ( offsetNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-formatisotimezoneoffsetstring
@@ -399,7 +400,7 @@ ThrowCompletionOr<DeprecatedString> builtin_time_zone_get_offset_string_for(VM&
auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(vm, time_zone, instant));
// 2. Return ! FormatTimeZoneOffsetString(offsetNanoseconds).
- return format_time_zone_offset_string(offset_nanoseconds);
+ return MUST_OR_THROW_OOM(format_time_zone_offset_string(vm, offset_nanoseconds)).to_deprecated_string();
}
// 11.6.10 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
index 1c59695816..a091d97129 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/TimeZone.h
@@ -42,7 +42,7 @@ ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM&, StringView identifie
ISODateTime get_iso_parts_from_epoch(VM&, Crypto::SignedBigInteger const& epoch_nanoseconds);
BigInt* get_named_time_zone_next_transition(VM&, StringView time_zone_identifier, BigInt const& epoch_nanoseconds);
BigInt* get_named_time_zone_previous_transition(VM&, StringView time_zone_identifier, BigInt const& epoch_nanoseconds);
-DeprecatedString format_time_zone_offset_string(double offset_nanoseconds);
+ThrowCompletionOr<String> format_time_zone_offset_string(VM&, double offset_nanoseconds);
DeprecatedString format_iso_time_zone_offset_string(double offset_nanoseconds);
ThrowCompletionOr<Object*> to_temporal_time_zone(VM&, Value temporal_time_zone_like);
ThrowCompletionOr<double> get_offset_nanoseconds_for(VM&, Value time_zone, Instant&);