1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 15:57:45 +00:00

LibJS: Clarify mathematical types in Temporal AOs and functions

This is an editorial change in the Temporal spec.

See: e480d40
This commit is contained in:
Linus Groh 2021-10-26 23:10:11 +02:00
parent 30c39e0e41
commit 09d1db5afd
3 changed files with 26 additions and 19 deletions

View file

@ -618,6 +618,9 @@ String format_seconds_string_part(u8 second, u16 millisecond, u16 microsecond, u
// 13.29 ConstrainToRange ( x, minimum, maximum ), https://tc39.es/proposal-temporal/#sec-temporal-constraintorange
double constrain_to_range(double x, double minimum, double maximum)
{
// 1. Assert: x, minimum and maximum are mathematical values.
// 2. Return min(max(x, minimum), maximum).
return min(max(x, minimum), maximum);
}

View file

@ -611,7 +611,7 @@ u8 to_iso_day_of_week(i32 year, u8 month, u8 day)
// 3. Assert: day is an integer.
// 4. Let date be the date given by year, month, and day.
// 5. Return date's day of the week according to ISO-8601.
// 5. Return date's day of the week according to ISO-8601 as an integer.
// NOTE: Implemented based on https://cs.uwaterloo.ca/~alopez-o/math-faq/node73.html
auto normalized_month = month + (month < 3 ? 10 : -2);
auto normalized_year = year - (month < 3 ? 1 : 0);
@ -631,7 +631,7 @@ u16 to_iso_day_of_year(i32 year, u8 month, u8 day)
// 3. Assert: day is an integer.
// 4. Let date be the date given by year, month, and day.
// 5. Return date's ordinal date in the year according to ISO-8601.
// 5. Return date's ordinal date in the year according to ISO-8601 as an integer.
u16 days = day;
for (u8 i = month - 1; i > 0; --i)
days += iso_days_in_month(year, i);
@ -646,7 +646,7 @@ u8 to_iso_week_of_year(i32 year, u8 month, u8 day)
// 3. Assert: day is an integer.
// 4. Let date be the date given by year, month, and day.
// 5. Return date's week number according to ISO-8601.
// 5. Return date's week number according to ISO-8601 as an integer.
auto day_of_year = to_iso_day_of_year(year, month, day);
auto day_of_week = to_iso_day_of_week(year, month, day);
auto week = (day_of_year - day_of_week + 10) / 7;

View file

@ -113,42 +113,44 @@ ThrowCompletionOr<TimeZone*> create_temporal_time_zone(GlobalObject& global_obje
// 11.6.3 GetISOPartsFromEpoch ( epochNanoseconds ), https://tc39.es/proposal-temporal/#sec-temporal-getisopartsfromepoch
ISODateTime get_iso_parts_from_epoch(BigInt const& epoch_nanoseconds)
{
// 1. Let remainderNs be remainder(epochNanoseconds, 10^6).
// 1. Assert: epochNanoseconds is an integer.
// 2. Let remainderNs be remainder(epochNanoseconds, 10^6).
auto remainder_ns_bigint = epoch_nanoseconds.big_integer().divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).remainder;
auto remainder_ns = remainder_ns_bigint.to_base(10).to_int<i64>().value();
// 2. Let epochMilliseconds be (epochNanoseconds remainderNs) / 10^6.
// 3. Let epochMilliseconds be (epochNanoseconds remainderNs) / 10^6.
auto epoch_milliseconds_bigint = epoch_nanoseconds.big_integer().minus(remainder_ns_bigint).divided_by(Crypto::UnsignedBigInteger { 1'000'000 }).quotient;
auto epoch_milliseconds = (double)epoch_milliseconds_bigint.to_base(10).to_int<i64>().value();
// 3. Let year be ! YearFromTime(epochMilliseconds).
// 4. Let year be ! YearFromTime(epochMilliseconds).
auto year = year_from_time(epoch_milliseconds);
// 4. Let month be ! MonthFromTime(epochMilliseconds) + 1.
// 5. Let month be ! MonthFromTime(epochMilliseconds) + 1.
auto month = static_cast<u8>(month_from_time(epoch_milliseconds) + 1);
// 5. Let day be ! DateFromTime(epochMilliseconds).
// 6. Let day be ! DateFromTime(epochMilliseconds).
auto day = date_from_time(epoch_milliseconds);
// 6. Let hour be ! HourFromTime(epochMilliseconds).
// 7. Let hour be ! HourFromTime(epochMilliseconds).
auto hour = hour_from_time(epoch_milliseconds);
// 7. Let minute be ! MinFromTime(epochMilliseconds).
// 8. Let minute be ! MinFromTime(epochMilliseconds).
auto minute = min_from_time(epoch_milliseconds);
// 8. Let second be ! SecFromTime(epochMilliseconds).
// 9. Let second be ! SecFromTime(epochMilliseconds).
auto second = sec_from_time(epoch_milliseconds);
// 9. Let millisecond be ! msFromTime(epochMilliseconds).
// 10. Let millisecond be ! msFromTime(epochMilliseconds).
auto millisecond = ms_from_time(epoch_milliseconds);
// 10. Let microsecond be floor(remainderNs / 1000) modulo 1000.
// 11. Let microsecond be floor(remainderNs / 1000) modulo 1000.
auto microsecond = static_cast<u16>((remainder_ns / 1000) % 1000);
// 11. Let nanosecond be remainderNs modulo 1000.
// 12. Let nanosecond be remainderNs modulo 1000.
auto nanosecond = static_cast<u16>(remainder_ns % 1000);
// 12. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
// 13. Return the Record { [[Year]]: year, [[Month]]: month, [[Day]]: day, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }.
return { .year = year, .month = month, .day = day, .hour = hour, .minute = minute, .second = second, .millisecond = millisecond, .microsecond = microsecond, .nanosecond = nanosecond };
}
@ -394,16 +396,18 @@ ThrowCompletionOr<String> builtin_time_zone_get_offset_string_for(GlobalObject&
// 11.6.13 BuiltinTimeZoneGetPlainDateTimeFor ( timeZone, instant, calendar ), https://tc39.es/proposal-temporal/#sec-temporal-builtintimezonegetplaindatetimefor
ThrowCompletionOr<PlainDateTime*> builtin_time_zone_get_plain_date_time_for(GlobalObject& global_object, Value time_zone, Instant& instant, Object& calendar)
{
// 1. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
// 1. Assert: instant has an [[InitializedTemporalInstant]] internal slot.
// 2. Let offsetNanoseconds be ? GetOffsetNanosecondsFor(timeZone, instant).
auto offset_nanoseconds = TRY(get_offset_nanoseconds_for(global_object, time_zone, instant));
// 2. Let result be ! GetISOPartsFromEpoch(instant.[[Nanoseconds]]).
// 3. Let result be ! GetISOPartsFromEpoch(instant.[[Nanoseconds]]).
auto result = get_iso_parts_from_epoch(instant.nanoseconds());
// 3. 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);
// 4. Return ? CreateTemporalDateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], calendar).
// 5. Return ? CreateTemporalDateTime(result.[[Year]], result.[[Month]], result.[[Day]], result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], calendar).
return create_temporal_date_time(global_object, result.year, result.month, result.day, result.hour, result.minute, result.second, result.millisecond, result.microsecond, result.nanosecond, calendar);
}