LibJS: Implement Temporal.PlainDateTime.prototype.toPlainDate/Time

This commit is contained in:
Timothy Flynn 2024-11-23 19:14:24 -05:00 committed by Andreas Kling
parent 990daaf63a
commit 649328fed2
Notes: github-actions[bot] 2024-11-24 10:45:02 +00:00
4 changed files with 54 additions and 0 deletions

View file

@ -9,6 +9,7 @@
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/PlainDate.h>
#include <LibJS/Runtime/Temporal/PlainDateTimePrototype.h>
#include <LibJS/Runtime/Temporal/PlainTime.h>
@ -67,6 +68,8 @@ void PlainDateTimePrototype::initialize(Realm& realm)
define_native_function(realm, vm.names.toString, to_string, 0, attr);
define_native_function(realm, vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(realm, vm.names.toJSON, to_json, 0, attr);
define_native_function(realm, vm.names.toPlainDate, to_plain_date, 0, attr);
define_native_function(realm, vm.names.toPlainTime, to_plain_time, 0, attr);
}
// 5.3.3 get Temporal.PlainDateTime.prototype.calendarId, https://tc39.es/proposal-temporal/#sec-get-temporal.plaindatetime.prototype.calendarid
@ -547,4 +550,26 @@ JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_json)
return PrimitiveString::create(vm, iso_date_time_to_string(date_time->iso_date_time(), date_time->calendar(), Auto {}, ShowCalendar::Auto));
}
// 5.3.39 Temporal.PlainDateTime.prototype.toPlainDate ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplaindate
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_date)
{
// 1. Let dateTime be the this value.
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
auto date_time = TRY(typed_this_object(vm));
// 3. Return ! CreateTemporalDate(dateTime.[[ISODateTime]].[[ISODate]], dateTime.[[Calendar]]).
return MUST(create_temporal_date(vm, date_time->iso_date_time().iso_date, date_time->calendar()));
}
// 5.3.40 Temporal.PlainDateTime.prototype.toPlainTime ( ), https://tc39.es/proposal-temporal/#sec-temporal.plaindatetime.prototype.toplaintime
JS_DEFINE_NATIVE_FUNCTION(PlainDateTimePrototype::to_plain_time)
{
// 1. Let dateTime be the this value.
// 2. Perform ? RequireInternalSlot(dateTime, [[InitializedTemporalDateTime]]).
auto date_time = TRY(typed_this_object(vm));
// 3. Return ! CreateTemporalTime(dateTime.[[ISODateTime]].[[Time]]).
return MUST(create_temporal_time(vm, date_time->iso_date_time().time));
}
}

View file

@ -57,6 +57,8 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(to_plain_date);
JS_DECLARE_NATIVE_FUNCTION(to_plain_time);
};
}

View file

@ -0,0 +1,11 @@
describe("normal behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainDateTime.prototype.toPlainDate).toHaveLength(0);
});
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 23, 0, 32, 18, 123, 456, 789);
const plainDate = plainDateTime.toPlainDate();
expect(plainDate.equals(new Temporal.PlainDate(2021, 7, 23))).toBeTrue();
});
});

View file

@ -0,0 +1,16 @@
describe("normal behavior", () => {
test("length is 0", () => {
expect(Temporal.PlainDateTime.prototype.toPlainTime).toHaveLength(0);
});
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 7, 31, 0, 32, 18, 123, 456, 789);
const plainTime = plainDateTime.toPlainTime();
expect(plainTime.hour).toBe(0);
expect(plainTime.minute).toBe(32);
expect(plainTime.second).toBe(18);
expect(plainTime.millisecond).toBe(123);
expect(plainTime.microsecond).toBe(456);
expect(plainTime.nanosecond).toBe(789);
});
});