1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:58:12 +00:00

LibJS: Implement Temporal.ZonedDateTime.prototype.subtract

This commit is contained in:
Luke Wilde 2021-11-12 00:36:58 +00:00 committed by Linus Groh
parent 9b8524b463
commit f65d25682c
3 changed files with 90 additions and 0 deletions

View file

@ -70,6 +70,7 @@ void ZonedDateTimePrototype::initialize(GlobalObject& global_object)
define_native_function(vm.names.withTimeZone, with_time_zone, 1, attr);
define_native_function(vm.names.withCalendar, with_calendar, 1, attr);
define_native_function(vm.names.add, add, 1, attr);
define_native_function(vm.names.subtract, subtract, 1, attr);
define_native_function(vm.names.equals, equals, 1, attr);
define_native_function(vm.names.toString, to_string, 0, attr);
define_native_function(vm.names.toLocaleString, to_locale_string, 0, attr);
@ -840,6 +841,32 @@ JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::add)
return MUST(create_temporal_zoned_date_time(global_object, *epoch_nanoseconds, time_zone, calendar));
}
// 6.3.36 Temporal.ZonedDateTime.prototype.subtract ( temporalDurationLike [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.subtract
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::subtract)
{
// 1. Let zonedDateTime be the this value.
// 2. Perform ? RequireInternalSlot(zonedDateTime, [[InitializedTemporalZonedDateTime]]).
auto* zoned_date_time = TRY(typed_this_object(global_object));
// 3. Let duration be ? ToLimitedTemporalDuration(temporalDurationLike, « »).
auto duration = TRY(to_limited_temporal_duration(global_object, vm.argument(0), {}));
// 4. Set options to ? GetOptionsObject(options).
auto* options = TRY(get_options_object(global_object, vm.argument(1)));
// 5. Let timeZone be zonedDateTime.[[TimeZone]].
auto& time_zone = zoned_date_time->time_zone();
// 6. Let calendar be zonedDateTime.[[Calendar]].
auto& calendar = zoned_date_time->calendar();
// 7. Let epochNanoseconds be ? AddZonedDateTime(zonedDateTime.[[Nanoseconds]], timeZone, calendar, duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], duration.[[Hours]], duration.[[Minutes]], duration.[[Seconds]], duration.[[Milliseconds]], duration.[[Microseconds]], duration.[[Nanoseconds]], options).
auto* epoch_nanoseconds = TRY(add_zoned_date_time(global_object, zoned_date_time->nanoseconds(), &time_zone, calendar, -duration.years, -duration.months, -duration.weeks, -duration.days, -duration.hours, -duration.minutes, -duration.seconds, -duration.milliseconds, -duration.microseconds, -duration.nanoseconds, options));
// 8. Return ! CreateTemporalZonedDateTime(epochNanoseconds, timeZone, calendar).
return MUST(create_temporal_zoned_date_time(global_object, *epoch_nanoseconds, time_zone, calendar));
}
// 6.3.40 Temporal.ZonedDateTime.prototype.equals ( other ), https://tc39.es/proposal-temporal/#sec-temporal.zoneddatetime.prototype.equals
JS_DEFINE_NATIVE_FUNCTION(ZonedDateTimePrototype::equals)
{

View file

@ -54,6 +54,7 @@ private:
JS_DECLARE_NATIVE_FUNCTION(with_time_zone);
JS_DECLARE_NATIVE_FUNCTION(with_calendar);
JS_DECLARE_NATIVE_FUNCTION(add);
JS_DECLARE_NATIVE_FUNCTION(subtract);
JS_DECLARE_NATIVE_FUNCTION(equals);
JS_DECLARE_NATIVE_FUNCTION(to_string);
JS_DECLARE_NATIVE_FUNCTION(to_locale_string);

View file

@ -0,0 +1,62 @@
describe("correct behavior", () => {
test("length is 1", () => {
expect(Temporal.ZonedDateTime.prototype.subtract).toHaveLength(1);
});
test("basic functionality", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 11, 12, 0, 22, 30, 100, 200, 300);
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
const dayDuration = new Temporal.Duration(0, 0, 0, 1);
const result = zonedDateTime.subtract(dayDuration);
expect(zonedDateTime.day).toBe(12);
expect(zonedDateTime.epochNanoseconds).toBe(1636676550100200300n);
expect(result.day).toBe(11);
expect(result.epochNanoseconds).toBe(1636590150100200300n);
});
test("duration-like object", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 11, 12, 0, 22, 30, 100, 200, 300);
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
const dayDuration = { days: 1 };
const result = zonedDateTime.subtract(dayDuration);
expect(zonedDateTime.day).toBe(12);
expect(zonedDateTime.epochNanoseconds).toBe(1636676550100200300n);
expect(result.day).toBe(11);
expect(result.epochNanoseconds).toBe(1636590150100200300n);
});
// FIXME: Unskip when ParseTemporalDurationString is implemented.
test.skip("duration string", () => {
const plainDateTime = new Temporal.PlainDateTime(2021, 11, 12, 0, 22, 30, 100, 200, 300);
const timeZone = new Temporal.TimeZone("UTC");
const zonedDateTime = plainDateTime.toZonedDateTime(timeZone);
const dayDuration = "P1D";
const result = zonedDateTime.subtract(dayDuration);
expect(zonedDateTime.day).toBe(12);
expect(zonedDateTime.epochNanoseconds).toBe(1636676550100200300n);
expect(result.day).toBe(11);
expect(result.epochNanoseconds).toBe(1636590150100200300n);
});
});
describe("errors", () => {
test("this value must be a Temporal.ZonedDateTime object", () => {
expect(() => {
Temporal.ZonedDateTime.prototype.subtract.call("foo");
}).toThrowWithMessage(TypeError, "Not an object of type Temporal.ZonedDateTime");
});
test("invalid duration-like object", () => {
expect(() => {
new Temporal.ZonedDateTime(1n, {}).subtract({});
}).toThrowWithMessage(TypeError, "Invalid duration-like object");
});
});