PlainDate.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
  3. * Copyright (c) 2021-2022, Linus Groh <linusg@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <LibJS/Runtime/AbstractOperations.h>
  8. #include <LibJS/Runtime/Completion.h>
  9. #include <LibJS/Runtime/Date.h>
  10. #include <LibJS/Runtime/GlobalObject.h>
  11. #include <LibJS/Runtime/Temporal/Calendar.h>
  12. #include <LibJS/Runtime/Temporal/Duration.h>
  13. #include <LibJS/Runtime/Temporal/Instant.h>
  14. #include <LibJS/Runtime/Temporal/PlainDate.h>
  15. #include <LibJS/Runtime/Temporal/PlainDateConstructor.h>
  16. #include <LibJS/Runtime/Temporal/PlainDateTime.h>
  17. #include <LibJS/Runtime/Temporal/PlainYearMonth.h>
  18. #include <LibJS/Runtime/Temporal/TimeZone.h>
  19. #include <LibJS/Runtime/Temporal/ZonedDateTime.h>
  20. namespace JS::Temporal {
  21. // 3 Temporal.PlainDate Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaindate-objects
  22. PlainDate::PlainDate(i32 year, u8 month, u8 day, Object& calendar, Object& prototype)
  23. : Object(prototype)
  24. , m_iso_year(year)
  25. , m_iso_month(month)
  26. , m_iso_day(day)
  27. , m_calendar(calendar)
  28. {
  29. }
  30. void PlainDate::visit_edges(Visitor& visitor)
  31. {
  32. Base::visit_edges(visitor);
  33. visitor.visit(&m_calendar);
  34. }
  35. // 3.5.1 CreateTemporalDate ( isoYear, isoMonth, isoDay, calendar [ , newTarget ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtemporaldate
  36. ThrowCompletionOr<PlainDate*> create_temporal_date(GlobalObject& global_object, i32 iso_year, u8 iso_month, u8 iso_day, Object& calendar, FunctionObject const* new_target)
  37. {
  38. auto& vm = global_object.vm();
  39. // 1. Assert: isoYear is an integer.
  40. // 2. Assert: isoMonth is an integer.
  41. // 3. Assert: isoDay is an integer.
  42. // 4. Assert: Type(calendar) is Object.
  43. // 5. If IsValidISODate(isoYear, isoMonth, isoDay) is false, throw a RangeError exception.
  44. if (!is_valid_iso_date(iso_year, iso_month, iso_day))
  45. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  46. // 6. If ISODateTimeWithinLimits(isoYear, isoMonth, isoDay, 12, 0, 0, 0, 0, 0) is false, throw a RangeError exception.
  47. if (!iso_date_time_within_limits(global_object, iso_year, iso_month, iso_day, 12, 0, 0, 0, 0, 0))
  48. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  49. // 7. If newTarget is not present, set newTarget to %Temporal.PlainDate%.
  50. if (!new_target)
  51. new_target = global_object.temporal_plain_date_constructor();
  52. // 8. Let object be ? OrdinaryCreateFromConstructor(newTarget, "%Temporal.PlainDate.prototype%", « [[InitializedTemporalDate]], [[ISOYear]], [[ISOMonth]], [[ISODay]], [[Calendar]] »).
  53. // 9. Set object.[[ISOYear]] to isoYear.
  54. // 10. Set object.[[ISOMonth]] to isoMonth.
  55. // 11. Set object.[[ISODay]] to isoDay.
  56. // 12. Set object.[[Calendar]] to calendar.
  57. auto* object = TRY(ordinary_create_from_constructor<PlainDate>(global_object, *new_target, &GlobalObject::temporal_plain_date_prototype, iso_year, iso_month, iso_day, calendar));
  58. return object;
  59. }
  60. // 3.5.2 ToTemporalDate ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaldate
  61. ThrowCompletionOr<PlainDate*> to_temporal_date(GlobalObject& global_object, Value item, Object const* options)
  62. {
  63. auto& vm = global_object.vm();
  64. // 1. If options is not present, set options to undefined.
  65. // 2. Assert: Type(options) is Object or Undefined.
  66. // 3. If Type(item) is Object, then
  67. if (item.is_object()) {
  68. auto& item_object = item.as_object();
  69. // a. If item has an [[InitializedTemporalDate]] internal slot, then
  70. if (is<PlainDate>(item_object)) {
  71. // i. Return item.
  72. return static_cast<PlainDate*>(&item_object);
  73. }
  74. // b. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then
  75. if (is<ZonedDateTime>(item_object)) {
  76. auto& zoned_date_time = static_cast<ZonedDateTime&>(item_object);
  77. // i. Let instant be ! CreateTemporalInstant(item.[[Nanoseconds]]).
  78. auto* instant = create_temporal_instant(global_object, zoned_date_time.nanoseconds()).release_value();
  79. // ii. Let plainDateTime be ? BuiltinTimeZoneGetPlainDateTimeFor(item.[[TimeZone]], instant, item.[[Calendar]]).
  80. auto* plain_date_time = TRY(builtin_time_zone_get_plain_date_time_for(global_object, &zoned_date_time.time_zone(), *instant, zoned_date_time.calendar()));
  81. // iii. Return ! CreateTemporalDate(plainDateTime.[[ISOYear]], plainDateTime.[[ISOMonth]], plainDateTime.[[ISODay]], plainDateTime.[[Calendar]]).
  82. return create_temporal_date(global_object, plain_date_time->iso_year(), plain_date_time->iso_month(), plain_date_time->iso_day(), plain_date_time->calendar());
  83. }
  84. // c. If item has an [[InitializedTemporalDateTime]] internal slot, then
  85. if (is<PlainDateTime>(item_object)) {
  86. auto& date_time_item = static_cast<PlainDateTime&>(item_object);
  87. // i. Return ! CreateTemporalDate(item.[[ISOYear]], item.[[ISOMonth]], item.[[ISODay]], item.[[Calendar]]).
  88. return create_temporal_date(global_object, date_time_item.iso_year(), date_time_item.iso_month(), date_time_item.iso_day(), date_time_item.calendar());
  89. }
  90. // d. Let calendar be ? GetTemporalCalendarWithISODefault(item).
  91. auto* calendar = TRY(get_temporal_calendar_with_iso_default(global_object, item_object));
  92. // e. Let fieldNames be ? CalendarFields(calendar, « "day", "month", "monthCode", "year" »).
  93. auto field_names = TRY(calendar_fields(global_object, *calendar, { "day"sv, "month"sv, "monthCode"sv, "year"sv }));
  94. // f. Let fields be ? PrepareTemporalFields(item, fieldNames, «»).
  95. auto* fields = TRY(prepare_temporal_fields(global_object, item_object, field_names, {}));
  96. // g. Return ? CalendarDateFromFields(calendar, fields, options).
  97. return calendar_date_from_fields(global_object, *calendar, *fields, options);
  98. }
  99. // 4. Perform ? ToTemporalOverflow(options).
  100. (void)TRY(to_temporal_overflow(global_object, options));
  101. // 5. Let string be ? ToString(item).
  102. auto string = TRY(item.to_string(global_object));
  103. // 6. Let result be ? ParseTemporalDateString(string).
  104. auto result = TRY(parse_temporal_date_string(global_object, string));
  105. // 7. Assert: IsValidISODate(result.[[Year]], result.[[Month]], result.[[Day]]) is true.
  106. VERIFY(is_valid_iso_date(result.year, result.month, result.day));
  107. // 8. Let calendar be ? ToTemporalCalendarWithISODefault(result.[[Calendar]]).
  108. auto* calendar = TRY(to_temporal_calendar_with_iso_default(global_object, result.calendar.has_value() ? js_string(vm, *result.calendar) : js_undefined()));
  109. // 9. Return ? CreateTemporalDate(result.[[Year]], result.[[Month]], result.[[Day]], calendar).
  110. return create_temporal_date(global_object, result.year, result.month, result.day, *calendar);
  111. }
  112. // 3.5.3 DifferenceISODate ( y1, m1, d1, y2, m2, d2, largestUnit ), https://tc39.es/proposal-temporal/#sec-temporal-differenceisodate
  113. DateDurationRecord difference_iso_date(GlobalObject& global_object, i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2, StringView largest_unit)
  114. {
  115. VERIFY(largest_unit.is_one_of("year"sv, "month"sv, "week"sv, "day"sv));
  116. // 1. If largestUnit is "year" or "month", then
  117. if (largest_unit.is_one_of("year"sv, "month"sv)) {
  118. // a. Let sign be -(! CompareISODate(y1, m1, d1, y2, m2, d2)).
  119. auto sign = -compare_iso_date(year1, month1, day1, year2, month2, day2);
  120. // b. If sign is 0, return ! CreateDateDurationRecord(0, 0, 0, 0).
  121. if (sign == 0)
  122. return create_date_duration_record(0, 0, 0, 0);
  123. // c. Let start be the Record { [[Year]]: y1, [[Month]]: m1, [[Day]]: d1 }.
  124. auto start = ISODate { .year = year1, .month = month1, .day = day1 };
  125. // d. Let end be the Record { [[Year]]: y2, [[Month]]: m2, [[Day]]: d2 }.
  126. auto end = ISODate { .year = year2, .month = month2, .day = day2 };
  127. // e. Let years be end.[[Year]] - start.[[Year]].
  128. double years = end.year - start.year;
  129. // f. Let mid be ! AddISODate(y1, m1, d1, years, 0, 0, 0, "constrain").
  130. auto mid = MUST(add_iso_date(global_object, year1, month1, day1, years, 0, 0, 0, "constrain"sv));
  131. // g. Let midSign be -(! CompareISODate(mid.[[Year]], mid.[[Month]], mid.[[Day]], y2, m2, d2)).
  132. auto mid_sign = -compare_iso_date(mid.year, mid.month, mid.day, year2, month2, day2);
  133. // h. If midSign is 0, then
  134. if (mid_sign == 0) {
  135. // i. If largestUnit is "year", return ! CreateDateDurationRecord(years, 0, 0, 0).
  136. if (largest_unit == "year"sv)
  137. return create_date_duration_record(years, 0, 0, 0);
  138. // ii. Return ! CreateDateDurationRecord(0, years × 12, 0, 0).
  139. return create_date_duration_record(0, years * 12, 0, 0);
  140. }
  141. // i. Let months be end.[[Month]] - start.[[Month]].
  142. double months = end.month - start.month;
  143. // j. If midSign is not equal to sign, then
  144. if (mid_sign != sign) {
  145. // i. Set years to years - sign.
  146. years -= sign;
  147. // ii. Set months to months + sign × 12.
  148. months += sign * 12;
  149. }
  150. // k. Set mid to ! AddISODate(y1, m1, d1, years, months, 0, 0, "constrain").
  151. mid = MUST(add_iso_date(global_object, year1, month1, day1, years, months, 0, 0, "constrain"sv));
  152. // l. Set midSign to -(! CompareISODate(mid.[[Year]], mid.[[Month]], mid.[[Day]], y2, m2, d2)).
  153. mid_sign = -compare_iso_date(mid.year, mid.month, mid.day, year2, month2, day2);
  154. // m. If midSign is 0, then
  155. if (mid_sign == 0) {
  156. // i. If largestUnit is "year", return ! CreateDateDurationRecord(years, months, 0, 0).
  157. if (largest_unit == "year"sv)
  158. return create_date_duration_record(years, months, 0, 0);
  159. // ii. Return ! CreateDateDurationRecord(0, months + years × 12, 0, 0).
  160. return create_date_duration_record(0, months + years * 12, 0, 0);
  161. }
  162. // n. If midSign is not equal to sign, then
  163. if (mid_sign != sign) {
  164. // i. Set months to months - sign.
  165. months -= sign;
  166. // ii. If months is equal to -sign, then
  167. if (months == -sign) {
  168. // 1. Set years to years - sign.
  169. years -= sign;
  170. // 2. Set months to 11 × sign.
  171. months = 11 * sign;
  172. }
  173. // iii. Set mid to ! AddISODate(y1, m1, d1, years, months, 0, 0, "constrain").
  174. mid = MUST(add_iso_date(global_object, year1, month1, day1, years, months, 0, 0, "constrain"sv));
  175. }
  176. // o. Let days be 0.
  177. double days = 0;
  178. // p. If mid.[[Month]] = end.[[Month]], then
  179. if (mid.month == end.month) {
  180. // i. Assert: mid.[[Year]] = end.[[Year]].
  181. VERIFY(mid.year == end.year);
  182. // ii. Set days to end.[[Day]] - mid.[[Day]].
  183. days = end.day - mid.day;
  184. }
  185. // q. Else if sign < 0, set days to -mid.[[Day]] - (! ISODaysInMonth(end.[[Year]], end.[[Month]]) - end.[[Day]]).
  186. else if (sign < 0) {
  187. days = -mid.day - (iso_days_in_month(end.year, end.month) - end.day);
  188. }
  189. // r. Else, set days to end.[[Day]] + (! ISODaysInMonth(mid.[[Year]], mid.[[Month]]) - mid.[[Day]]).
  190. else {
  191. days = end.day + (iso_days_in_month(mid.year, mid.month) - mid.day);
  192. }
  193. // s. If largestUnit is "month", then
  194. if (largest_unit == "month"sv) {
  195. // i. Set months to months + years × 12.
  196. months += years * 12;
  197. // ii. Set years to 0.
  198. years = 0;
  199. }
  200. // t. Return ! CreateDateDurationRecord(years, months, 0, days).
  201. return create_date_duration_record(years, months, 0, days);
  202. }
  203. // 2. If largestUnit is "day" or "week", then
  204. else {
  205. // a. Let epochDays1 be MakeDay(𝔽(y1), 𝔽(m1 - 1), 𝔽(d1)).
  206. auto epoch_days_1 = make_day(year1, month1 - 1, day1);
  207. // b. Assert: epochDays1 is finite.
  208. VERIFY(isfinite(epoch_days_1));
  209. // c. Let epochDays2 be MakeDay(𝔽(y2), 𝔽(m2 - 1), 𝔽(d2)).
  210. auto epoch_days_2 = make_day(year2, month2 - 1, day2);
  211. // d. Assert: epochDays2 is finite.
  212. VERIFY(isfinite(epoch_days_2));
  213. // e. Let days be ℝ(epochDays2) - ℝ(epochDays1).
  214. auto days = epoch_days_2 - epoch_days_1;
  215. // f. Let weeks be 0.
  216. double weeks = 0;
  217. // g. If largestUnit is "week", then
  218. if (largest_unit == "week"sv) {
  219. // i. Set weeks to RoundTowardsZero(days / 7).
  220. weeks = trunc(days / 7);
  221. // ii. Set days to remainder(days, 7).
  222. days = fmod(days, 7);
  223. }
  224. // h. Return ! CreateDateDurationRecord(0, 0, weeks, days).
  225. return create_date_duration_record(0, 0, weeks, days);
  226. }
  227. VERIFY_NOT_REACHED();
  228. }
  229. // 3.5.4 RegulateISODate ( year, month, day, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-regulateisodate
  230. ThrowCompletionOr<ISODate> regulate_iso_date(GlobalObject& global_object, double year, double month, double day, StringView overflow)
  231. {
  232. auto& vm = global_object.vm();
  233. // 1. Assert: year, month, and day are integers.
  234. VERIFY(year == trunc(year) && month == trunc(month) && day == trunc(day));
  235. // 2. Assert: overflow is either "constrain" or "reject".
  236. // NOTE: Asserted by the VERIFY_NOT_REACHED at the end
  237. // 3. If overflow is "reject", then
  238. if (overflow == "reject"sv) {
  239. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat these doubles as normal integers from this point onwards.
  240. // This does not change the exposed behavior as the call to IsValidISODate will immediately check that these values are valid ISO
  241. // values (for years: -273975 - 273975, for months: 1 - 12, for days: 1 - 31) all of which are subsets of this check.
  242. if (!AK::is_within_range<i32>(year) || !AK::is_within_range<u8>(month) || !AK::is_within_range<u8>(day))
  243. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  244. auto y = static_cast<i32>(year);
  245. auto m = static_cast<u8>(month);
  246. auto d = static_cast<u8>(day);
  247. // a. If IsValidISODate(year, month, day) is false, throw a RangeError exception.
  248. if (!is_valid_iso_date(y, m, d))
  249. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  250. // b. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  251. return ISODate { .year = y, .month = m, .day = d };
  252. }
  253. // 4. If overflow is "constrain", then
  254. else if (overflow == "constrain"sv) {
  255. // IMPLEMENTATION DEFINED: This is an optimization that allows us to treat this double as normal integer from this point onwards. This
  256. // does not change the exposed behavior as the parent's call to CreateTemporalDate will immediately check that this value is a valid
  257. // ISO value for years: -273975 - 273975, which is a subset of this check.
  258. if (!AK::is_within_range<i32>(year))
  259. return vm.throw_completion<RangeError>(global_object, ErrorType::TemporalInvalidPlainDate);
  260. auto y = static_cast<i32>(year);
  261. // a. Set month to the result of clamping month between 1 and 12.
  262. month = clamp(month, 1, 12);
  263. // b. Let daysInMonth be ! ISODaysInMonth(year, month).
  264. auto days_in_month = iso_days_in_month(y, (u8)month);
  265. // c. Set day to the result of clamping day between 1 and daysInMonth.
  266. day = clamp(day, 1, days_in_month);
  267. // d. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
  268. return ISODate { .year = y, .month = static_cast<u8>(month), .day = static_cast<u8>(day) };
  269. }
  270. VERIFY_NOT_REACHED();
  271. }
  272. // 3.5.5 IsValidISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-isvalidisodate
  273. bool is_valid_iso_date(i32 year, u8 month, u8 day)
  274. {
  275. // 1. If month < 1 or month > 12, then
  276. if (month < 1 || month > 12) {
  277. // a. Return false.
  278. return false;
  279. }
  280. // 2. Let daysInMonth be ! ISODaysInMonth(year, month).
  281. auto days_in_month = iso_days_in_month(year, month);
  282. // 3. If day < 1 or day > daysInMonth, then
  283. if (day < 1 || day > days_in_month) {
  284. // a. Return false.
  285. return false;
  286. }
  287. // 4. Return true.
  288. return true;
  289. }
  290. // 3.5.6 BalanceISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisodate
  291. ISODate balance_iso_date(double year, double month, double day)
  292. {
  293. // 1. Let epochDays be MakeDay(𝔽(year), 𝔽(month - 1), 𝔽(day)).
  294. auto epoch_days = make_day(year, month - 1, day);
  295. // 2. Assert: epochDays is finite.
  296. VERIFY(isfinite(epoch_days));
  297. // 3. Let ms be MakeDate(epochDays, +0𝔽).
  298. auto ms = make_date(epoch_days, 0);
  299. // 4. Return the Record { [[Year]]: ℝ(YearFromTime(ms)), [[Month]]: ℝ(MonthFromTime(ms)) + 1, [[Day]]: ℝ(DateFromTime(ms)) }.
  300. return { .year = year_from_time(ms), .month = static_cast<u8>(month_from_time(ms) + 1), .day = date_from_time(ms) };
  301. }
  302. // 3.5.7 PadISOYear ( y ), https://tc39.es/proposal-temporal/#sec-temporal-padisoyear
  303. String pad_iso_year(i32 y)
  304. {
  305. // 1. Assert: y is an integer.
  306. // 2. If y ≥ 0 and y ≤ 9999, then
  307. if (y >= 0 && y <= 9999) {
  308. // a. Return ToZeroPaddedDecimalString(y, 4).
  309. return String::formatted("{:04}", y);
  310. }
  311. // 3. If y > 0, let yearSign be "+"; otherwise, let yearSign be "-".
  312. auto year_sign = y > 0 ? '+' : '-';
  313. // 4. Let year be ToZeroPaddedDecimalString(abs(y), 6).
  314. // 5. Return the string-concatenation of yearSign and year.
  315. return String::formatted("{}{:06}", year_sign, abs(y));
  316. }
  317. // 3.5.8 TemporalDateToString ( temporalDate, showCalendar ), https://tc39.es/proposal-temporal/#sec-temporal-temporaldatetostring
  318. ThrowCompletionOr<String> temporal_date_to_string(GlobalObject& global_object, PlainDate& temporal_date, StringView show_calendar)
  319. {
  320. // 1. Assert: Type(temporalDate) is Object.
  321. // 2. Assert: temporalDate has an [[InitializedTemporalDate]] internal slot.
  322. // 3. Let year be ! PadISOYear(temporalDate.[[ISOYear]]).
  323. auto year = pad_iso_year(temporal_date.iso_year());
  324. // 4. Let month be ToZeroPaddedDecimalString(monthDay.[[ISOMonth]], 2).
  325. auto month = String::formatted("{:02}", temporal_date.iso_month());
  326. // 5. Let day be ToZeroPaddedDecimalString(monthDay.[[ISODay]], 2).
  327. auto day = String::formatted("{:02}", temporal_date.iso_day());
  328. // 6. Let calendarID be ? ToString(temporalDate.[[Calendar]]).
  329. auto calendar_id = TRY(Value(&temporal_date.calendar()).to_string(global_object));
  330. // 7. Let calendar be ! FormatCalendarAnnotation(calendarID, showCalendar).
  331. auto calendar = format_calendar_annotation(calendar_id, show_calendar);
  332. // 8. Return the string-concatenation of year, the code unit 0x002D (HYPHEN-MINUS), month, the code unit 0x002D (HYPHEN-MINUS), day, and calendar.
  333. return String::formatted("{}-{}-{}{}", year, month, day, calendar);
  334. }
  335. // 3.5.9 AddISODate ( year, month, day, years, months, weeks, days, overflow ), https://tc39.es/proposal-temporal/#sec-temporal-addisodate
  336. ThrowCompletionOr<ISODate> add_iso_date(GlobalObject& global_object, i32 year, u8 month, u8 day, double years, double months, double weeks, double days, StringView overflow)
  337. {
  338. // 1. Assert: year, month, day, years, months, weeks, and days are integers.
  339. VERIFY(years == trunc(years) && months == trunc(months) && weeks == trunc(weeks) && days == trunc(days));
  340. // 2. Assert: overflow is either "constrain" or "reject".
  341. VERIFY(overflow == "constrain"sv || overflow == "reject"sv);
  342. // 3. Let intermediate be ! BalanceISOYearMonth(year + years, month + months).
  343. auto intermediate_year_month = balance_iso_year_month(year + years, month + months);
  344. // 4. Let intermediate be ? RegulateISODate(intermediate.[[Year]], intermediate.[[Month]], day, overflow).
  345. auto intermediate_date = TRY(regulate_iso_date(global_object, intermediate_year_month.year, intermediate_year_month.month, day, overflow));
  346. // 5. Set days to days + 7 × weeks.
  347. days += 7 * weeks;
  348. // 6. Let d be intermediate.[[Day]] + days.
  349. auto d = intermediate_date.day + days;
  350. // 7. Let intermediate be BalanceISODate(intermediate.[[Year]], intermediate.[[Month]], d).
  351. auto intermediate = balance_iso_date(intermediate_date.year, intermediate_date.month, d);
  352. // 8. Return ? RegulateISODate(intermediate.[[Year]], intermediate.[[Month]], intermediate.[[Day]], overflow).
  353. return regulate_iso_date(global_object, intermediate.year, intermediate.month, intermediate.day, overflow);
  354. }
  355. // 3.5.10 CompareISODate ( y1, m1, d1, y2, m2, d2 ), https://tc39.es/proposal-temporal/#sec-temporal-compareisodate
  356. i8 compare_iso_date(i32 year1, u8 month1, u8 day1, i32 year2, u8 month2, u8 day2)
  357. {
  358. // 1. Assert: y1, m1, d1, y2, m2, and d2 are integers.
  359. // 2. If y1 > y2, return 1.
  360. if (year1 > year2)
  361. return 1;
  362. // 3. If y1 < y2, return -1.
  363. if (year1 < year2)
  364. return -1;
  365. // 4. If m1 > m2, return 1.
  366. if (month1 > month2)
  367. return 1;
  368. // 5. If m1 < m2, return -1.
  369. if (month1 < month2)
  370. return -1;
  371. // 6. If d1 > d2, return 1.
  372. if (day1 > day2)
  373. return 1;
  374. // 7. If d1 < d2, return -1.
  375. if (day1 < day2)
  376. return -1;
  377. // 8. Return 0.
  378. return 0;
  379. }
  380. }