1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 06:37:43 +00:00

LibJS: Port create_temporal_time_zone() to String

This commit is contained in:
Linus Groh 2023-01-26 16:06:23 +00:00
parent ae98eddc9f
commit 5a2dfc52f8
7 changed files with 17 additions and 16 deletions

View file

@ -661,7 +661,7 @@ ThrowCompletionOr<Value> to_relative_temporal_object(VM& vm, Object const& optio
}
// ii. Let timeZone be ! CreateTemporalTimeZone(timeZoneName).
time_zone = MUST(create_temporal_time_zone(vm, time_zone_name->to_deprecated_string()));
time_zone = MUST_OR_THROW_OOM(create_temporal_time_zone(vm, *time_zone_name));
// iii. If result.[[TimeZone]].[[Z]] is true, then
if (result.time_zone.z) {

View file

@ -250,7 +250,7 @@ ThrowCompletionOr<String> temporal_instant_to_string(VM& vm, Instant& instant, V
// 4. If outputTimeZone is undefined, then
if (output_time_zone.is_undefined()) {
// a. Set outputTimeZone to ! CreateTemporalTimeZone("UTC").
output_time_zone = MUST(create_temporal_time_zone(vm, "UTC"sv));
output_time_zone = MUST_OR_THROW_OOM(create_temporal_time_zone(vm, "UTC"sv));
}
// 5. Let isoCalendar be ! GetISO8601Calendar().

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2021-2023, Linus Groh <linusg@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -155,6 +155,7 @@ TimeZone* system_time_zone(VM& vm)
auto identifier = default_time_zone();
// 2. Return ! CreateTemporalTimeZone(identifier).
// FIXME: Propagate possible OOM error
return MUST(create_temporal_time_zone(vm, identifier));
}

View file

@ -57,7 +57,7 @@ ThrowCompletionOr<String> canonicalize_time_zone_name(VM& vm, StringView time_zo
}
// 11.6.1 CreateTemporalTimeZone ( identifier [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaltimezone
ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM& vm, DeprecatedString const& identifier, FunctionObject const* new_target)
ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM& vm, StringView identifier, FunctionObject const* new_target)
{
auto& realm = *vm.current_realm();
@ -74,7 +74,7 @@ ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM& vm, DeprecatedString
auto offset_nanoseconds_result = parse_time_zone_offset_string(identifier);
// b. Set object.[[Identifier]] to ! FormatTimeZoneOffsetString(offsetNanosecondsResult).
object->set_identifier(format_time_zone_offset_string(offset_nanoseconds_result));
object->set_identifier(TRY_OR_THROW_OOM(vm, String::from_utf8(format_time_zone_offset_string(offset_nanoseconds_result))));
// c. Set object.[[OffsetNanoseconds]] to offsetNanosecondsResult.
object->set_offset_nanoseconds(offset_nanoseconds_result);
@ -82,10 +82,10 @@ ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM& vm, DeprecatedString
// 4. Else,
else {
// a. Assert: ! CanonicalizeTimeZoneName(identifier) is identifier.
VERIFY(MUST_OR_THROW_OOM(canonicalize_time_zone_name(vm, identifier)) == identifier.view());
VERIFY(MUST_OR_THROW_OOM(canonicalize_time_zone_name(vm, identifier)) == identifier);
// b. Set object.[[Identifier]] to identifier.
object->set_identifier(identifier);
object->set_identifier(TRY_OR_THROW_OOM(vm, String::from_utf8(identifier)));
// c. Set object.[[OffsetNanoseconds]] to undefined.
// NOTE: No-op.
@ -353,15 +353,15 @@ ThrowCompletionOr<Object*> to_temporal_time_zone(VM& vm, Value temporal_time_zon
}
// c. Return ! CreateTemporalTimeZone(name).
return MUST(create_temporal_time_zone(vm, name.to_deprecated_string()));
return MUST_OR_THROW_OOM(create_temporal_time_zone(vm, name));
}
// 5. If parseResult.[[Z]] is true, return ! CreateTemporalTimeZone("UTC").
if (parse_result.z)
return MUST(create_temporal_time_zone(vm, "UTC"sv));
return MUST_OR_THROW_OOM(create_temporal_time_zone(vm, "UTC"sv));
// 6. Return ! CreateTemporalTimeZone(parseResult.[[OffsetString]]).
return MUST(create_temporal_time_zone(vm, parse_result.offset_string->to_deprecated_string()));
return MUST_OR_THROW_OOM(create_temporal_time_zone(vm, *parse_result.offset_string));
}
// 11.6.8 GetOffsetNanosecondsFor ( timeZone, instant ), https://tc39.es/proposal-temporal/#sec-temporal-getoffsetnanosecondsfor

View file

@ -22,23 +22,23 @@ public:
virtual ~TimeZone() override = default;
[[nodiscard]] DeprecatedString const& identifier() const { return m_identifier; }
[[nodiscard]] String const& identifier() const { return m_identifier; }
[[nodiscard]] Optional<OffsetType> const& offset_nanoseconds() const { return m_offset_nanoseconds; }
void set_identifier(DeprecatedString identifier) { m_identifier = move(identifier); };
void set_identifier(String identifier) { m_identifier = move(identifier); };
void set_offset_nanoseconds(OffsetType offset_nanoseconds) { m_offset_nanoseconds = offset_nanoseconds; };
private:
explicit TimeZone(Object& prototype);
// 11.5 Properties of Temporal.TimeZone Instances, https://tc39.es/proposal-temporal/#sec-properties-of-temporal-timezone-instances
DeprecatedString m_identifier; // [[Identifier]]
String m_identifier; // [[Identifier]]
Optional<OffsetType> m_offset_nanoseconds; // [[OffsetNanoseconds]]
};
bool is_available_time_zone_name(StringView time_zone);
ThrowCompletionOr<String> canonicalize_time_zone_name(VM&, StringView time_zone);
ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM&, DeprecatedString const& identifier, FunctionObject const* new_target = nullptr);
ThrowCompletionOr<TimeZone*> create_temporal_time_zone(VM&, StringView identifier, FunctionObject const* new_target = nullptr);
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);

View file

@ -63,7 +63,7 @@ ThrowCompletionOr<NonnullGCPtr<Object>> TimeZoneConstructor::construct(FunctionO
}
// 4. Return ? CreateTemporalTimeZone(identifier, NewTarget).
return *TRY(create_temporal_time_zone(vm, identifier.to_deprecated_string(), &new_target));
return *TRY(create_temporal_time_zone(vm, identifier, &new_target));
}
// 11.3.2 Temporal.TimeZone.from ( item ), https://tc39.es/proposal-temporal/#sec-temporal.timezone.from

View file

@ -221,7 +221,7 @@ ThrowCompletionOr<ZonedDateTime*> to_temporal_zoned_date_time(VM& vm, Value item
}
// j. Let timeZone be ! CreateTemporalTimeZone(timeZoneName).
time_zone = MUST(create_temporal_time_zone(vm, time_zone_name->to_deprecated_string()));
time_zone = MUST_OR_THROW_OOM(create_temporal_time_zone(vm, *time_zone_name));
// k. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
auto temporal_calendar_like = result.calendar.has_value()