summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS
diff options
context:
space:
mode:
authorLuke Wilde <lukew@serenityos.org>2021-11-05 01:30:58 +0000
committerLinus Groh <mail@linusgroh.de>2021-11-05 09:49:47 +0100
commit132a56f07c4271eef634abe41e3007c710f99c34 (patch)
tree809475333b98a05111b06048475159caf1d66136 /Userland/Libraries/LibJS
parentc098a6495b283d4379fbb31e8c4b562dc2587604 (diff)
downloadserenity-132a56f07c4271eef634abe41e3007c710f99c34.zip
LibJS: Implement Temporal.ZonedDateTime.prototype.withPlainTime
Diffstat (limited to 'Userland/Libraries/LibJS')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp43
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h1
-rw-r--r--Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainTime.js128
3 files changed, 172 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
index 8043106947..c80984ca11 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.cpp
@@ -64,6 +64,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_accessor(vm.names.eraYear, era_year_getter, {}, Attribute::Configurable);
u8 attr = Attribute::Writable | Attribute::Configurable;
+ define_native_function(vm.names.withPlainTime, with_plain_time, 0, 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);
@@ -702,6 +703,48 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::era_year_getter)
return TRY(calendar_era_year(global_object, calendar, *plain_date_time));
}
+// 6.3.31 Temporal.ZonedDateTime.prototype.withPlainTime ( [ plainTimeLike ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.withplaintime
+JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::with_plain_time)
+{
+ // 1. Let zonedDateTime be the this value.
+ // 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
+ auto* zoned_date_time = TRY(typed_this_object(global_object));
+
+ PlainTime* plain_time = nullptr;
+
+ // 3. If plainTimeLike is undefined, then
+ if (vm.argument(0).is_undefined()) {
+ // a. Let plainTime be ? CreateTemporalTime(0, 0, 0, 0, 0, 0).
+ plain_time = TRY(create_temporal_time(global_object, 0, 0, 0, 0, 0, 0));
+ }
+ // 4. Else,
+ else {
+ // a. Let plainTime be ? ToTemporalTime(plainTimeLike).
+ plain_time = TRY(to_temporal_time(global_object, vm.argument(0)));
+ }
+
+ // 5. Let timeZone be zonedDateTime.[[TimeZone]].
+ auto& time_zone = zoned_date_time->time_zone();
+
+ // 6. Let instant be ! CreateTemporalInstant(zonedDateTime.[[Nanoseconds]]).
+ auto* instant = MUST(create_temporal_instant(global_object, zoned_date_time->nanoseconds()));
+
+ // 7. Let calendar be zonedDateTime.[[Calendar]].
+ auto& calendar = zoned_date_time->calendar();
+
+ // 8. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(timeZone, instant, calendar).
+ auto* plain_date_time = TRY(builtin_time_zone_get_plain_date_time_for(global_object, &time_zone, *instant, calendar));
+
+ // 9. Let resultPlainDateTime be ? CreateTemporalDateTime(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainTime.[[ISOHour]], plainTime.[[ISOMinute]], plainTime.[[ISOSecond]], plainTime.[[ISOMillisecond]], plainTime.[[ISOMicrosecond]], plainTime.[[ISONanosecond]], calendar).
+ auto* result_plain_date_time = TRY(create_temporal_date_time(global_object, plain_date_time->iso_year(), plain_date_time->iso_month(), plain_date_time->iso_day(), plain_time->iso_hour(), plain_time->iso_minute(), plain_time->iso_second(), plain_time->iso_millisecond(), plain_time->iso_microsecond(), plain_time->iso_nanosecond(), calendar));
+
+ // 10. 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));
+
+ // 11. 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 8941100327..6745a424e9 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/ZonedDateTimePrototype.h
@@ -49,6 +49,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(offset_getter);
JS_DECLARE_NATIVE_FUNCTION(era_getter);
JS_DECLARE_NATIVE_FUNCTION(era_year_getter);
+ JS_DECLARE_NATIVE_FUNCTION(with_plain_time);
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.withPlainTime.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainTime.js
new file mode 100644
index 0000000000..f05634ba84
--- /dev/null
+++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/ZonedDateTime/ZonedDateTime.prototype.withPlainTime.js
@@ -0,0 +1,128 @@
+describe("correct behavior", () => {
+ test("length is 0", () => {
+ expect(Temporal.ZonedDateTime.prototype.withPlainTime).toHaveLength(0);
+ });
+
+ function checkCommonExpectedResults(withPlainTimeZonedDateTime) {
+ expect(withPlainTimeZonedDateTime.epochNanoseconds).toBe(1636064604200300400n);
+ expect(withPlainTimeZonedDateTime.epochMicroseconds).toBe(1636064604200300n);
+ expect(withPlainTimeZonedDateTime.epochMilliseconds).toBe(1636064604200);
+ expect(withPlainTimeZonedDateTime.epochSeconds).toBe(1636064604);
+ expect(withPlainTimeZonedDateTime.year).toBe(2021);
+ expect(withPlainTimeZonedDateTime.month).toBe(11);
+ expect(withPlainTimeZonedDateTime.monthCode).toBe("M11");
+ expect(withPlainTimeZonedDateTime.day).toBe(4);
+ expect(withPlainTimeZonedDateTime.hour).toBe(22);
+ expect(withPlainTimeZonedDateTime.minute).toBe(23);
+ expect(withPlainTimeZonedDateTime.second).toBe(24);
+ expect(withPlainTimeZonedDateTime.millisecond).toBe(200);
+ expect(withPlainTimeZonedDateTime.microsecond).toBe(300);
+ expect(withPlainTimeZonedDateTime.nanosecond).toBe(400);
+ expect(withPlainTimeZonedDateTime.dayOfWeek).toBe(4);
+ expect(withPlainTimeZonedDateTime.dayOfYear).toBe(308);
+ expect(withPlainTimeZonedDateTime.weekOfYear).toBe(44);
+ expect(withPlainTimeZonedDateTime.hoursInDay).toBe(24);
+ expect(withPlainTimeZonedDateTime.daysInWeek).toBe(7);
+ expect(withPlainTimeZonedDateTime.daysInYear).toBe(365);
+ expect(withPlainTimeZonedDateTime.monthsInYear).toBe(12);
+ expect(withPlainTimeZonedDateTime.inLeapYear).toBeFalse();
+ expect(withPlainTimeZonedDateTime.offset).toBe("+00:00");
+ expect(withPlainTimeZonedDateTime.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 plainTime = new Temporal.PlainTime(22, 23, 24, 200, 300, 400);
+ const withPlainTimeZonedDateTime = zonedDateTime.withPlainTime(plainTime);
+
+ checkCommonExpectedResults(withPlainTimeZonedDateTime);
+ });
+
+ 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 plainTimeLike = {
+ hour: 22,
+ minute: 23,
+ second: 24,
+ millisecond: 200,
+ microsecond: 300,
+ nanosecond: 400,
+ };
+ const withPlainTimeZonedDateTime = zonedDateTime.withPlainTime(plainTimeLike);
+
+ checkCommonExpectedResults(withPlainTimeZonedDateTime);
+ });
+
+ test("passing no parameters is the equivalent of using startOfDay", () => {
+ 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 startOfDayZonedDateTime = zonedDateTime.startOfDay();
+ const withPlainTimeZonedDateTime = zonedDateTime.withPlainTime();
+
+ expect(startOfDayZonedDateTime.epochNanoseconds).toBe(
+ withPlainTimeZonedDateTime.epochNanoseconds
+ );
+ expect(startOfDayZonedDateTime.epochMicroseconds).toBe(
+ withPlainTimeZonedDateTime.epochMicroseconds
+ );
+ expect(startOfDayZonedDateTime.epochMilliseconds).toBe(
+ withPlainTimeZonedDateTime.epochMilliseconds
+ );
+ expect(startOfDayZonedDateTime.epochSeconds).toBe(withPlainTimeZonedDateTime.epochSeconds);
+ expect(startOfDayZonedDateTime.year).toBe(withPlainTimeZonedDateTime.year);
+ expect(startOfDayZonedDateTime.month).toBe(withPlainTimeZonedDateTime.month);
+ expect(startOfDayZonedDateTime.monthCode).toBe(withPlainTimeZonedDateTime.monthCode);
+ expect(startOfDayZonedDateTime.day).toBe(withPlainTimeZonedDateTime.day);
+ expect(startOfDayZonedDateTime.hour).toBe(withPlainTimeZonedDateTime.hour);
+ expect(startOfDayZonedDateTime.minute).toBe(withPlainTimeZonedDateTime.minute);
+ expect(startOfDayZonedDateTime.second).toBe(withPlainTimeZonedDateTime.second);
+ expect(startOfDayZonedDateTime.millisecond).toBe(withPlainTimeZonedDateTime.millisecond);
+ expect(startOfDayZonedDateTime.microsecond).toBe(withPlainTimeZonedDateTime.microsecond);
+ expect(startOfDayZonedDateTime.nanosecond).toBe(withPlainTimeZonedDateTime.nanosecond);
+ expect(startOfDayZonedDateTime.dayOfWeek).toBe(withPlainTimeZonedDateTime.dayOfWeek);
+ expect(startOfDayZonedDateTime.dayOfYear).toBe(withPlainTimeZonedDateTime.dayOfYear);
+ expect(startOfDayZonedDateTime.weekOfYear).toBe(withPlainTimeZonedDateTime.weekOfYear);
+ expect(startOfDayZonedDateTime.hoursInDay).toBe(withPlainTimeZonedDateTime.hoursInDay);
+ expect(startOfDayZonedDateTime.daysInWeek).toBe(withPlainTimeZonedDateTime.daysInWeek);
+ expect(startOfDayZonedDateTime.daysInYear).toBe(withPlainTimeZonedDateTime.daysInYear);
+ expect(startOfDayZonedDateTime.monthsInYear).toBe(withPlainTimeZonedDateTime.monthsInYear);
+ expect(startOfDayZonedDateTime.inLeapYear).toBe(withPlainTimeZonedDateTime.inLeapYear);
+ expect(startOfDayZonedDateTime.offset).toBe(withPlainTimeZonedDateTime.offset);
+ expect(startOfDayZonedDateTime.offsetNanoseconds).toBe(
+ withPlainTimeZonedDateTime.offsetNanoseconds
+ );
+ });
+
+ // FIXME: Enable when time string parsing is implemented.
+ test.skip("from plain time 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 withPlainTimeZonedDateTime = zonedDateTime.withPlainTime("22:23:24.200300400");
+
+ checkCommonExpectedResults(withPlainTimeZonedDateTime);
+ });
+});
+
+describe("errors", () => {
+ test("this value must be a Temporal.ZonedDateTime object", () => {
+ expect(() => {
+ Temporal.ZonedDateTime.prototype.withPlainTime.call("foo");
+ }).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
+ });
+
+ test("invalid plain time-like object", () => {
+ expect(() => {
+ new Temporal.ZonedDateTime(1n, {}).withPlainTime({});
+ }).toThrowWithMessage(TypeError, "Invalid plain time-like object");
+
+ expect(() => {
+ new Temporal.ZonedDateTime(1n, {}).withPlainTime({ foo: 1, bar: 2 });
+ }).toThrowWithMessage(TypeError, "Invalid plain time-like object");
+ });
+});