mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-06 09:36:08 +00:00
LibJS: Implement the ECMA-402 ZonedDateTime.prototype.toLocaleString.js
This commit is contained in:
parent
c96f6c396f
commit
0468463e2e
Notes:
github-actions[bot]
2024-11-29 08:53:04 +00:00
Author: https://github.com/trflynn89
Commit: 0468463e2e
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2615
2 changed files with 83 additions and 5 deletions
|
@ -7,6 +7,8 @@
|
|||
*/
|
||||
|
||||
#include <LibJS/Runtime/Date.h>
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormat.h>
|
||||
#include <LibJS/Runtime/Intl/DateTimeFormatConstructor.h>
|
||||
#include <LibJS/Runtime/Temporal/Calendar.h>
|
||||
#include <LibJS/Runtime/Temporal/Duration.h>
|
||||
#include <LibJS/Runtime/Temporal/Instant.h>
|
||||
|
@ -15,6 +17,7 @@
|
|||
#include <LibJS/Runtime/Temporal/PlainTime.h>
|
||||
#include <LibJS/Runtime/Temporal/TimeZone.h>
|
||||
#include <LibJS/Runtime/Temporal/ZonedDateTimePrototype.h>
|
||||
#include <Libraries/LibJS/Runtime/Intl/AbstractOperations.h>
|
||||
|
||||
namespace JS::Temporal {
|
||||
|
||||
|
@ -785,15 +788,56 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_string)
|
|||
}
|
||||
|
||||
// 6.3.42 Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tolocalestring
|
||||
// NOTE: This is the minimum toLocaleString implementation for engines without ECMA-402.
|
||||
// 15.12.8.1 Temporal.ZonedDateTime.prototype.toLocaleString ( [ locales [ , options ] ] ), https://tc39.es/proposal-temporal/#sup-properties-of-the-temporal-zoneddatetime-prototype-object
|
||||
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::to_locale_string)
|
||||
{
|
||||
auto& realm = *vm.current_realm();
|
||||
|
||||
auto locales = vm.argument(0);
|
||||
auto options = vm.argument(1);
|
||||
|
||||
// 1. Let zonedDateTime be the this value.
|
||||
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
|
||||
auto zoned_date_time = TRY(typed_this_object(vm));
|
||||
|
||||
// 3. Return TemporalZonedDateTimeToString(zonedDateTime, AUTO, AUTO, AUTO, AUTO).
|
||||
return PrimitiveString::create(vm, temporal_zoned_date_time_to_string(zoned_date_time, Auto {}, ShowCalendar::Auto, ShowTimeZoneName::Auto, ShowOffset::Auto));
|
||||
// 3. Let timeZone be zonedDateTime.[[TimeZone]].
|
||||
auto time_zone = zoned_date_time->time_zone();
|
||||
|
||||
// 4. Let timeZoneParseResult be ? ParseTimeZoneIdentifier(timeZone).
|
||||
auto time_zone_parse_result = TRY(parse_time_zone_identifier(vm, time_zone));
|
||||
|
||||
// 5. If timeZoneParseResult.[[OffsetMinutes]] is empty, then
|
||||
if (!time_zone_parse_result.offset_minutes.has_value()) {
|
||||
// a. Let timeZoneIdentifierRecord be GetAvailableNamedTimeZoneIdentifier(timeZone).
|
||||
auto time_zone_identifier_record = Intl::get_available_named_time_zone_identifier(time_zone);
|
||||
|
||||
// b. If timeZoneIdentifierRecord is empty, throw a RangeError exception.
|
||||
if (!time_zone_identifier_record.has_value())
|
||||
return vm.throw_completion<RangeError>(ErrorType::TemporalInvalidTimeZoneName, time_zone);
|
||||
|
||||
// c. Set timeZone to timeZoneIdentifierRecord.[[Identifier]].
|
||||
time_zone = time_zone_identifier_record->identifier;
|
||||
}
|
||||
// 6. Else,
|
||||
else {
|
||||
// a. Set timeZone to FormatOffsetTimeZoneIdentifier(timeZoneParseResult.[[OffsetMinutes]]).
|
||||
time_zone = format_offset_time_zone_identifier(*time_zone_parse_result.offset_minutes);
|
||||
}
|
||||
|
||||
// 7. Let dateTimeFormat be ? CreateDateTimeFormat(%Intl.DateTimeFormat%, locales, options, ANY, ALL, timeZone).
|
||||
auto date_time_format = TRY(Intl::create_date_time_format(vm, realm.intrinsics().intl_date_time_format_constructor(), locales, options, Intl::OptionRequired::Any, Intl::OptionDefaults::All, time_zone));
|
||||
|
||||
// 8. If zonedDateTime.[[Calendar]] is not "iso8601" and CalendarEquals(zonedDateTime.[[Calendar]], dateTimeFormat.[[Calendar]]) is false, then
|
||||
if (zoned_date_time->calendar() != "iso8601"sv && !calendar_equals(zoned_date_time->calendar(), date_time_format->calendar())) {
|
||||
// a. Throw a RangeError exception.
|
||||
return vm.throw_completion<RangeError>(ErrorType::IntlTemporalInvalidCalendar, "Temporal.ZonedDateTime"sv, zoned_date_time->calendar(), date_time_format->calendar());
|
||||
}
|
||||
|
||||
// 9. Let instant be ! CreateTemporalInstant(zonedDateTime.[[EpochNanoseconds]]).
|
||||
auto instant = MUST(create_temporal_instant(vm, zoned_date_time->epoch_nanoseconds()));
|
||||
|
||||
// 10. Return ? FormatDateTime(dateTimeFormat, instant).
|
||||
return PrimitiveString::create(vm, TRY(Intl::format_date_time(vm, date_time_format, instant)));
|
||||
}
|
||||
|
||||
// 6.3.43 Temporal.ZonedDateTime.prototype.toJSON ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.tojson
|
||||
|
|
|
@ -4,9 +4,20 @@ describe("correct behavior", () => {
|
|||
});
|
||||
|
||||
test("basic functionality", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(2021, 11, 3, 1, 33, 5, 100, 200, 300);
|
||||
const plainDateTime = new Temporal.PlainDateTime(
|
||||
2021,
|
||||
11,
|
||||
3,
|
||||
1,
|
||||
33,
|
||||
5,
|
||||
100,
|
||||
200,
|
||||
300,
|
||||
"gregory"
|
||||
);
|
||||
const zonedDateTime = plainDateTime.toZonedDateTime("UTC");
|
||||
expect(zonedDateTime.toLocaleString()).toBe("2021-11-03T01:33:05.1002003+00:00[UTC]");
|
||||
expect(zonedDateTime.toLocaleString()).toBe("11/3/2021, 1:33:5 AM UTC");
|
||||
});
|
||||
});
|
||||
|
||||
|
@ -16,4 +27,27 @@ describe("errors", () => {
|
|||
Temporal.ZonedDateTime.prototype.toLocaleString.call("foo");
|
||||
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
|
||||
});
|
||||
|
||||
test("Temporal object must have same calendar", () => {
|
||||
const plainDateTime = new Temporal.PlainDateTime(
|
||||
2021,
|
||||
11,
|
||||
3,
|
||||
1,
|
||||
33,
|
||||
5,
|
||||
100,
|
||||
200,
|
||||
300,
|
||||
"gregory"
|
||||
);
|
||||
const zonedDateTime = plainDateTime.toZonedDateTime("UTC");
|
||||
|
||||
expect(() => {
|
||||
zonedDateTime.toLocaleString("en", { calendar: "iso8601" });
|
||||
}).toThrowWithMessage(
|
||||
RangeError,
|
||||
"Cannot format Temporal.ZonedDateTime with calendar 'gregory' in locale with calendar 'iso8601'"
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue