Browse Source

LibJS: Port temporal_instant_to_string() to String

Linus Groh 2 years ago
parent
commit
d2c10f6bea

+ 6 - 6
Userland/Libraries/LibJS/Runtime/Temporal/Instant.cpp

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
@@ -239,7 +239,7 @@ BigInt* round_temporal_instant(VM& vm, BigInt const& nanoseconds, u64 increment,
 }
 
 // 8.5.9 TemporalInstantToString ( instant, timeZone, precision ), https://tc39.es/proposal-temporal/#sec-temporal-temporalinstanttostring
-ThrowCompletionOr<DeprecatedString> temporal_instant_to_string(VM& vm, Instant& instant, Value time_zone, Variant<StringView, u8> const& precision)
+ThrowCompletionOr<String> temporal_instant_to_string(VM& vm, Instant& instant, Value time_zone, Variant<StringView, u8> const& precision)
 {
     // 1. Assert: Type(instant) is Object.
     // 2. Assert: instant has an [[InitializedTemporalInstant]] internal slot.
@@ -262,12 +262,12 @@ ThrowCompletionOr<DeprecatedString> temporal_instant_to_string(VM& vm, Instant&
     // 7. Let dateTimeString be ! TemporalDateTimeToString(dateTime.[[ISOYear]], dateTime.[[ISOMonth]], dateTime.[[ISODay]], dateTime.[[ISOHour]], dateTime.[[ISOMinute]], dateTime.[[ISOSecond]], dateTime.[[ISOMillisecond]], dateTime.[[ISOMicrosecond]], dateTime.[[ISONanosecond]], undefined, precision, "never").
     auto date_time_string = MUST(temporal_date_time_to_string(vm, date_time->iso_year(), date_time->iso_month(), date_time->iso_day(), date_time->iso_hour(), date_time->iso_minute(), date_time->iso_second(), date_time->iso_millisecond(), date_time->iso_microsecond(), date_time->iso_nanosecond(), nullptr, precision, "never"sv));
 
-    DeprecatedString time_zone_string;
+    String time_zone_string;
 
     // 8. If timeZone is undefined, then
     if (time_zone.is_undefined()) {
         // a. Let timeZoneString be "Z".
-        time_zone_string = "Z"sv;
+        time_zone_string = String::from_utf8_short_string("Z"sv);
     }
     // 9. Else,
     else {
@@ -275,11 +275,11 @@ ThrowCompletionOr<DeprecatedString> temporal_instant_to_string(VM& vm, Instant&
         auto offset_ns = TRY(get_offset_nanoseconds_for(vm, time_zone, instant));
 
         // b. Let timeZoneString be ! FormatISOTimeZoneOffsetString(offsetNs).
-        time_zone_string = format_iso_time_zone_offset_string(offset_ns);
+        time_zone_string = TRY_OR_THROW_OOM(vm, String::from_deprecated_string(format_iso_time_zone_offset_string(offset_ns)));
     }
 
     // 10. Return the string-concatenation of dateTimeString and timeZoneString.
-    return DeprecatedString::formatted("{}{}", date_time_string, time_zone_string);
+    return TRY_OR_THROW_OOM(vm, String::formatted("{}{}", date_time_string, time_zone_string));
 }
 
 // 8.5.10 DifferenceTemporalInstant ( operation, instant, other, options ), https://tc39.es/proposal-temporal/#sec-temporal-differencetemporalinstant

+ 2 - 2
Userland/Libraries/LibJS/Runtime/Temporal/Instant.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
+ * Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
  * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  *
  * SPDX-License-Identifier: BSD-2-Clause
@@ -50,7 +50,7 @@ i32 compare_epoch_nanoseconds(BigInt const&, BigInt const&);
 ThrowCompletionOr<BigInt*> add_instant(VM&, BigInt const& epoch_nanoseconds, double hours, double minutes, double seconds, double milliseconds, double microseconds, double nanoseconds);
 BigInt* difference_instant(VM&, BigInt const& nanoseconds1, BigInt const& nanoseconds2, u64 rounding_increment, StringView smallest_unit, StringView rounding_mode);
 BigInt* round_temporal_instant(VM&, BigInt const& nanoseconds, u64 increment, StringView unit, StringView rounding_mode);
-ThrowCompletionOr<DeprecatedString> temporal_instant_to_string(VM&, Instant&, Value time_zone, Variant<StringView, u8> const& precision);
+ThrowCompletionOr<String> temporal_instant_to_string(VM&, Instant&, Value time_zone, Variant<StringView, u8> const& precision);
 ThrowCompletionOr<Duration*> difference_temporal_instant(VM&, DifferenceOperation, Instant const&, Value other, Value options);
 ThrowCompletionOr<Instant*> add_duration_to_or_subtract_duration_from_instant(VM&, ArithmeticOperation, Instant const&, Value temporal_duration_like);