Skip to content

Truly independent web browser

37.1 KB
Raw Blame
/*
 * Copyright (c) 2026-present, the Ladybird developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

//! Date objects and the time value math of the spec.

use core::cell::{Cell, RefCell};
use core::ops::Deref;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};

use ak::Utf16String;
use libjs_runtime_macros::Trace;
use num_integer::Integer;
use num_traits::{FromPrimitive, ToPrimitive};

use crate::gc::class::{GcCell, define_cell};
use crate::interpreter::vm::Vm;
use crate::layout::cell::Gc;
use crate::layout::object::Object;
use crate::runtime::abstract_operations::modulo;
use crate::runtime::big_int::SignedBigInteger;
use crate::runtime::completion::ThrowCompletionOr;
use crate::runtime::error::ErrorKind;
use crate::runtime::error_types::ErrorType;
use crate::runtime::intl::abstract_operations::get_available_named_time_zone_identifier;
use crate::runtime::object::MayInterfereWithIndexedPropertyAccess;
use crate::runtime::realm::Realm;
use crate::runtime::temporal::instant::{
    NANOSECONDS_PER_MICROSECOND, NANOSECONDS_PER_MILLISECOND, NANOSECONDS_PER_SECOND,
};
use crate::runtime::temporal::iso_records::ISODateTime;
use crate::runtime::temporal::iso8601::{SubMinutePrecision, TimeZoneOffset, parse_utc_offset};
use crate::runtime::temporal::time_zone::parse_time_zone_identifier;
use crate::runtime::value_conversions::to_integer_or_infinity;
use crate::unicode::time_zone::{
    self as unicode_time_zone, TimeZoneOffset as UnicodeTimeZoneOffset, TimeZoneTime, UnixDateTime,
};
use crate::utf16::Utf16View;

/// A Date object, whose [[DateValue]] is `date_value`.
#[repr(C)]
#[derive(Trace)]
pub struct Date {
    base: Object,
    date_value: Cell<f64>,
}

define_cell!(Date, Object, extends: [Object]);

impl Deref for Date {
    type Target = Object;

    fn deref(&self) -> &Object {
        &self.base
    }
}

impl Date {
    pub fn new(vm: &Vm, realm: Gc<Realm>, date_value: f64, prototype: Gc<Object>) -> Date {
        Date {
            base: Object::new_with_prototype(
                vm,
                Self::CLASS,
                realm,
                Some(prototype),
                MayInterfereWithIndexedPropertyAccess::No,
            ),
            date_value: Cell::new(date_value),
        }
    }

    pub fn create(vm: &Vm, realm: Gc<Realm>, date_value: f64) -> Gc<Date> {
        realm.create_object(vm, |_| {
            Date::new(vm, realm, date_value, realm.intrinsics().date_prototype(vm))
        })
    }

    pub fn date_value(&self) -> f64 {
        self.date_value.get()
    }

    pub fn set_date_value(&self, value: f64) {
        self.date_value.set(value);
    }

    pub fn iso_date_string(&self) -> Utf16String {
        let date_value = self.date_value();
        let year = year_from_time(date_value);

        let mut string = if year < 0 {
            format!("-{:06}", -year)
        } else if year > 9999 {
            format!("+{year:06}")
        } else {
            format!("{year:04}")
        };
        string.push_str(&format!(
            "-{:02}-{:02}T{:02}:{:02}:{:02}.{:03}Z",
            month_from_time(date_value) + 1,
            date_from_time(date_value),
            hour_from_time(date_value),
            min_from_time(date_value),
            sec_from_time(date_value),
            ms_from_time(date_value),
        ));
        Utf16String::from_utf8(&string)
    }
}

// 21.4.1.22 Time Zone Identifier Record, https://tc39.es/ecma262/#sec-time-zone-identifier-record
#[derive(Clone)]
pub struct TimeZoneIdentifier {
    pub identifier: Utf16String,         // [[Identifier]]
    pub primary_identifier: Utf16String, // [[PrimaryIdentifier]]
}

// https://tc39.es/ecma262/#eqn-HoursPerDay
pub const HOURS_PER_DAY: f64 = 24.0;
// https://tc39.es/ecma262/#eqn-MinutesPerHour
pub const MINUTES_PER_HOUR: f64 = 60.0;
// https://tc39.es/ecma262/#eqn-SecondsPerMinute
pub const SECONDS_PER_MINUTE: f64 = 60.0;
// https://tc39.es/ecma262/#eqn-msPerSecond
pub const MS_PER_SECOND: f64 = 1_000.0;
// https://tc39.es/ecma262/#eqn-msPerMinute
pub const MS_PER_MINUTE: f64 = 60_000.0;
// https://tc39.es/ecma262/#eqn-msPerHour
pub const MS_PER_HOUR: f64 = 3_600_000.0;
// https://tc39.es/ecma262/#eqn-msPerDay
pub const MS_PER_DAY: f64 = 86_400_000.0;
// https://tc39.es/proposal-temporal/#eqn-nsPerDay
pub const NS_PER_DAY: f64 = 86_400_000_000_000.0;

// https://tc39.es/ecma262/#sec-time-values-and-time-range
// A time value supports a [...] range of -8,640,000,000,000,000 to 8,640,000,000,000,000 milliseconds
pub const MAX_TIME_VALUE: f64 = 8.64E15;

// 21.4.1.3 Day ( t ), https://tc39.es/ecma262/#sec-day
pub fn day(time_value: f64) -> f64 {
    // 1. Return 𝔽(floor(ℝ(t / msPerDay))).
    (time_value / MS_PER_DAY).floor()
}

// 21.4.1.4 TimeWithinDay ( t ), https://tc39.es/ecma262/#sec-timewithinday
pub fn time_within_day(time: f64) -> f64 {
    // 1. Return 𝔽(ℝ(t) modulo ℝ(msPerDay)).
    modulo(time, MS_PER_DAY)
}

// 21.4.1.5 DaysInYear ( y ), https://tc39.es/ecma262/#sec-daysinyear
pub fn days_in_year(y: i32) -> u16 {
    // 1. Let ry be ℝ(y).
    // NB: y is an integer, so integer remainders tell whether it is divisible.

    // 2. If (ry modulo 400) = 0, return 366𝔽.
    if y % 400 == 0 {
        return 366;
    }

    // 3. If (ry modulo 100) = 0, return 365𝔽.
    if y % 100 == 0 {
        return 365;
    }

    // 4. If (ry modulo 4) = 0, return 366𝔽.
    if y % 4 == 0 {
        return 366;
    }

    // 5. Return 365𝔽.
    365
}

// 21.4.1.6 DayFromYear ( y ), https://tc39.es/ecma262/#sec-dayfromyear
pub fn day_from_year(y: i32) -> f64 {
    // 1. Let ry be ℝ(y).
    let ry = f64::from(y);

    // 2. NOTE: In the following steps, each _numYearsN_ is the number of years divisible by N that occur between the
    //    epoch and the start of year y. (The number is negative if y is before the epoch.)

    // 3. Let numYears1 be (ry - 1970).
    let num_years_1 = ry - 1970.0;

    // 4. Let numYears4 be floor((ry - 1969) / 4).
    let num_years_4 = ((ry - 1969.0) / 4.0).floor();

    // 5. Let numYears100 be floor((ry - 1901) / 100).
    let num_years_100 = ((ry - 1901.0) / 100.0).floor();

    // 6. Let numYears400 be floor((ry - 1601) / 400).
    let num_years_400 = ((ry - 1601.0) / 400.0).floor();

    // 7. Return 𝔽(365 Γ— numYears1 + numYears4 - numYears100 + numYears400).
    365.0 * num_years_1 + num_years_4 - num_years_100 + num_years_400
}

// 21.4.1.7 TimeFromYear ( y ), https://tc39.es/ecma262/#sec-timefromyear
pub fn time_from_year(y: i32) -> f64 {
    // 1. Return msPerDay Γ— DayFromYear(y).
    MS_PER_DAY * day_from_year(y)
}

// 21.4.1.8 YearFromTime ( t ), https://tc39.es/ecma262/#sec-yearfromtime
pub fn year_from_time(t: f64) -> i32 {
    // 1. Return the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≀ t.
    if !t.is_finite() {
        return i32::MAX;
    }

    // Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
    // NB: For times far outside the time value range, the conversion saturates and the adjustment wraps around.
    let mut year = (t / (365.2425 * MS_PER_DAY) + 1970.0).floor() as i32;

    let year_t = time_from_year(year);
    if year_t > t {
        year = year.wrapping_sub(1);
    } else if year_t + f64::from(days_in_year(year)) * MS_PER_DAY <= t {
        year = year.wrapping_add(1);
    }

    year
}

/// An integral double as the u16 of a day within a year: a value outside the u16 range, which only a year far outside
/// the time value range gives, becomes u16::MAX, which stays outside the range of a day within a year.
fn day_within_year_to_u16(value: f64) -> u16 {
    if (0.0..=f64::from(u16::MAX)).contains(&value) {
        value as u16
    } else {
        u16::MAX
    }
}

// 21.4.1.9 DayWithinYear ( t ), https://tc39.es/ecma262/#sec-daywithinyear
pub fn day_within_year(t: f64) -> u16 {
    if !t.is_finite() {
        return 0;
    }

    // 1. Return Day(t) - DayFromYear(YearFromTime(t)).
    day_within_year_to_u16(day(t) - day_from_year(year_from_time(t)))
}

// 21.4.1.10 InLeapYear ( t ), https://tc39.es/ecma262/#sec-inleapyear
pub fn in_leap_year(t: f64) -> bool {
    // 1. If DaysInYear(YearFromTime(t)) is 366𝔽, return 1𝔽; else return +0𝔽.
    days_in_year(year_from_time(t)) == 366
}

/// InLeapYear(t) as 0 or 1, and DayWithinYear(t), from a single YearFromTime(t).
fn in_leap_year_and_day_within_year(t: f64) -> (u16, u16) {
    let year = year_from_time(t);
    let in_leap_year = u16::from(days_in_year(year) == 366);
    if !t.is_finite() {
        return (in_leap_year, 0);
    }
    (in_leap_year, day_within_year_to_u16(day(t) - day_from_year(year)))
}

// 21.4.1.11 MonthFromTime ( t ), https://tc39.es/ecma262/#sec-monthfromtime
pub fn month_from_time(t: f64) -> u8 {
    // 1. Let inLeapYear be InLeapYear(t).
    // 2. Let dayWithinYear be DayWithinYear(t).
    let (in_leap_year, day_within_year) = in_leap_year_and_day_within_year(t);
    month_from_day_within_year(in_leap_year, day_within_year)
}

/// Steps 3 and on of MonthFromTime(t), given inLeapYear and dayWithinYear.
fn month_from_day_within_year(in_leap_year: u16, day_within_year: u16) -> u8 {
    // 3. If dayWithinYear < 31𝔽, return +0𝔽.
    if day_within_year < 31 {
        return 0;
    }

    // 4. If dayWithinYear < 59𝔽 + inLeapYear, return 1𝔽.
    if day_within_year < 59 + in_leap_year {
        return 1;
    }

    // 5. If dayWithinYear < 90𝔽 + inLeapYear, return 2𝔽.
    if day_within_year < 90 + in_leap_year {
        return 2;
    }

    // 6. If dayWithinYear < 120𝔽 + inLeapYear, return 3𝔽.
    if day_within_year < 120 + in_leap_year {
        return 3;
    }

    // 7. If dayWithinYear < 151𝔽 + inLeapYear, return 4𝔽.
    if day_within_year < 151 + in_leap_year {
        return 4;
    }

    // 8. If dayWithinYear < 181𝔽 + inLeapYear, return 5𝔽.
    if day_within_year < 181 + in_leap_year {
        return 5;
    }

    // 9. If dayWithinYear < 212𝔽 + inLeapYear, return 6𝔽.
    if day_within_year < 212 + in_leap_year {
        return 6;
    }

    // 10. If dayWithinYear < 243𝔽 + inLeapYear, return 7𝔽.
    if day_within_year < 243 + in_leap_year {
        return 7;
    }

    // 11. If dayWithinYear < 273𝔽 + inLeapYear, return 8𝔽.
    if day_within_year < 273 + in_leap_year {
        return 8;
    }

    // 12. If dayWithinYear < 304𝔽 + inLeapYear, return 9𝔽.
    if day_within_year < 304 + in_leap_year {
        return 9;
    }

    // 13. If dayWithinYear < 334𝔽 + inLeapYear, return 10𝔽.
    if day_within_year < 334 + in_leap_year {
        return 10;
    }

    // 14. Assert: dayWithinYear < 365𝔽 + inLeapYear.
    assert!(
        day_within_year < 365 + in_leap_year,
        "day_within_year < (365 + in_leap_year)"
    );

    // 15. Return 11𝔽.
    11
}

// 21.4.1.12 DateFromTime ( t ), https://tc39.es/ecma262/#sec-datefromtime
pub fn date_from_time(t: f64) -> u8 {
    // 1. Let inLeapYear be InLeapYear(t).
    // 2. Let dayWithinYear be DayWithinYear(t).
    let (in_leap_year, day_within_year) = in_leap_year_and_day_within_year(t);

    // 3. Let month be MonthFromTime(t).
    let month = month_from_day_within_year(in_leap_year, day_within_year);

    let in_leap_year = i32::from(in_leap_year);
    let day_within_year = i32::from(day_within_year);

    let date = match month {
        // 4. If month is +0𝔽, return dayWithinYear + 1𝔽.
        0 => day_within_year + 1,
        // 5. If month is 1𝔽, return dayWithinYear - 30𝔽.
        1 => day_within_year - 30,
        // 6. If month is 2𝔽, return dayWithinYear - 58𝔽 - inLeapYear.
        2 => day_within_year - 58 - in_leap_year,
        // 7. If month is 3𝔽, return dayWithinYear - 89𝔽 - inLeapYear.
        3 => day_within_year - 89 - in_leap_year,
        // 8. If month is 4𝔽, return dayWithinYear - 119𝔽 - inLeapYear.
        4 => day_within_year - 119 - in_leap_year,
        // 9. If month is 5𝔽, return dayWithinYear - 150𝔽 - inLeapYear.
        5 => day_within_year - 150 - in_leap_year,
        // 10. If month is 6𝔽, return dayWithinYear - 180𝔽 - inLeapYear.
        6 => day_within_year - 180 - in_leap_year,
        // 11. If month is 7𝔽, return dayWithinYear - 211𝔽 - inLeapYear.
        7 => day_within_year - 211 - in_leap_year,
        // 12. If month is 8𝔽, return dayWithinYear - 242𝔽 - inLeapYear.
        8 => day_within_year - 242 - in_leap_year,
        // 13. If month is 9𝔽, return dayWithinYear - 272𝔽 - inLeapYear.
        9 => day_within_year - 272 - in_leap_year,
        // 14. If month is 10𝔽, return dayWithinYear - 303𝔽 - inLeapYear.
        10 => day_within_year - 303 - in_leap_year,
        _ => {
            // 15. Assert: month is 11𝔽.
            assert!(month == 11, "month == 11");

            // 16. Return dayWithinYear - 333𝔽 - inLeapYear.
            day_within_year - 333 - in_leap_year
        }
    };
    date as u8
}

// 21.4.1.13 WeekDay ( t ), https://tc39.es/ecma262/#sec-weekday
pub fn week_day(t: f64) -> u8 {
    if !t.is_finite() {
        return 0;
    }

    // 1. Return 𝔽(ℝ(Day(t) + 4𝔽) modulo 7).
    modulo(day(t) + 4.0, 7.0) as u8
}

// 21.4.1.14 HourFromTime ( t ), https://tc39.es/ecma262/#sec-hourfromtime
pub fn hour_from_time(t: f64) -> u8 {
    if !t.is_finite() {
        return 0;
    }

    // 1. Return 𝔽(floor(ℝ(t / msPerHour)) modulo HoursPerDay).
    modulo((t / MS_PER_HOUR).floor(), HOURS_PER_DAY) as u8
}

// 21.4.1.15 MinFromTime ( t ), https://tc39.es/ecma262/#sec-minfromtime
pub fn min_from_time(t: f64) -> u8 {
    if !t.is_finite() {
        return 0;
    }

    // 1. Return 𝔽(floor(ℝ(t / msPerMinute)) modulo MinutesPerHour).
    modulo((t / MS_PER_MINUTE).floor(), MINUTES_PER_HOUR) as u8
}

// 21.4.1.16 SecFromTime ( t ), https://tc39.es/ecma262/#sec-secfromtime
pub fn sec_from_time(t: f64) -> u8 {
    if !t.is_finite() {
        return 0;
    }

    // 1. Return 𝔽(floor(ℝ(t / msPerSecond)) modulo SecondsPerMinute).
    modulo((t / MS_PER_SECOND).floor(), SECONDS_PER_MINUTE) as u8
}

// 21.4.1.17 msFromTime ( t ), https://tc39.es/ecma262/#sec-msfromtime
pub fn ms_from_time(t: f64) -> u16 {
    if !t.is_finite() {
        return 0;
    }

    // 1. Return 𝔽(ℝ(t) modulo ℝ(msPerSecond)).
    modulo(t, MS_PER_SECOND) as u16
}

// 21.4.1.18 GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/ecma262/#sec-getutcepochnanoseconds
// 14.5.1 GetUTCEpochNanoseconds ( isoDateTime ), https://tc39.es/proposal-temporal/#sec-getutcepochnanoseconds
pub fn get_utc_epoch_nanoseconds(iso_date_time: &ISODateTime) -> SignedBigInteger {
    // 1. Let date be MakeDay(𝔽(isoDateTime.[[ISODate]].[[Year]]), 𝔽(isoDateTime.[[ISODate]].[[Month]] - 1), 𝔽(isoDateTime.[[ISODate]].[[Day]])).
    let date = make_day(
        f64::from(iso_date_time.iso_date.year),
        f64::from(iso_date_time.iso_date.month) - 1.0,
        f64::from(iso_date_time.iso_date.day),
    );

    // 2. Let time be MakeTime(𝔽(isoDateTime.[[Time]].[[Hour]]), 𝔽(isoDateTime.[[Time]].[[Minute]]), 𝔽(isoDateTime.[[Time]].[[Second]]), 𝔽(isoDateTime.[[Time]].[[Millisecond]])).
    let time = make_time(
        f64::from(iso_date_time.time.hour),
        f64::from(iso_date_time.time.minute),
        f64::from(iso_date_time.time.second),
        f64::from(iso_date_time.time.millisecond),
    );

    // 3. Let ms be MakeDate(date, time).
    let ms = make_date(date, time);

    // 4. Assert: ms is an integral Number.
    assert!(ms == ms.trunc(), "ms == trunc(ms)");

    // 5. Return β„€(ℝ(ms) Γ— 10**6 + isoDateTime.[[Time]].[[Microsecond]] Γ— 10**3 + isoDateTime.[[Time]].[[Nanosecond]]).
    let ms = SignedBigInteger::from_f64(ms).expect("an integral Number is an integer");
    ms * &*NANOSECONDS_PER_MILLISECOND
        + SignedBigInteger::from(iso_date_time.time.microsecond) * &*NANOSECONDS_PER_MICROSECOND
        + SignedBigInteger::from(iso_date_time.time.nanosecond)
}

pub fn clip_bigint_to_sane_time(value: &SignedBigInteger) -> i64 {
    // The provided epoch (nano)seconds value is potentially out of range for AK::Duration and subsequently
    // get_time_zone_offset(). We can safely assume that the TZDB has no useful information that far
    // into the past and future anyway, so clamp it to the i64 range.
    value.to_i64().unwrap_or(if value.sign() == num_bigint::Sign::Minus {
        i64::MIN
    } else {
        i64::MAX
    })
}

pub fn clip_double_to_sane_time(value: f64) -> i64 {
    const MIN_DOUBLE: f64 = i64::MIN as f64;
    const MAX_DOUBLE: f64 = i64::MAX as f64;

    // The provided epoch milliseconds value is potentially out of range for AK::Duration and subsequently
    // get_time_zone_offset(). We can safely assume that the TZDB has no useful information that far
    // into the past and future anyway, so clamp it to the i64 range.
    if value < MIN_DOUBLE {
        return i64::MIN;
    }
    if value > MAX_DOUBLE {
        return i64::MAX;
    }

    value as i64
}

// 21.4.1.20 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/ecma262/#sec-getnamedtimezoneepochnanoseconds
// 14.6.3 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier, isoDateTime ), https://tc39.es/proposal-temporal/#sec-getnamedtimezoneepochnanoseconds
pub fn get_named_time_zone_epoch_nanoseconds(
    time_zone_identifier: Utf16View<'_>,
    iso_date_time: &ISODateTime,
) -> Vec<SignedBigInteger> {
    let local_nanoseconds = get_utc_epoch_nanoseconds(iso_date_time);
    let milliseconds = local_nanoseconds.div_floor(&NANOSECONDS_PER_MILLISECOND);
    let local_time = UnixDateTime::from_milliseconds_since_epoch(clip_bigint_to_sane_time(&milliseconds));

    let offsets = unicode_time_zone::disambiguated_time_zone_offsets(time_zone_identifier, local_time);

    offsets
        .iter()
        .map(|offset| &local_nanoseconds - SignedBigInteger::from(offset.offset_nanoseconds))
        .collect()
}

// 21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds ), https://tc39.es/ecma262/#sec-getnamedtimezoneoffsetnanoseconds
pub fn get_named_time_zone_offset_nanoseconds(
    time_zone_identifier: Utf16View<'_>,
    epoch_nanoseconds: &SignedBigInteger,
) -> UnicodeTimeZoneOffset {
    // TZDB offsets have second precision. Convert before narrowing to keep the full Temporal range.
    let seconds = epoch_nanoseconds.div_floor(&NANOSECONDS_PER_SECOND.clone());
    let time = UnixDateTime::from_seconds_since_epoch(clip_bigint_to_sane_time(&seconds));

    unicode_time_zone::time_zone_offset(time_zone_identifier, time, TimeZoneTime::Utc).expect("offset.has_value()")
}

// 21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds ), https://tc39.es/ecma262/#sec-getnamedtimezoneoffsetnanoseconds
// OPTIMIZATION: This overload is provided to allow callers to avoid BigInt construction if they do not need infinitely precise nanosecond resolution.
pub fn get_named_time_zone_offset_milliseconds(
    time_zone_identifier: Utf16View<'_>,
    epoch_milliseconds: f64,
) -> UnicodeTimeZoneOffset {
    let seconds = epoch_milliseconds / 1000.0;
    let time = UnixDateTime::from_seconds_since_epoch(clip_double_to_sane_time(seconds));

    unicode_time_zone::time_zone_offset(time_zone_identifier, time, TimeZoneTime::Utc).expect("offset.has_value()")
}

/// The cached SystemTimeZoneIdentifier(), as code units, so that it can be shared by the threads of the agents.
static CACHED_SYSTEM_TIME_ZONE_IDENTIFIER: Mutex<Option<Vec<u16>>> = Mutex::new(None);

// 21.4.1.24 SystemTimeZoneIdentifier ( ), https://tc39.es/ecma262/#sec-systemtimezoneidentifier
pub fn system_time_zone_identifier() -> Utf16String {
    // OPTIMIZATION: We cache the system time zone to avoid the expensive lookups below.
    if let Some(identifier) = CACHED_SYSTEM_TIME_ZONE_IDENTIFIER
        .lock()
        .expect("the time zone cache is never poisoned")
        .as_ref()
    {
        return Utf16String::from_utf16(identifier);
    }

    // 1. If the implementation only supports the UTC time zone, return "UTC".

    // 2. Let systemTimeZoneString be the String representing the host environment's current time zone, either a primary
    //    time zone identifier or an offset time zone identifier.
    let mut system_time_zone_string = unicode_time_zone::current_time_zone();

    if !is_offset_time_zone_identifier(Utf16View::of_string(&system_time_zone_string)) {
        let Some(time_zone_identifier) =
            get_available_named_time_zone_identifier(Utf16View::of_string(&system_time_zone_string))
        else {
            return Utf16String::from_utf8("UTC");
        };

        system_time_zone_string = time_zone_identifier.primary_identifier.clone();
    }

    // 3. Return systemTimeZoneString.
    *CACHED_SYSTEM_TIME_ZONE_IDENTIFIER
        .lock()
        .expect("the time zone cache is never poisoned") =
        Some(Utf16View::of_string(&system_time_zone_string).code_units().collect());
    system_time_zone_string
}

pub fn clear_system_time_zone_cache() {
    *CACHED_SYSTEM_TIME_ZONE_IDENTIFIER
        .lock()
        .expect("the time zone cache is never poisoned") = None;
    SYSTEM_TIME_ZONE_GENERATION.fetch_add(1, Ordering::AcqRel);
}

/// What LocalTime and UTC need to know about the system time zone, kept per thread so that they take neither the lock
/// of the shared identifier cache nor parse the identifier again on every call.
struct SystemTimeZone {
    generation: u64,
    identifier: Utf16String,
    offset_minutes: Option<i64>,
    /// The epoch seconds and offset in nanoseconds of the most recent named time zone offset lookup.
    last_offset: Option<(i64, i64)>,
}

/// Bumped whenever the system time zone cache is cleared, which makes every thread's SystemTimeZone stale.
static SYSTEM_TIME_ZONE_GENERATION: AtomicU64 = AtomicU64::new(0);

std::thread_local! {
    static SYSTEM_TIME_ZONE: RefCell<Option<SystemTimeZone>> = const { RefCell::new(None) };
}

fn with_system_time_zone<R>(callback: impl FnOnce(&mut SystemTimeZone) -> R) -> R {
    SYSTEM_TIME_ZONE.with_borrow_mut(|system_time_zone| {
        let generation = SYSTEM_TIME_ZONE_GENERATION.load(Ordering::Acquire);
        if system_time_zone
            .as_ref()
            .is_none_or(|system_time_zone| system_time_zone.generation != generation)
        {
            let identifier = system_time_zone_identifier();
            let offset_minutes = parse_time_zone_identifier(Utf16View::of_string(&identifier)).offset_minutes;
            *system_time_zone = Some(SystemTimeZone {
                generation,
                identifier,
                offset_minutes,
                last_offset: None,
            });
        }
        callback(system_time_zone.as_mut().expect("the system time zone was just cached"))
    })
}

// 21.4.1.25 LocalTime ( t ), https://tc39.es/ecma262/#sec-localtime
// 14.5.6 LocalTime ( t ), https://tc39.es/proposal-temporal/#sec-localtime
pub fn local_time(time: f64) -> f64 {
    let offset_nanoseconds = with_system_time_zone(|system_time_zone| {
        // 1. Let systemTimeZoneIdentifier be SystemTimeZoneIdentifier().
        // 2. Let parseResult be ! ParseTimeZoneIdentifier(systemTimeZoneIdentifier).
        // NB: Both come from the per-thread system time zone cache.

        // 3. If parseResult.[[OffsetMinutes]] is not EMPTY, then
        if let Some(offset_minutes) = system_time_zone.offset_minutes {
            // a. Let offsetNs be parseResult.[[OffsetMinutes]] Γ— (60 Γ— 10**9).
            return offset_minutes as f64 * 60_000_000_000.0;
        }

        // 4. Else,
        //     a. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, β„€(ℝ(t) Γ— 10^6)).
        // OPTIMIZATION: Code tends to call several getters on the same Date in a row, so remember the offset of the
        //               last second we looked up instead of asking the time zone database again.
        let seconds = clip_double_to_sane_time(time / 1000.0);
        if let Some((cached_seconds, cached_offset_nanoseconds)) = system_time_zone.last_offset
            && cached_seconds == seconds
        {
            return cached_offset_nanoseconds as f64;
        }
        let offset = get_named_time_zone_offset_milliseconds(Utf16View::of_string(&system_time_zone.identifier), time);
        system_time_zone.last_offset = Some((seconds, offset.offset_nanoseconds));
        offset.offset_nanoseconds as f64
    });

    // 5. Let offsetMs be truncate(offsetNs / 10^6).
    let offset_milliseconds = (offset_nanoseconds / 1e6).trunc();

    // 6. Return t + 𝔽(offsetMs).
    time + offset_milliseconds
}

// 21.4.1.26 UTC ( t ), https://tc39.es/ecma262/#sec-utc-t
// 14.6.7 UTC ( t ), https://tc39.es/proposal-temporal/#sec-utc-t
pub fn utc_time(time: f64) -> f64 {
    // 1. If t is not finite, return NaN.
    if !time.is_finite() {
        return f64::NAN;
    }

    if time.abs() > MAX_TIME_VALUE + MS_PER_DAY {
        return f64::NAN;
    }

    // 2. Let systemTimeZoneIdentifier be SystemTimeZoneIdentifier().
    // 3. Let parseResult be ! ParseTimeZoneIdentifier(systemTimeZoneIdentifier).
    // NB: Both come from the per-thread system time zone cache.
    let (system_time_zone_identifier, offset_minutes) = with_system_time_zone(|system_time_zone| {
        (system_time_zone.identifier.clone(), system_time_zone.offset_minutes)
    });
    let system_time_zone_identifier = Utf16View::of_string(&system_time_zone_identifier);

    // 4. If parseResult.[[OffsetMinutes]] is not EMPTY, then
    let offset_nanoseconds = if let Some(offset_minutes) = offset_minutes {
        // a. Let offsetNs be parseResult.[[OffsetMinutes]] Γ— (60 Γ— 10**9).
        offset_minutes as f64 * 60_000_000_000.0
    }
    // 5. Else,
    else {
        // Steps 5.a-f use the offset before the transition for both skipped and repeated local times.
        let local_time = UnixDateTime::from_milliseconds_since_epoch(time as i64);
        let Some(offset) =
            unicode_time_zone::time_zone_offset(system_time_zone_identifier, local_time, TimeZoneTime::Local)
        else {
            return f64::NAN;
        };
        offset.offset_nanoseconds as f64
    };

    // 6. Let offsetMs be truncate(offsetNs / 10^6).
    let offset_milliseconds = (offset_nanoseconds / 1e6).trunc();

    // 7. Return t - 𝔽(offsetMs).
    time - offset_milliseconds
}

// 21.4.1.27 MakeTime ( hour, min, sec, ms ), https://tc39.es/ecma262/#sec-maketime
pub fn make_time(hour: f64, min: f64, sec: f64, ms: f64) -> f64 {
    // 1. If hour is not finite or min is not finite or sec is not finite or ms is not finite, return NaN.
    if !hour.is_finite() || !min.is_finite() || !sec.is_finite() || !ms.is_finite() {
        return f64::NAN;
    }

    // 2. Let h be 𝔽(! ToIntegerOrInfinity(hour)).
    let h = to_integer_or_infinity(hour);
    // 3. Let m be 𝔽(! ToIntegerOrInfinity(min)).
    let m = to_integer_or_infinity(min);
    // 4. Let s be 𝔽(! ToIntegerOrInfinity(sec)).
    let s = to_integer_or_infinity(sec);
    // 5. Let milli be 𝔽(! ToIntegerOrInfinity(ms)).
    let milli = to_integer_or_infinity(ms);
    // 6. Let t be ((h * msPerHour + m * msPerMinute) + s * msPerSecond) + milli, performing the arithmetic according to IEEE 754-2019 rules (that is, as if using the ECMAScript operators * and +).
    // NOTE: Rust arithmetic abides by IEEE 754 rules
    // 7. Return t.
    ((h * MS_PER_HOUR + m * MS_PER_MINUTE) + s * MS_PER_SECOND) + milli
}

/// AK::is_within_range<int>() of a double: whether it is an integer in the range of an i32.
pub fn is_within_i32_range(value: f64) -> bool {
    const BOUNDARY: f64 = 2_147_483_648.0;
    (-BOUNDARY..BOUNDARY).contains(&value) && f64::from(value as i32) == value
}

/// AK::is_within_range<u8>() of a double: whether it is an integer in the range of a u8.
pub fn is_within_u8_range(value: f64) -> bool {
    (0.0..256.0).contains(&value) && f64::from(value as u8) == value
}

// Integer division rounding towards negative infinity, as AK::Detail::floor_div_by does.
fn floor_div_by(dividend: i64, divisor: i64) -> i64 {
    let is_negative = i64::from(dividend < 0);
    (dividend + is_negative) / divisor - is_negative
}

// Counts how many integers n are in the interval [begin, end) with n % positive_mod == 0, as
// AK::Detail::mod_zeros_in_range does.
fn mod_zeros_in_range(begin: i64, end: i64, positive_mod: i64) -> i64 {
    floor_div_by(end - 1, positive_mod) - floor_div_by(begin - 1, positive_mod)
}

/// AK::is_leap_year.
fn is_leap_year(year: i32) -> bool {
    year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)
}

/// AK::years_to_days_since_epoch.
fn years_to_days_since_epoch(year: i32) -> i64 {
    let (begin_year, end_year, leap_sign) = if year < 1970 {
        (i64::from(year), 1970, -1)
    } else {
        (1970, i64::from(year), 1)
    };
    let days = 365 * (i64::from(year) - 1970);
    let mut extra_leap_days = 0;
    extra_leap_days += mod_zeros_in_range(begin_year, end_year, 4);
    extra_leap_days -= mod_zeros_in_range(begin_year, end_year, 100);
    extra_leap_days += mod_zeros_in_range(begin_year, end_year, 400);
    days + extra_leap_days * leap_sign
}

/// AK::day_of_year, with a month in [1, 12].
fn day_of_year(year: i32, month: i32, day: i32) -> i64 {
    const SEEK_TABLE: [i64; 12] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];

    if !(1..=12).contains(&month) {
        return 0;
    }

    let mut day_of_year = SEEK_TABLE[(month - 1) as usize] + i64::from(day) - 1;
    if is_leap_year(year) && month >= 3 {
        day_of_year += 1;
    }
    day_of_year
}

/// AK::days_since_epoch.
pub(crate) fn days_since_epoch(year: i32, month: i32, day: i32) -> i64 {
    years_to_days_since_epoch(year) + day_of_year(year, month, day)
}

// 21.4.1.28 MakeDay ( year, month, date ), https://tc39.es/ecma262/#sec-makeday
pub fn make_day(year: f64, month: f64, date: f64) -> f64 {
    // 1. If year is not finite or month is not finite or date is not finite, return NaN.
    if !year.is_finite() || !month.is_finite() || !date.is_finite() {
        return f64::NAN;
    }

    // 2. Let y be 𝔽(! ToIntegerOrInfinity(year)).
    let y = to_integer_or_infinity(year);
    // 3. Let m be 𝔽(! ToIntegerOrInfinity(month)).
    let m = to_integer_or_infinity(month);
    // 4. Let dt be 𝔽(! ToIntegerOrInfinity(date)).
    let dt = to_integer_or_infinity(date);
    // 5. Let ym be y + 𝔽(floor(ℝ(m) / 12)).
    let ym = y + (m / 12.0).floor();
    // 6. If ym is not finite, return NaN.
    if !ym.is_finite() {
        return f64::NAN;
    }
    // 7. Let mn be 𝔽(ℝ(m) modulo 12).
    let mn = modulo(m, 12.0);

    // 8. Find a finite time value t such that YearFromTime(t) is ym and MonthFromTime(t) is mn and DateFromTime(t) is 1𝔽; but if this is not possible (because some argument is out of range), return NaN.
    if !is_within_i32_range(ym) || !is_within_i32_range(mn + 1.0) {
        return f64::NAN;
    }
    let t = days_since_epoch(ym as i32, mn as i32 + 1, 1) as f64 * MS_PER_DAY;

    // 9. Return Day(t) + dt - 1𝔽.
    day(t) + dt - 1.0
}

// 21.4.1.29 MakeDate ( day, time ), https://tc39.es/ecma262/#sec-makedate
pub fn make_date(day: f64, time: f64) -> f64 {
    // 1. If day is not finite or time is not finite, return NaN.
    if !day.is_finite() || !time.is_finite() {
        return f64::NAN;
    }

    // 2. Let tv be day Γ— msPerDay + time.
    let tv = day * MS_PER_DAY + time;

    // 3. If tv is not finite, return NaN.
    if !tv.is_finite() {
        return f64::NAN;
    }

    // 4. Return tv.
    tv
}

// 21.4.1.31 TimeClip ( time ), https://tc39.es/ecma262/#sec-timeclip
pub fn time_clip(time: f64) -> f64 {
    // 1. If time is not finite, return NaN.
    if !time.is_finite() {
        return f64::NAN;
    }

    // 2. If abs(ℝ(time)) > 8.64 Γ— 10^15, return NaN.
    if time.abs() > 8.64E15 {
        return f64::NAN;
    }

    // 3. Return 𝔽(! ToIntegerOrInfinity(time)).
    to_integer_or_infinity(time)
}

// 21.4.1.33.1 IsTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-istimezoneoffsetstring
// 14.5.10 IsOffsetTimeZoneIdentifier ( offsetString ), https://tc39.es/proposal-temporal/#sec-isoffsettimezoneidentifier
pub fn is_offset_time_zone_identifier(offset_string: Utf16View<'_>) -> bool {
    // 1. Let parseResult be ParseText(StringToCodePoints(offsetString), UTCOffset[~SubMinutePrecision]).
    let parse_result = parse_utc_offset(offset_string, SubMinutePrecision::No);

    // 2. If parseResult is a List of errors, return false.
    // 3. Return true.
    parse_result.is_some()
}

// 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
// 14.5.11 ParseDateTimeUTCOffset ( offsetString ), https://tc39.es/proposal-temporal/#sec-parsedatetimeutcoffset
pub fn parse_date_time_utc_offset_or_throw(vm: &Vm, offset_string: Utf16View<'_>) -> ThrowCompletionOr<f64> {
    // 1. Let parseResult be ParseText(offsetString, UTCOffset[+SubMinutePrecision]).
    let Some(parse_result) = parse_utc_offset(offset_string, SubMinutePrecision::Yes) else {
        // 2. If parseResult is a List of errors, throw a RangeError exception.
        return vm.throw_completion(
            ErrorKind::RangeError,
            ErrorType::TemporalInvalidTimeZoneString,
            &[&offset_string],
        );
    };

    Ok(parse_date_time_utc_offset_from_parse_result(&parse_result))
}

// 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
// 14.5.11 ParseDateTimeUTCOffset ( offsetString ), https://tc39.es/proposal-temporal/#sec-parsedatetimeutcoffset
pub fn parse_date_time_utc_offset(offset_string: Utf16View<'_>) -> f64 {
    // OPTIMIZATION: Some callers can assume that parsing will succeed.

    // 1. Let parseResult be ParseText(offsetString, UTCOffset[+SubMinutePrecision]).
    let parse_result = parse_utc_offset(offset_string, SubMinutePrecision::Yes).expect("parse_result.has_value()");

    parse_date_time_utc_offset_from_parse_result(&parse_result)
}

/// The value of a parse node of decimal digits, as Utf16View::to_number<u8>() computes it.
fn decimal_digits_value(digits: Utf16View<'_>) -> f64 {
    digits.code_units().fold(0.0, |value, code_unit| {
        value * 10.0 + f64::from(code_unit - u16::from(b'0'))
    })
}

// 21.4.1.33.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
// 14.5.11 ParseDateTimeUTCOffset ( offsetString ), https://tc39.es/proposal-temporal/#sec-parsedatetimeutcoffset
pub fn parse_date_time_utc_offset_from_parse_result(parse_result: &TimeZoneOffset<'_>) -> f64 {
    // OPTIMIZATION: Some callers will have already parsed and validated the time zone identifier.

    // 3. Assert: parseResult contains a ASCIISign Parse Node.
    let parsed_sign = parse_result.sign.expect("parse_result.sign.has_value()");

    // 4. Let parsedSign be the source text matched by the ASCIISign Parse Node contained within parseResult.
    // 5. If parsedSign is the single code point U+002D (HYPHEN-MINUS), then
    //     a. Let sign be -1.
    // 6. Else,
    //     a. Let sign be 1.
    let sign = if parsed_sign == b'-' { -1.0 } else { 1.0 };

    // 7. NOTE: Applications of StringToNumber below do not lose precision, since each of the parsed values is guaranteed
    //    to be a sufficiently short string of decimal digits.

    // 8. Assert: parseResult contains an Hour Parse Node.
    // 9. Let parsedHours be the source text matched by the Hour Parse Node contained within parseResult.
    // 10. Let hours be ℝ(StringToNumber(CodePointsToString(parsedHours))).
    let hours = decimal_digits_value(parse_result.hours.expect("parse_result.hours.has_value()"));

    // 11. If parseResult does not contain a MinuteSecond Parse Node, then
    //     a. Let minutes be 0.
    // 12. Else,
    //     a. Let parsedMinutes be the source text matched by the first MinuteSecond Parse Node contained within parseResult.
    //     b. Let minutes be ℝ(StringToNumber(CodePointsToString(parsedMinutes))).
    let minutes = parse_result.minutes.map_or(0.0, decimal_digits_value);

    // 13. If parseResult does not contain two MinuteSecond Parse Nodes, then
    //     a. Let seconds be 0.
    // 14. Else,
    //     a. Let parsedSeconds be the source text matched by the second secondSecond Parse Node contained within parseResult.
    //     b. Let seconds be ℝ(StringToNumber(CodePointsToString(parsedSeconds))).
    let seconds = parse_result.seconds.map_or(0.0, decimal_digits_value);

    let mut nanoseconds = 0.0;

    // 15. If parseResult does not contain a TemporalDecimalFraction Parse Node, then
    //     a. Let nanoseconds be 0.
    // 16. Else,
    if let Some(parsed_fraction) = parse_result.fraction {
        // a. Let parsedFraction be the source text matched by the TemporalDecimalFraction Parse Node contained within parseResult.
        // b. Let fraction be the string-concatenation of CodePointsToString(parsedFraction) and "000000000".
        // c. Let nanosecondsString be the substring of fraction from 1 to 10.
        // d. Let nanoseconds be ℝ(StringToNumber(nanosecondsString)).
        for i in 1..10 {
            nanoseconds *= 10.0;
            if i < parsed_fraction.length_in_code_units() {
                nanoseconds += f64::from(parsed_fraction.code_unit_at(i) - u16::from(b'0'));
            }
        }
    }

    // 17. Return sign Γ— (((hours Γ— 60 + minutes) Γ— 60 + seconds) Γ— 10^9 + nanoseconds).
    sign * (((hours * 60.0 + minutes) * 60.0 + seconds) * 1e9 + nanoseconds)
}