mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-27 01:50:24 +00:00
LibJS: Simplify BalanceISODate
This is an editorial change in the Temporal spec. See: https://github.com/tc39/proposal-temporal/commit/9544573
This commit is contained in:
parent
938e68d003
commit
de0ea331cd
Notes:
sideshowbarker
2024-07-17 11:10:13 +09:00
Author: https://github.com/linusg Commit: https://github.com/SerenityOS/serenity/commit/de0ea331cd Pull-request: https://github.com/SerenityOS/serenity/pull/13944 Reviewed-by: https://github.com/IdanHo
3 changed files with 16 additions and 95 deletions
|
@ -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.
|
||||
// 1. Let epochDays be MakeDay(𝔽(year), 𝔽(month - 1), 𝔽(day)).
|
||||
auto epoch_days = make_day(year, month - 1, day);
|
||||
|
||||
// 2. Let balancedYearMonth be ! BalanceISOYearMonth(year, month).
|
||||
auto balanced_year_month = balance_iso_year_month(year_, month_);
|
||||
// 2. Assert: epochDays is finite.
|
||||
VERIFY(isfinite(epoch_days));
|
||||
|
||||
// 3. Set month to balancedYearMonth.[[Month]].
|
||||
auto month = balanced_year_month.month;
|
||||
// 3. Let ms be MakeDate(epochDays, +0𝔽).
|
||||
auto ms = make_date(epoch_days, 0);
|
||||
|
||||
// 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.
|
||||
|
||||
// 10. Set testYear to testYear + 1.
|
||||
test_year++;
|
||||
|
||||
// 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;
|
||||
}
|
||||
|
||||
// 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).
|
||||
|
|
|
@ -212,7 +212,7 @@ ISODateTime balance_iso_date_time(i32 year, u8 month, u8 day, u8 hour, u8 minute
|
|||
// 2. Let balancedTime be ! BalanceTime(hour, minute, second, millisecond, microsecond, nanosecond).
|
||||
auto balanced_time = balance_time(hour, minute, second, millisecond, microsecond, nanosecond);
|
||||
|
||||
// 3. Let balancedDate be ! BalanceISODate(year, month, day + balancedTime.[[Days]]).
|
||||
// 3. Let balancedDate be BalanceISODate(year, month, day + balancedTime.[[Days]]).
|
||||
auto balanced_date = balance_iso_date(year, month, day + balanced_time.days);
|
||||
|
||||
// 4. Return the Record { [[Year]]: balancedDate.[[Year]], [[Month]]: balancedDate.[[Month]], [[Day]]: balancedDate.[[Day]], [[Hour]]: balancedTime.[[Hour]], [[Minute]]: balancedTime.[[Minute]], [[Second]]: balancedTime.[[Second]], [[Millisecond]]: balancedTime.[[Millisecond]], [[Microsecond]]: balancedTime.[[Microsecond]], [[Nanosecond]]: balancedTime.[[Nanosecond]] }.
|
||||
|
@ -335,7 +335,7 @@ ISODateTime round_iso_date_time(i32 year, u8 month, u8 day, u8 hour, u8 minute,
|
|||
// 3. Let roundedTime be ! RoundTime(hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, roundingMode, dayLength).
|
||||
auto rounded_time = round_time(hour, minute, second, millisecond, microsecond, nanosecond, increment, unit, rounding_mode, day_length);
|
||||
|
||||
// 4. Let balanceResult be ! BalanceISODate(year, month, day + roundedTime.[[Days]]).
|
||||
// 4. Let balanceResult be BalanceISODate(year, month, day + roundedTime.[[Days]]).
|
||||
auto balance_result = balance_iso_date(year, month, day + rounded_time.days);
|
||||
|
||||
// 5. Return the Record { [[Year]]: balanceResult.[[Year]], [[Month]]: balanceResult.[[Month]], [[Day]]: balanceResult.[[Day]], [[Hour]]: roundedTime.[[Hour]], [[Minute]]: roundedTime.[[Minute]], [[Second]]: roundedTime.[[Second]], [[Millisecond]]: roundedTime.[[Millisecond]], [[Microsecond]]: roundedTime.[[Microsecond]], [[Nanosecond]]: roundedTime.[[Nanosecond]] }.
|
||||
|
@ -357,12 +357,12 @@ ThrowCompletionOr<DurationRecord> difference_iso_date_time(GlobalObject& global_
|
|||
// 5. Let dateSign be ! CompareISODate(y2, mon2, d2, y1, mon1, d1).
|
||||
auto date_sign = compare_iso_date(year2, month2, day2, year1, month1, day1);
|
||||
|
||||
// 6. Let adjustedDate be ! BalanceISODate(y1, mon1, d1 + timeDifference.[[Days]]).
|
||||
// 6. Let adjustedDate be BalanceISODate(y1, mon1, d1 + timeDifference.[[Days]]).
|
||||
auto adjusted_date = balance_iso_date(year1, month1, day1 + time_difference.days);
|
||||
|
||||
// 7. If timeSign is -dateSign, then
|
||||
if (time_sign == -date_sign) {
|
||||
// a. Set adjustedDate to ! BalanceISODate(adjustedDate.[[Year]], adjustedDate.[[Month]], adjustedDate.[[Day]] - timeSign).
|
||||
// a. Set adjustedDate to BalanceISODate(adjustedDate.[[Year]], adjustedDate.[[Month]], adjustedDate.[[Day]] - timeSign).
|
||||
adjusted_date = balance_iso_date(adjusted_date.year, adjusted_date.month, adjusted_date.day - time_sign);
|
||||
|
||||
// b. Set timeDifference to ? BalanceDuration(-timeSign, timeDifference.[[Hours]], timeDifference.[[Minutes]], timeDifference.[[Seconds]], timeDifference.[[Milliseconds]], timeDifference.[[Microseconds]], timeDifference.[[Nanoseconds]], largestUnit).
|
||||
|
|
|
@ -520,7 +520,7 @@ ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(Glob
|
|||
// 3. Let result be ! GetISOPartsFromEpoch(ℝ(instant.[[Nanoseconds]])).
|
||||
auto result = get_iso_parts_from_epoch(global_object, instant.nanoseconds().big_integer());
|
||||
|
||||
// 4. Set result to ! BalanceISODateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]] + offsetNanoseconds).
|
||||
// 4. Set result to BalanceISODateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]] + offsetNanoseconds).
|
||||
result = balance_iso_date_time(result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond + offset_nanoseconds);
|
||||
|
||||
// 5. Return ? CreateTemporalDateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], calendar).
|
||||
|
|
Loading…
Reference in a new issue