/* * Copyright (c) 2021, Idan Horowitz * Copyright (c) 2021-2023, Linus Groh * Copyright (c) 2024, Tim Flynn * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include namespace JS::Temporal { GC_DEFINE_ALLOCATOR(PlainTime); // 4 Temporal.PlainTime Objects, https://tc39.es/proposal-temporal/#sec-temporal-plaintime-objects PlainTime::PlainTime(Time const& time, Object& prototype) : Object(ConstructWithPrototypeTag::Tag, prototype) , m_time(time) { } // FIXME: We should add a generic floor() method to our BigInt classes. But for now, since we know we are only dividing // by powers of 10, we can implement a very situationally specific method to compute the floor of a division. static TimeDuration big_floor(TimeDuration const& numerator, Crypto::UnsignedBigInteger const& denominator) { auto result = numerator.divided_by(denominator); if (result.remainder.is_zero()) return result.quotient; if (!result.quotient.is_negative() && result.remainder.is_positive()) return result.quotient; return result.quotient.minus(TimeDuration { 1 }); } // 4.5.2 CreateTimeRecord ( hour, minute, second, millisecond, microsecond, nanosecond [ , deltaDays ] ), https://tc39.es/proposal-temporal/#sec-temporal-createtimerecord Time create_time_record(double hour, double minute, double second, double millisecond, double microsecond, double nanosecond, double delta_days) { // 1. If deltaDays is not present, set deltaDays to 0. // 2. Assert: IsValidTime(hour, minute, second, millisecond, microsecond, nanosecond). VERIFY(is_valid_time(hour, minute, second, millisecond, microsecond, nanosecond)); // 3. Return Time Record { [[Days]]: deltaDays, [[Hour]]: hour, [[Minute]]: minute, [[Second]]: second, [[Millisecond]]: millisecond, [[Microsecond]]: microsecond, [[Nanosecond]]: nanosecond }. return { .days = delta_days, .hour = static_cast(hour), .minute = static_cast(minute), .second = static_cast(second), .millisecond = static_cast(millisecond), .microsecond = static_cast(microsecond), .nanosecond = static_cast(nanosecond), }; } // 4.5.3 MidnightTimeRecord ( ), https://tc39.es/proposal-temporal/#sec-temporal-midnighttimerecord Time midnight_time_record() { // 1. Return Time Record { [[Days]]: 0, [[Hour]]: 0, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }. return { .days = 0, .hour = 0, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 }; } // 4.5.4 NoonTimeRecord ( ), https://tc39.es/proposal-temporal/#sec-temporal-noontimerecord Time noon_time_record() { // 1. Return Time Record { [[Days]]: 0, [[Hour]]: 12, [[Minute]]: 0, [[Second]]: 0, [[Millisecond]]: 0, [[Microsecond]]: 0, [[Nanosecond]]: 0 }. return { .days = 0, .hour = 12, .minute = 0, .second = 0, .millisecond = 0, .microsecond = 0, .nanosecond = 0 }; } // 4.5.5 DifferenceTime ( time1, time2 ), https://tc39.es/proposal-temporal/#sec-temporal-differencetime TimeDuration difference_time(Time const& time1, Time const& time2) { // 1. Let hours be time2.[[Hour]] - time1.[[Hour]]. auto hours = static_cast(time2.hour) - static_cast(time1.hour); // 2. Let minutes be time2.[[Minute]] - time1.[[Minute]]. auto minutes = static_cast(time2.minute) - static_cast(time1.minute); // 3. Let seconds be time2.[[Second]] - time1.[[Second]]. auto seconds = static_cast(time2.second) - static_cast(time1.second); // 4. Let milliseconds be time2.[[Millisecond]] - time1.[[Millisecond]]. auto milliseconds = static_cast(time2.millisecond) - static_cast(time1.millisecond); // 5. Let microseconds be time2.[[Microsecond]] - time1.[[Microsecond]]. auto microseconds = static_cast(time2.microsecond) - static_cast(time1.microsecond); // 6. Let nanoseconds be time2.[[Nanosecond]] - time1.[[Nanosecond]]. auto nanoseconds = static_cast(time2.nanosecond) - static_cast(time1.nanosecond); // 7. Let timeDuration be TimeDurationFromComponents(hours, minutes, seconds, milliseconds, microseconds, nanoseconds). auto time_duration = time_duration_from_components(hours, minutes, seconds, milliseconds, microseconds, nanoseconds); // 8. Assert: abs(timeDuration) < nsPerDay. VERIFY(time_duration.unsigned_value() < NANOSECONDS_PER_DAY); // 9. Return timeDuration. return time_duration; } // 4.5.6 ToTemporalTime ( item [ , options ] ), https://tc39.es/proposal-temporal/#sec-temporal-totemporaltime ThrowCompletionOr> to_temporal_time(VM& vm, Value item, Value options) { // 1. If options is not present, set options to undefined. Time time; // 2. If item is an Object, then if (item.is_object()) { auto const& object = item.as_object(); // a. If item has an [[InitializedTemporalTime]] internal slot, then if (is(object)) { auto const& plain_time = static_cast(object); // i. Let resolvedOptions be ? GetOptionsObject(options). auto resolved_options = TRY(get_options_object(vm, options)); // ii. Perform ? GetTemporalOverflowOption(resolvedOptions). TRY(get_temporal_overflow_option(vm, resolved_options)); // iii. Return ! CreateTemporalTime(item.[[Time]]). return MUST(create_temporal_time(vm, plain_time.time())); } // b. If item has an [[InitializedTemporalDateTime]] internal slot, then if (is(object)) { auto const& plain_date_time = static_cast(object); // i. Let resolvedOptions be ? GetOptionsObject(options). auto resolved_options = TRY(get_options_object(vm, options)); // ii. Perform ? GetTemporalOverflowOption(resolvedOptions). TRY(get_temporal_overflow_option(vm, resolved_options)); // iii. Return ! CreateTemporalTime(item.[[ISODateTime]].[[Time]]). return MUST(create_temporal_time(vm, plain_date_time.iso_date_time().time)); } // FIXME: c. If item has an [[InitializedTemporalZonedDateTime]] internal slot, then // FIXME: i. Let isoDateTime be GetISODateTimeFor(item.[[TimeZone]], item.[[EpochNanoseconds]]). // FIXME: ii. Let resolvedOptions be ? GetOptionsObject(options). // FIXME: iii. Perform ? GetTemporalOverflowOption(resolvedOptions). // FIXME: iv. Return ! CreateTemporalTime(isoDateTime.[[Time]]). // d. Let result be ? ToTemporalTimeRecord(item). auto result = TRY(to_temporal_time_record(vm, object)); // e. Let resolvedOptions be ? GetOptionsObject(options). auto resolved_options = TRY(get_options_object(vm, options)); // f. Let overflow be ? GetTemporalOverflowOption(resolvedOptions). auto overflow = TRY(get_temporal_overflow_option(vm, resolved_options)); // g. Set result to ? RegulateTime(result.[[Hour]], result.[[Minute]], result.[[Second]], result.[[Millisecond]], result.[[Microsecond]], result.[[Nanosecond]], overflow). time = TRY(regulate_time(vm, *result.hour, *result.minute, *result.second, *result.millisecond, *result.microsecond, *result.nanosecond, overflow)); } // 3. Else, else { // a. If item is not a String, throw a TypeError exception. if (!item.is_string()) return vm.throw_completion(ErrorType::TemporalInvalidPlainTime); // b. Let parseResult be ? ParseISODateTime(item, « TemporalTimeString »). auto parse_result = TRY(parse_iso_date_time(vm, item.as_string().utf8_string_view(), { { Production::TemporalTimeString } })); // c. Assert: parseResult.[[Time]] is not START-OF-DAY. VERIFY(!parse_result.time.has()); // d. Set result to parseResult.[[Time]]. time = parse_result.time.get