summaryrefslogtreecommitdiff
path: root/Userland/Libraries
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-11-05 01:34:31 +0000
committerLinus Groh <mail@linusgroh.de>2021-11-05 09:49:47 +0100
commitd5f2745a199051f932e6c2bd71caa4b27bc74987 (patch)
treee2cc094ce2fe5c60de4b0a875cf6eee62f624093 /Userland/Libraries
parent132a56f07c4271eef634abe41e3007c710f99c34 (diff)
downloadserenity-d5f2745a199051f932e6c2bd71caa4b27bc74987.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.withPlainDate
Diffstat (limited to 'Userland/Libraries')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp33
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainDate.js84
3 files changed, 118 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index c80984ca11..87fbe70ba5 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -65,6 +65,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
u8 attr = Attribute::Writable | Attribute::Configurable;
define_native_function(vm.names.withPlainTime, with_plain_time, 0, attr);
+ define_native_function(vm.names.withPlainDate, with_plain_date, 1, attr);
define_native_function(vm.names.valueOf, value_of, 0, attr);
define_native_function(vm.names.startOfDay, start_of_day, 0, attr);
define_native_function(vm.names.toInstant, to_instant, 0, attr);
@@ -745,6 +746,38 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with_plain_time)
return MUST(create_temporal_zoned_date_time(global_object, instant->nanoseconds(), time_zone, calendar));
}
+// 6.3.32 Temporal.ZonedDateTime.prototype.withPlainDate ( plainDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.withplaindate
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with_plain_date)
+{
+ // 1. Let zonedDateTime be the this value.
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+ auto* zoned_date_time = TRY(typed_this_object(global_object));
+
+ // 3. Let plainDate be ? ToTemporalDate(plainDateLike).
+ auto* plain_date = TRY(to_temporal_date(global_object, vm.argument(0)));
+
+ // 4. Let timeZone be zonedDateTime.[[TimeZone]].
+ auto& time_zone = zoned_date_time->time_zone();
+
+ // 5. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]).
+ auto* instant = MUST(create_temporal_instant(global_object, zoned_date_time->nanoseconds()));
+
+ // 6. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, zonedDateTime.[[Calendar]]).
+ auto* plain_date_time = TRY(builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, zoned_date_time->calendar()));
+
+ // 7. Let calendar be ? ConsolidateCalendars(zonedDateTime.[[Calendar]], plainDate.[[Calendar]]).
+ auto* calendar = TRY(consolidate_calendars(global_object, zoned_date_time->calendar(), plain_date->calendar()));
+
+ // 8. Let resultPlainDateTime be ? CreateTemporalDateTime(plainDate.[[ISOYear]], plainDate.[[ISOMonth]], plainDate.[[ISODay]], plainDateTime.[[ISOHour]], plainDateTime.[[ISOMinute]], plainDateTime.[[ISOSecond]], plainDateTime.[[ISOMillisecond]], plainDateTime.[[ISOMicrosecond]], plainDateTime.[[ISONanosecond]], calendar).
+ auto* result_plain_date_time = TRY(create_temporal_date_time(global_object, plain_date->iso_year(), plain_date->iso_month(), plain_date->iso_day(), plain_date_time->iso_hour(), plain_date_time->iso_minute(), plain_date_time->iso_second(), plain_date_time->iso_millisecond(), plain_date_time->iso_microsecond(), plain_date_time->iso_nanosecond(), *calendar));
+
+ // 9. Set instant to ? BuiltinTimeZoneGetInstantFor(timeZone, resultPlainDateTime, "compatible").
+ instant = TRY(builtin_time_zone_get_instant_for(global_object, &time_zone, *result_plain_date_time, "compatible"sv));
+
+ // 10. Return ! CreateTemporalZonedDateTime(instant.[[Nanoseconds]], timeZone, calendar).
+ return MUST(create_temporal_zoned_date_time(global_object, instant->nanoseconds(), time_zone, *calendar));
+}
+
// 6.3.44 Temporal.ZonedDateTime.prototype.valueOf ( ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.valueof
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::value_of)
{
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
index 6745a424e9..bc5bc59bad 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -50,6 +50,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(era_getter);
JS_DECLARE_NATIVE_FUNCTION(era_year_getter);
JS_DECLARE_NATIVE_FUNCTION(with_plain_time);
+ JS_DECLARE_NATIVE_FUNCTION(with_plain_date);
JS_DECLARE_NATIVE_FUNCTION(value_of);
JS_DECLARE_NATIVE_FUNCTION(start_of_day);
JS_DECLARE_NATIVE_FUNCTION(to_instant);
diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainDate.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainDate.js
new file mode 100644
index 0000000000..3b33d68203
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainDate.js
@@ -0,0 +1,84 @@
+describe("correct behavior", () => {
+ test("length is 1", () => {
+ expect(Temporal.ZonedDateTime.prototype.withPlainDate).toHaveLength(1);
+ });
+
+ function checkExpectedResults(withPlainDateZonedDateTime) {
+ expect(withPlainDateZonedDateTime.epochNanoseconds).toBe(1640467016100200300n);
+ expect(withPlainDateZonedDateTime.epochMicroseconds).toBe(1640467016100200n);
+ expect(withPlainDateZonedDateTime.epochMilliseconds).toBe(1640467016100);
+ expect(withPlainDateZonedDateTime.epochSeconds).toBe(1640467016);
+ expect(withPlainDateZonedDateTime.year).toBe(2021);
+ expect(withPlainDateZonedDateTime.month).toBe(12);
+ expect(withPlainDateZonedDateTime.monthCode).toBe("M12");
+ expect(withPlainDateZonedDateTime.day).toBe(25);
+ expect(withPlainDateZonedDateTime.hour).toBe(21);
+ expect(withPlainDateZonedDateTime.minute).toBe(16);
+ expect(withPlainDateZonedDateTime.second).toBe(56);
+ expect(withPlainDateZonedDateTime.millisecond).toBe(100);
+ expect(withPlainDateZonedDateTime.microsecond).toBe(200);
+ expect(withPlainDateZonedDateTime.nanosecond).toBe(300);
+ expect(withPlainDateZonedDateTime.dayOfWeek).toBe(6);
+ expect(withPlainDateZonedDateTime.dayOfYear).toBe(359);
+ expect(withPlainDateZonedDateTime.weekOfYear).toBe(51);
+ expect(withPlainDateZonedDateTime.hoursInDay).toBe(24);
+ expect(withPlainDateZonedDateTime.daysInWeek).toBe(7);
+ expect(withPlainDateZonedDateTime.daysInYear).toBe(365);
+ expect(withPlainDateZonedDateTime.monthsInYear).toBe(12);
+ expect(withPlainDateZonedDateTime.inLeapYear).toBeFalse();
+ expect(withPlainDateZonedDateTime.offset).toBe("+00:00");
+ expect(withPlainDateZonedDateTime.offsetNanoseconds).toBe(0);
+ }
+
+ test("basic functionality", () => {
+ const plainDateTime = new Temporal.PlainDateTime(2021, 11, 4, 21, 16, 56, 100, 200, 300);
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
+ const plainDate = new Temporal.PlainDate(2021, 12, 25);
+ const withPlainDateZonedDateTime = zonedDateTime.withPlainDate(plainDate);
+
+ checkExpectedResults(withPlainDateZonedDateTime);
+ });
+
+ test("plain time-like object", () => {
+ const plainDateTime = new Temporal.PlainDateTime(2021, 11, 4, 21, 16, 56, 100, 200, 300);
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
+ const plainDateLike = { year: 2021, month: 12, day: 25 };
+ const withPlainDateZonedDateTime = zonedDateTime.withPlainDate(plainDateLike);
+
+ checkExpectedResults(withPlainDateZonedDateTime);
+ });
+
+ // FIXME: Enable when time string parsing is implemented.
+ test.skip("from plain date string", () => {
+ const plainDateTime = new Temporal.PlainDateTime(2021, 11, 4, 21, 16, 56, 100, 200, 300);
+ const timeZone = new Temporal.TimeZone("UTC");
+ const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
+ const withPlainDateZonedDateTime = zonedDateTime.withPlainDate("2021-12-25");
+
+ checkExpectedResults(withPlainDateZonedDateTime);
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Temporal.ZonedDateTime.prototype.withPlainDate.call("foo");
+ }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
+ });
+
+ test("missing properties", () => {
+ expect(() => {
+ new Temporal.ZonedDateTime(1n, {}).withPlainDate({});
+ }).toThrowWithMessage(TypeError, "Required property year is missing or undefined");
+
+ expect(() => {
+ new Temporal.ZonedDateTime(1n, {}).withPlainDate({ year: 1 });
+ }).toThrowWithMessage(TypeError, "Required property month is missing or undefined");
+
+ expect(() => {
+ new Temporal.ZonedDateTime(1n, {}).withPlainDate({ year: 1, month: 1 });
+ }).toThrowWithMessage(TypeError, "Required property day is missing or undefined");
+ });
+});