diff options
author | Linus Groh <mail@linusgroh.de> | 2021-08-30 20:44:05 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2021-08-30 22:33:10 +0100 |
commit | e3254bf4c5df98d96d962d21bd553f1d6b5486ec (patch) | |
tree | 0dbd30233703292ca790acc9580c727f9bf5588d /Userland/Libraries/LibJS | |
parent | f492e98f19c3a70b9204702181d20c46ed90896b (diff) | |
download | serenity-e3254bf4c5df98d96d962d21bd553f1d6b5486ec.zip |
LibJS: Implement Temporal.Calendar.prototype.dateAdd()
Diffstat (limited to 'Userland/Libraries/LibJS')
6 files changed, 94 insertions, 0 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h index 745052bfdb..d2010126e4 100644 --- a/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h +++ b/Userland/Libraries/LibJS/Runtime/CommonPropertyNames.h @@ -107,6 +107,7 @@ namespace JS { P(count) \ P(countReset) \ P(create) \ + P(dateAdd) \ P(dateFromFields) \ P(day) \ P(dayOfWeek) \ diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp index 2410713e1f..d80804d601 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.cpp @@ -10,6 +10,7 @@ #include <LibJS/Runtime/Temporal/AbstractOperations.h> #include <LibJS/Runtime/Temporal/Calendar.h> #include <LibJS/Runtime/Temporal/CalendarPrototype.h> +#include <LibJS/Runtime/Temporal/Duration.h> #include <LibJS/Runtime/Temporal/PlainDate.h> #include <LibJS/Runtime/Temporal/PlainDateTime.h> #include <LibJS/Runtime/Temporal/PlainMonthDay.h> @@ -38,6 +39,7 @@ void CalendarPrototype::initialize(GlobalObject& global_object) define_native_function(vm.names.dateFromFields, date_from_fields, 2, attr); define_native_function(vm.names.yearMonthFromFields, year_month_from_fields, 2, attr); define_native_function(vm.names.monthDayFromFields, month_day_from_fields, 2, attr); + define_native_function(vm.names.dateAdd, date_add, 3, attr); define_native_function(vm.names.year, year, 1, attr); define_native_function(vm.names.month, month, 1, attr); define_native_function(vm.names.monthCode, month_code, 1, attr); @@ -183,6 +185,48 @@ JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::month_day_from_fields) return create_temporal_month_day(global_object, result->month, result->day, *calendar, result->reference_iso_year); } +// 12.4.7 Temporal.Calendar.prototype.dateAdd ( date, duration, options ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.dateadd +// NOTE: This is the minimum dateAdd implementation for engines without ECMA-402. +JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::date_add) +{ + // 1. Let calendar be the this value. + // 2. Perform ? RequireInternalSlot(calendar, [[InitializedTemporalCalendar]]). + auto* calendar = typed_this(global_object); + if (vm.exception()) + return {}; + + // 3. Assert: calendar.[[Identifier]] is "iso8601". + VERIFY(calendar->identifier() == "iso8601"sv); + + // 4. Set date to ? ToTemporalDate(date). + auto* date = to_temporal_date(global_object, vm.argument(0)); + if (vm.exception()) + return {}; + + // 5. Set duration to ? ToTemporalDuration(duration). + auto* duration = to_temporal_duration(global_object, vm.argument(1)); + if (vm.exception()) + return {}; + + // 6. Set options to ? GetOptionsObject(options). + auto* options = get_options_object(global_object, vm.argument(2)); + if (vm.exception()) + return {}; + + // 7. Let overflow be ? ToTemporalOverflow(options). + auto overflow = to_temporal_overflow(global_object, *options); + if (vm.exception()) + return {}; + + // 8. Let result be ? AddISODate(date.[[ISOYear]], date.[[ISOMonth]], date.[[ISODay]], duration.[[Years]], duration.[[Months]], duration.[[Weeks]], duration.[[Days]], overflow). + auto result = add_iso_date(global_object, date->iso_year(), date->iso_month(), date->iso_day(), duration->years(), duration->months(), duration->weeks(), duration->days(), *overflow); + if (vm.exception()) + return {}; + + // 9. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar). + return create_temporal_date(global_object, result->year, result->month, result->day, *calendar); +} + // 12.4.9 Temporal.Calendar.prototype.year ( temporalDateLike ), https://tc39.es/proposal-temporal/#sec-temporal.calendar.prototype.year // NOTE: This is the minimum year implementation for engines without ECMA-402. JS_DEFINE_NATIVE_FUNCTION(CalendarPrototype::year) diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h index cbbf458614..9f8679ac7b 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/CalendarPrototype.h @@ -23,6 +23,7 @@ private: JS_DECLARE_NATIVE_FUNCTION(date_from_fields); JS_DECLARE_NATIVE_FUNCTION(year_month_from_fields); JS_DECLARE_NATIVE_FUNCTION(month_day_from_fields); + JS_DECLARE_NATIVE_FUNCTION(date_add); JS_DECLARE_NATIVE_FUNCTION(year); JS_DECLARE_NATIVE_FUNCTION(month); JS_DECLARE_NATIVE_FUNCTION(month_code); diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp index 848a5bcf3b..4170dbbbe1 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp @@ -379,6 +379,38 @@ Optional<String> temporal_date_to_string(GlobalObject& global_object, PlainDate& return String::formatted("{}-{}-{}{}", year, month, day, calendar); } +// 3.5.9 AddISODate ( year, month, day, years, months, weeks, days, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-addisodate +Optional<ISODate> add_iso_date(GlobalObject& global_object, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, String const& overflow) +{ + auto& vm = global_object.vm(); + + // 1. Assert: year, month, day, years, months, weeks, and days are integers. + VERIFY(years == trunc(years) && months == trunc(months) && weeks == trunc(weeks) && days == trunc(days)); + + // 2. Assert: overflow is either "constrain" or "reject". + VERIFY(overflow == "constrain"sv || overflow == "reject"sv); + + // 3. Let intermediate be ! BalanceISOYearMonth(year + years, month + months). + auto intermediate_year_month = balance_iso_year_month(year + years, month + months); + + // 4. Let intermediate be ? RegulateISODate(intermediate.[[Year]], intermediate.[[Month]], day, overflow). + auto intermediate_date = regulate_iso_date(global_object, intermediate_year_month.year, intermediate_year_month.month, day, overflow); + if (vm.exception()) + return {}; + + // 5. Set days to days + 7 × weeks. + days += 7 * weeks; + + // 6. Let d be intermediate.[[Day]] + days. + auto d = intermediate_date->day + days; + + // 7. Let intermediate be ! BalanceISODate(intermediate.[[Year]], intermediate.[[Month]], d). + auto intermediate = balance_iso_date(intermediate_date->year, intermediate_date->month, d); + + // 8. Return ? RegulateISODate(intermediate.[[Year]], intermediate.[[Month]], intermediate.[[Day]], overflow). + return regulate_iso_date(global_object, intermediate.year, intermediate.month, intermediate.day, overflow); +} + // 3.5.10 CompareISODate ( y1, m1, d1, y2, m2, d2 ), https://tc39.es/proposal-temporal/#sec-temporal-compareisodate i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2) { diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h index 03497bc29b..ec196704a4 100644 --- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h +++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.h @@ -47,6 +47,7 @@ bool is_valid_iso_date(i32 year, u8 month, u8 day); ISODate balance_iso_date(double year, double month, double day); String pad_iso_year(i32 y); Optional<String> temporal_date_to_string(GlobalObject&, PlainDate&, StringView show_calendar); +Optional<ISODate> add_iso_date(GlobalObject&, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, String const& overflow); i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2); } diff --git a/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.dateAdd.js b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.dateAdd.js new file mode 100644 index 0000000000..dcdb1f36a4 --- /dev/null +++ b/Userland/Libraries/LibJS/Tests/builtins/Temporal/Calendar/Calendar.prototype.dateAdd.js @@ -0,0 +1,15 @@ +describe("correct behavior", () => { + test("length is 3", () => { + expect(Temporal.Calendar.prototype.dateAdd).toHaveLength(3); + }); + + test("basic functionality", () => { + const calendar = new Temporal.Calendar("iso8601"); + const plainDate = new Temporal.PlainDate(1970, 1, 1); + const duration = new Temporal.Duration(1, 2, 3, 4); + const newPlainDate = calendar.dateAdd(plainDate, duration); + expect(newPlainDate.year).toBe(1971); + expect(newPlainDate.month).toBe(3); + expect(newPlainDate.day).toBe(26); + }); +}); |