mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 05:47:35 +00:00
LibJS: Implement Temporal.PlainTime.from()
This commit is contained in:
parent
bb857330d2
commit
a77cdc5f92
3 changed files with 82 additions and 0 deletions
|
@ -4,6 +4,7 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <AK/TypeCasts.h>
|
||||||
#include <LibJS/Runtime/GlobalObject.h>
|
#include <LibJS/Runtime/GlobalObject.h>
|
||||||
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
|
||||||
#include <LibJS/Runtime/Temporal/PlainTime.h>
|
#include <LibJS/Runtime/Temporal/PlainTime.h>
|
||||||
|
@ -26,6 +27,9 @@ void PlainTimeConstructor::initialize(GlobalObject& global_object)
|
||||||
// 4.2.1 Temporal.PlainTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-prototype
|
// 4.2.1 Temporal.PlainTime.prototype, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-prototype
|
||||||
define_direct_property(vm.names.prototype, global_object.temporal_plain_time_prototype(), 0);
|
define_direct_property(vm.names.prototype, global_object.temporal_plain_time_prototype(), 0);
|
||||||
|
|
||||||
|
u8 attr = Attribute::Writable | Attribute::Configurable;
|
||||||
|
define_native_function(vm.names.from, from, 1, attr);
|
||||||
|
|
||||||
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
define_direct_property(vm.names.length, Value(0), Attribute::Configurable);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,4 +92,30 @@ Value PlainTimeConstructor::construct(FunctionObject& new_target)
|
||||||
return create_temporal_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, &new_target);
|
return create_temporal_time(global_object, hour, minute, second, millisecond, microsecond, nanosecond, &new_target);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 4.2.2 Temporal.PlainTime.from ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.plaintime.from
|
||||||
|
JS_DEFINE_NATIVE_FUNCTION(PlainTimeConstructor::from)
|
||||||
|
{
|
||||||
|
// 1. Set options to ? GetOptionsObject(options).
|
||||||
|
auto* options = get_options_object(global_object, vm.argument(1));
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
// 2. Let overflow be ? ToTemporalOverflow(options).
|
||||||
|
auto overflow = to_temporal_overflow(global_object, *options);
|
||||||
|
if (vm.exception())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto item = vm.argument(0);
|
||||||
|
|
||||||
|
// 3. If Type(item) is Object and item has an [[InitializedTemporalTime]] internal slot, then
|
||||||
|
if (item.is_object() && is<PlainTime>(item.as_object())) {
|
||||||
|
auto& plain_time = static_cast<PlainTime&>(item.as_object());
|
||||||
|
// a. Return ? CreateTemporalTime(item.[[ISOHour]], item.[[ISOMinute]], item.[[ISOSecond]], item.[[ISOMillisecond]], item.[[ISOMicrosecond]], item.[[ISONanosecond]]).
|
||||||
|
return create_temporal_time(global_object, plain_time.iso_hour(), plain_time.iso_minute(), plain_time.iso_second(), plain_time.iso_millisecond(), plain_time.iso_microsecond(), plain_time.iso_nanosecond());
|
||||||
|
}
|
||||||
|
|
||||||
|
// 4. Return ? ToTemporalTime(item, overflow).
|
||||||
|
return to_temporal_time(global_object, item, *overflow);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,8 @@ public:
|
||||||
|
|
||||||
private:
|
private:
|
||||||
virtual bool has_constructor() const override { return true; }
|
virtual bool has_constructor() const override { return true; }
|
||||||
|
|
||||||
|
JS_DECLARE_NATIVE_FUNCTION(from);
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,50 @@
|
||||||
|
describe("correct behavior", () => {
|
||||||
|
test("length is 1", () => {
|
||||||
|
expect(Temporal.PlainTime.from).toHaveLength(1);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("PlainTime instance argument", () => {
|
||||||
|
const plainTime = new Temporal.PlainTime(18, 45, 37, 1, 2, 3);
|
||||||
|
const createdPlainTime = Temporal.PlainTime.from(plainTime);
|
||||||
|
expect(createdPlainTime.hour).toBe(18);
|
||||||
|
expect(createdPlainTime.minute).toBe(45);
|
||||||
|
expect(createdPlainTime.second).toBe(37);
|
||||||
|
expect(createdPlainTime.millisecond).toBe(1);
|
||||||
|
expect(createdPlainTime.microsecond).toBe(2);
|
||||||
|
expect(createdPlainTime.nanosecond).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("PlainDateTime instance argument", () => {
|
||||||
|
const plainDateTime = new Temporal.PlainDateTime(2021, 8, 27, 18, 45, 37, 1, 2, 3);
|
||||||
|
const createdPlainTime = Temporal.PlainTime.from(plainDateTime);
|
||||||
|
expect(createdPlainTime.hour).toBe(18);
|
||||||
|
expect(createdPlainTime.minute).toBe(45);
|
||||||
|
expect(createdPlainTime.second).toBe(37);
|
||||||
|
expect(createdPlainTime.millisecond).toBe(1);
|
||||||
|
expect(createdPlainTime.microsecond).toBe(2);
|
||||||
|
expect(createdPlainTime.nanosecond).toBe(3);
|
||||||
|
});
|
||||||
|
|
||||||
|
test("ZonedDateTime instance argument", () => {
|
||||||
|
const timeZone = new Temporal.TimeZone("UTC");
|
||||||
|
const zonedDateTime = new Temporal.ZonedDateTime(1627318123456789000n, timeZone);
|
||||||
|
const createdPlainTime = Temporal.PlainTime.from(zonedDateTime);
|
||||||
|
expect(createdPlainTime.hour).toBe(16);
|
||||||
|
expect(createdPlainTime.minute).toBe(48);
|
||||||
|
expect(createdPlainTime.second).toBe(43);
|
||||||
|
expect(createdPlainTime.millisecond).toBe(456);
|
||||||
|
expect(createdPlainTime.microsecond).toBe(789);
|
||||||
|
expect(createdPlainTime.nanosecond).toBe(0);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Un-skip once ParseISODateTime & ParseTemporalTimeString are implemented
|
||||||
|
test.skip("PlainTime string argument", () => {
|
||||||
|
const createdPlainTime = Temporal.PlainTime.from("2021-08-27T18:44:11Z");
|
||||||
|
expect(createdPlainTime.hour).toBe(18);
|
||||||
|
expect(createdPlainTime.minute).toBe(44);
|
||||||
|
expect(createdPlainTime.second).toBe(11);
|
||||||
|
expect(createdPlainTime.millisecond).toBe(0);
|
||||||
|
expect(createdPlainTime.microsecond).toBe(0);
|
||||||
|
expect(createdPlainTime.nanosecond).toBe(0);
|
||||||
|
});
|
||||||
|
});
|
Loading…
Add table
Add a link
Reference in a new issue