summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2022-05-06 20:57:55 +0200
committerLinus Groh <mail@linusgroh.de>2022-05-08 00:07:58 +0200
commitde0ea331cd427602ce5d6141368c985f850a0b84 (patch)
tree810df3a3ece41cf489aa9878d345c62334c62714 /Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
parent938e68d003ae91f3c95b53ae2a234534ff72f438 (diff)
downloadserenity-de0ea331cd427602ce5d6141368c985f850a0b84.zip
LibJS: Simplify BalanceISODate
This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/9544573
Diffstat (limited to 'Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp')
-rw-r--r--Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp101
1 files changed, 11 insertions, 90 deletions
diff --git a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
index f571dd5c11..1f0b7557e6 100644
--- a/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
+++ b/Userland/Libraries/LibJS/Runtime/Temporal/PlainDate.cpp
@@ -7,6 +7,7 @@
#include <LibJS/Runtime/AbstractOperations.h>
#include <LibJS/Runtime/Completion.h>
+#include <LibJS/Runtime/Date.h>
#include <LibJS/Runtime/GlobalObject.h>
#include <LibJS/Runtime/Temporal/Calendar.h>
#include <LibJS/Runtime/Temporal/Duration.h>
@@ -394,99 +395,19 @@ bool is_valid_iso_date(i32 year, u8 month, u8 day)
}
// 3.5.6 BalanceISODate ( year, month, day ), https://tc39.es/proposal-temporal/#sec-temporal-balanceisodate
-ISODate balance_iso_date(double year_, double month_, double day)
+ISODate balance_iso_date(double year, double month, double day)
{
- // 1. Assert: year, month, and day are integers.
-
- // 2. Let balancedYearMonth be ! BalanceISOYearMonth(year, month).
- auto balanced_year_month = balance_iso_year_month(year_, month_);
-
- // 3. Set month to balancedYearMonth.[[Month]].
- auto month = balanced_year_month.month;
-
- // 4. Set year to balancedYearMonth.[[Year]].
- auto year = balanced_year_month.year;
-
- // 5. NOTE: To deal with negative numbers of days whose absolute value is greater than the number of days in a year, the following section subtracts years and adds days until the number of days is greater than -366 or -365.
-
- i32 test_year;
-
- // 6. If month > 2, then
- if (month > 2) {
- // a. Let testYear be year.
- test_year = year;
- }
- // 7. Else,
- else {
- // a. Let testYear be year - 1.
- test_year = year - 1;
- }
-
- // 8. Repeat, while day < -1 × ! ISODaysInYear(testYear),
- while (day < -1 * iso_days_in_year(test_year)) {
- // a. Set day to day + !ISODaysInYear(testYear).
- day += iso_days_in_year(test_year);
-
- // b. Set year to year - 1.
- year--;
-
- // c. Set testYear to testYear - 1.
- test_year--;
- }
-
- // 9. NOTE: To deal with numbers of days greater than the number of days in a year, the following section adds years and subtracts days until the number of days is less than 366 or 365.
+ // 1. Let epochDays be MakeDay(𝔽(year), 𝔽(month - 1), 𝔽(day)).
+ auto epoch_days = make_day(year, month - 1, day);
- // 10. Set testYear to testYear + 1.
- test_year++;
+ // 2. Assert: epochDays is finite.
+ VERIFY(isfinite(epoch_days));
- // 11. Repeat, while day > ! ISODaysInYear(testYear),
- while (day > iso_days_in_year(test_year)) {
- // a. Set day to day - ! ISODaysInYear(testYear).
- day -= iso_days_in_year(test_year);
-
- // b. Set year to year + 1.
- year++;
-
- // c. Set testYear to testYear + 1.
- test_year++;
- }
-
- // 12. NOTE: To deal with negative numbers of days whose absolute value is greater than the number of days in the current month, the following section subtracts months and adds days until the number of days is greater than 0.
-
- // 13. Repeat, while day < 1,
- while (day < 1) {
- // a. Set balancedYearMonth to ! BalanceISOYearMonth(year, month - 1).
- balanced_year_month = balance_iso_year_month(year, month - 1);
-
- // b. Set year to balancedYearMonth.[[Year]].
- year = balanced_year_month.year;
-
- // c. Set month to balancedYearMonth.[[Month]].
- month = balanced_year_month.month;
-
- // d. Set day to day + ! ISODaysInMonth(year, month).
- day += iso_days_in_month(year, month);
- }
-
- // 14. NOTE: To deal with numbers of days greater than the number of days in the current month, the following section adds months and subtracts days until the number of days is less than the number of days in the month.
-
- // 15. Repeat, while day > ! ISODaysInMonth(year, month),
- while (day > iso_days_in_month(year, month)) {
- // a. Set day to day - ! ISODaysInMonth(year, month).
- day -= iso_days_in_month(year, month);
-
- // b. Set balancedYearMonth to ! BalanceISOYearMonth(year, month + 1).
- balanced_year_month = balance_iso_year_month(year, month + 1);
-
- // c. Set year to balancedYearMonth.[[Year]].
- year = balanced_year_month.year;
-
- // d. Set month to balancedYearMonth.[[Month]].
- month = balanced_year_month.month;
- }
+ // 3. Let ms be MakeDate(epochDays, +0𝔽).
+ auto ms = make_date(epoch_days, 0);
- // 16. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day }.
- return ISODate { .year = year, .month = static_cast<u8>(month), .day = static_cast<u8>(day) };
+ // 4. Return the Record { [[Year]]: ℝ(YearFromTime(ms)), [[Month]]: ℝ(MonthFromTime(ms)) + 1, [[Day]]: ℝ(DateFromTime(ms)) }.
+ return { .year = year_from_time(ms), .month = static_cast<u8>(month_from_time(ms) + 1), .day = date_from_time(ms) };
}
// 3.5.7 PadISOYear ( y ), https://tc39.es/proposal-temporal/#sec-temporal-padisoyear
@@ -554,7 +475,7 @@ ThrowCompletionOr<ISODate> add_iso_date(GlobalObject& global_object, i32 year, u
// 6. Let d be intermediate.[[Day]] + days.
auto d = intermediate_date.day + days;
- // 7. Let intermediate be ! BalanceISODate(intermediate.[[Year]], intermediate.[[Month]], d).
+ // 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).