LibJS: Implement Temporal.Instant.prototype.toZonedDateTime()

This commit is contained in:
Linus Groh 2021-09-09 00:27:19 +01:00
parent 1c78ff1b9f
commit 6607d1dfb1
Notes: sideshowbarker 2024-07-18 04:23:47 +09:00
5 changed files with 116 additions and 0 deletions

View File

@ -435,6 +435,7 @@ namespace JS {
P(toTimeString) \
P(toUpperCase) \
P(toUTCString) \
P(toZonedDateTime) \
P(trace) \
P(trim) \
P(trimEnd) \

View File

@ -22,6 +22,7 @@ public:
virtual ~Instant() override = default;
[[nodiscard]] BigInt const& nanoseconds() const { return m_nanoseconds; }
[[nodiscard]] BigInt& nanoseconds() { return m_nanoseconds; }
private:
virtual void visit_edges(Visitor&) override;

View File

@ -8,10 +8,12 @@
#include <LibCrypto/BigInt/UnsignedBigInteger.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/AbstractOperations.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/Duration.h>
#include <LibJS/Runtime/Temporal/Instant.h>
#include <LibJS/Runtime/Temporal/InstantPrototype.h>
#include <LibJS/Runtime/Temporal/TimeZone.h>
#include <LibJS/Runtime/Temporal/ZonedDateTime.h>
namespace JS::Temporal {
@ -46,6 +48,7 @@ void InstantPrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
define_native_function(vm.names.toJSON, to_json, 0, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.toZonedDateTime, to_zoned_date_time, 1, attr);
}
static Instant* typed_this(GlobalObject& global_object)
@ -509,4 +512,60 @@ JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::value_of)
return {};
}
// 8.3.17 Temporal.Instant.prototype.toZonedDateTime ( item ), https://tc39.es/proposal-temporal/#sec-temporal.instant.prototype.tozoneddatetime
JS_DEFINE_NATIVE_FUNCTION(InstantPrototype::to_zoned_date_time)
{
auto item = vm.argument(0);
// 1. Let instant be the this value.
// 2. Perform ? RequireInternalSlot(instant, [[InitializedTemporalInstant]]).
auto* instant = typed_this(global_object);
if (vm.exception())
return {};
// 3. If Type(item) is not Object, then
if (!item.is_object()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::NotAnObject, item);
return {};
}
// 4. Let calendarLike be ? Get(item, "calendar").
auto calendar_like = item.as_object().get(vm.names.calendar);
if (vm.exception())
return {};
// 5. If calendarLike is undefined, then
if (calendar_like.is_undefined()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.calendar.as_string());
return {};
}
// 6. Let calendar be ? ToTemporalCalendar(calendarLike).
auto* calendar = to_temporal_calendar(global_object, calendar_like);
if (vm.exception())
return {};
// 7. Let temporalTimeZoneLike be ? Get(item, "timeZone").
auto temporal_time_zone_like = item.as_object().get(vm.names.timeZone);
if (vm.exception())
return {};
// 8. If temporalTimeZoneLike is undefined, then
if (temporal_time_zone_like.is_undefined()) {
// a. Throw a TypeError exception.
vm.throw_exception<TypeError>(global_object, ErrorType::TemporalMissingRequiredProperty, vm.names.timeZone.as_string());
return {};
}
// 9. Let timeZone be ? ToTemporalTimeZone(temporalTimeZoneLike).
auto* time_zone = to_temporal_time_zone(global_object, temporal_time_zone_like);
if (vm.exception())
return {};
// 10. Return ? CreateTemporalZonedDateTime(instant.[[Nanoseconds]], timeZone, calendar).
return create_temporal_zoned_date_time(global_object, instant->nanoseconds(), *time_zone, *calendar);
}
}

View File

@ -33,6 +33,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);
JS_DECLARE_NATIVE_FUNCTION(to_json);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(to_zoned_date_time);
};
}

View File

@ -0,0 +1,54 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.Instant.prototype.toZonedDateTime).toHaveLength(1);
});
test("basic functionality", () => {
const instant = new Temporal.Instant(1625614921123456789n);
const calendar = new Temporal.Calendar("iso8601");
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = instant.toZonedDateTime({ calendar, timeZone });
expect(zonedDateTime.year).toBe(2021);
expect(zonedDateTime.month).toBe(7);
expect(zonedDateTime.day).toBe(6);
expect(zonedDateTime.hour).toBe(23);
expect(zonedDateTime.minute).toBe(42);
expect(zonedDateTime.second).toBe(1);
expect(zonedDateTime.millisecond).toBe(123);
expect(zonedDateTime.microsecond).toBe(456);
expect(zonedDateTime.nanosecond).toBe(789);
expect(zonedDateTime.calendar).toBe(calendar);
expect(zonedDateTime.timeZone).toBe(timeZone);
});
});
describe("errors", () => {
test("this value must be a Temporal.Instant object", () => {
expect(() => {
Temporal.Instant.prototype.toZonedDateTime.call("foo");
}).toThrowWithMessage(TypeError, "Not a Temporal.Instant");
});
test("items argument must be an object", () => {
const instant = new Temporal.Instant(0n);
for (const value of [123, NaN, Infinity, true, false, null, undefined]) {
expect(() => {
instant.toZonedDateTime(value);
}).toThrowWithMessage(TypeError, `${value} is not an object`);
}
});
test("items argument must have a 'calendar' property", () => {
const instant = new Temporal.Instant(0n);
expect(() => {
instant.toZonedDateTime({});
}).toThrowWithMessage(TypeError, "Required property calendar is missing or undefined");
});
test("items argument must have a 'timeZone' property", () => {
const instant = new Temporal.Instant(0n);
expect(() => {
instant.toZonedDateTime({ calendar: {} });
}).toThrowWithMessage(TypeError, "Required property timeZone is missing or undefined");
});
});