2020-03-29 23:21:56 +00:00
/*
2023-03-01 14:48:12 +00:00
* Copyright ( c ) 2020 - 2023 , Linus Groh < linusg @ serenityos . org >
2022-01-31 18:07:22 +00:00
* Copyright ( c ) 2022 , Tim Flynn < trflynn89 @ serenityos . org >
2020-03-29 23:21:56 +00:00
*
2021-04-22 08:24:48 +00:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-03-29 23:21:56 +00:00
*/
2022-01-15 22:37:36 +00:00
# include <AK/NumericLimits.h>
2020-08-20 20:29:27 +00:00
# include <AK/StringBuilder.h>
2022-01-05 17:26:15 +00:00
# include <AK/Time.h>
2021-11-16 19:40:03 +00:00
# include <LibJS/Runtime/AbstractOperations.h>
2020-03-29 23:21:56 +00:00
# include <LibJS/Runtime/Date.h>
2020-04-17 17:07:59 +00:00
# include <LibJS/Runtime/GlobalObject.h>
2022-10-14 13:23:43 +00:00
# include <LibJS/Runtime/Temporal/ISO8601.h>
2022-01-12 04:27:05 +00:00
# include <LibTimeZone/TimeZone.h>
2021-06-06 12:42:19 +00:00
# include <time.h>
2020-03-29 23:21:56 +00:00
namespace JS {
2022-10-14 13:23:43 +00:00
static Crypto : : SignedBigInteger const s_one_billion_bigint { 1'000'000'000 } ;
static Crypto : : SignedBigInteger const s_one_million_bigint { 1'000'000 } ;
static Crypto : : SignedBigInteger const s_one_thousand_bigint { 1'000 } ;
2023-01-28 16:12:54 +00:00
Crypto : : SignedBigInteger const ns_per_day_bigint { static_cast < i64 > ( ns_per_day ) } ;
2022-12-13 20:49:50 +00:00
NonnullGCPtr < Date > Date : : create ( Realm & realm , double date_value )
2022-01-14 22:42:42 +00:00
{
2023-04-12 22:47:15 +00:00
return realm . heap ( ) . allocate < Date > ( realm , date_value , realm . intrinsics ( ) . date_prototype ( ) ) . release_allocated_value_but_fixme_should_propagate_errors ( ) ;
2022-01-14 22:42:42 +00:00
}
Date : : Date ( double date_value , Object & prototype )
2022-12-14 11:17:58 +00:00
: Object ( ConstructWithPrototypeTag : : Tag , prototype )
2022-01-14 22:42:42 +00:00
, m_date_value ( date_value )
{
}
2022-12-04 18:02:33 +00:00
DeprecatedString Date : : iso_date_string ( ) const
2020-08-23 17:30:18 +00:00
{
2022-01-14 22:42:42 +00:00
int year = year_from_time ( m_date_value ) ;
2020-08-20 20:29:27 +00:00
StringBuilder builder ;
if ( year < 0 )
2021-05-07 09:51:57 +00:00
builder . appendff ( " -{:06} " , - year ) ;
2020-08-20 20:29:27 +00:00
else if ( year > 9999 )
2021-05-07 09:51:57 +00:00
builder . appendff ( " +{:06} " , year ) ;
2020-08-20 20:29:27 +00:00
else
2021-05-07 09:51:57 +00:00
builder . appendff ( " {:04} " , year ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' - ' ) ;
2022-01-14 22:42:42 +00:00
builder . appendff ( " {:02} " , month_from_time ( m_date_value ) + 1 ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' - ' ) ;
2022-01-14 22:42:42 +00:00
builder . appendff ( " {:02} " , date_from_time ( m_date_value ) ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' T ' ) ;
2022-01-14 22:42:42 +00:00
builder . appendff ( " {:02} " , hour_from_time ( m_date_value ) ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' : ' ) ;
2022-01-14 22:42:42 +00:00
builder . appendff ( " {:02} " , min_from_time ( m_date_value ) ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' : ' ) ;
2022-01-14 22:42:42 +00:00
builder . appendff ( " {:02} " , sec_from_time ( m_date_value ) ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' . ' ) ;
2022-01-14 22:42:42 +00:00
builder . appendff ( " {:03} " , ms_from_time ( m_date_value ) ) ;
2020-08-20 20:29:27 +00:00
builder . append ( ' Z ' ) ;
2023-01-26 18:58:09 +00:00
return builder . to_deprecated_string ( ) ;
2020-08-20 20:29:27 +00:00
}
2021-07-25 19:47:40 +00:00
// DayWithinYear(t), https://tc39.es/ecma262/#eqn-DayWithinYear
u16 day_within_year ( double t )
{
2022-01-16 05:54:48 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return 0 ;
2021-07-25 19:47:40 +00:00
// Day(t) - DayFromYear(YearFromTime(t))
return static_cast < u16 > ( day ( t ) - day_from_year ( year_from_time ( t ) ) ) ;
}
// DateFromTime(t), https://tc39.es/ecma262/#sec-date-number
u8 date_from_time ( double t )
{
switch ( month_from_time ( t ) ) {
// DayWithinYear(t) + 1𝔽 if MonthFromTime(t) = +0𝔽
case 0 :
return day_within_year ( t ) + 1 ;
// DayWithinYear(t) - 30𝔽 if MonthFromTime(t) = 1𝔽
case 1 :
return day_within_year ( t ) - 30 ;
// DayWithinYear(t) - 58𝔽 - InLeapYear(t) if MonthFromTime(t) = 2𝔽
case 2 :
return day_within_year ( t ) - 58 - in_leap_year ( t ) ;
// DayWithinYear(t) - 89𝔽 - InLeapYear(t) if MonthFromTime(t) = 3𝔽
case 3 :
return day_within_year ( t ) - 89 - in_leap_year ( t ) ;
// DayWithinYear(t) - 119𝔽 - InLeapYear(t) if MonthFromTime(t) = 4𝔽
case 4 :
return day_within_year ( t ) - 119 - in_leap_year ( t ) ;
// DayWithinYear(t) - 150𝔽 - InLeapYear(t) if MonthFromTime(t) = 5𝔽
case 5 :
return day_within_year ( t ) - 150 - in_leap_year ( t ) ;
// DayWithinYear(t) - 180𝔽 - InLeapYear(t) if MonthFromTime(t) = 6𝔽
case 6 :
return day_within_year ( t ) - 180 - in_leap_year ( t ) ;
// DayWithinYear(t) - 211𝔽 - InLeapYear(t) if MonthFromTime(t) = 7𝔽
case 7 :
return day_within_year ( t ) - 211 - in_leap_year ( t ) ;
// DayWithinYear(t) - 242𝔽 - InLeapYear(t) if MonthFromTime(t) = 8𝔽
case 8 :
return day_within_year ( t ) - 242 - in_leap_year ( t ) ;
// DayWithinYear(t) - 272𝔽 - InLeapYear(t) if MonthFromTime(t) = 9𝔽
case 9 :
return day_within_year ( t ) - 272 - in_leap_year ( t ) ;
// DayWithinYear(t) - 303𝔽 - InLeapYear(t) if MonthFromTime(t) = 10𝔽
case 10 :
return day_within_year ( t ) - 303 - in_leap_year ( t ) ;
// DayWithinYear(t) - 333𝔽 - InLeapYear(t) if MonthFromTime(t) = 11𝔽
case 11 :
return day_within_year ( t ) - 333 - in_leap_year ( t ) ;
default :
VERIFY_NOT_REACHED ( ) ;
}
}
// DaysInYear(y), https://tc39.es/ecma262/#eqn-DaysInYear
u16 days_in_year ( i32 y )
{
// 365𝔽 if (ℝ (y) modulo 4) ≠ 0
if ( y % 4 ! = 0 )
return 365 ;
// 366𝔽 if (ℝ (y) modulo 4) = 0 and (ℝ (y) modulo 100) ≠ 0
if ( y % 4 = = 0 & & y % 100 ! = 0 )
return 366 ;
// 365𝔽 if (ℝ (y) modulo 100) = 0 and (ℝ (y) modulo 400) ≠ 0
if ( y % 100 = = 0 & & y % 400 ! = 0 )
return 365 ;
// 366𝔽 if (ℝ (y) modulo 400) = 0
if ( y % 400 = = 0 )
return 366 ;
VERIFY_NOT_REACHED ( ) ;
}
// DayFromYear(y), https://tc39.es/ecma262/#eqn-DaysFromYear
double day_from_year ( i32 y )
{
// 𝔽 (365 × (ℝ (y) - 1970) + floor((ℝ (y) - 1969) / 4) - floor((ℝ (y) - 1901) / 100) + floor((ℝ (y) - 1601) / 400))
2022-01-15 22:36:25 +00:00
return 365.0 * ( y - 1970 ) + floor ( ( y - 1969 ) / 4.0 ) - floor ( ( y - 1901 ) / 100.0 ) + floor ( ( y - 1601 ) / 400.0 ) ;
2021-07-25 19:47:40 +00:00
}
2021-11-05 00:18:27 +00:00
// TimeFromYear(y), https://tc39.es/ecma262/#eqn-TimeFromYear
double time_from_year ( i32 y )
{
// msPerDay × DayFromYear(y)
2022-05-06 17:10:34 +00:00
return ms_per_day * day_from_year ( y ) ;
2021-11-05 00:18:27 +00:00
}
2021-07-25 19:47:40 +00:00
// YearFromTime(t), https://tc39.es/ecma262/#eqn-YearFromTime
i32 year_from_time ( double t )
{
// the largest integral Number y (closest to +∞) such that TimeFromYear(y) ≤ t
2022-01-15 22:37:36 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return NumericLimits < i32 > : : max ( ) ;
2021-11-05 00:18:27 +00:00
// Approximation using average number of milliseconds per year. We might have to adjust this guess afterwards.
2022-05-06 17:10:34 +00:00
auto year = static_cast < i32 > ( t / ( 365.2425 * ms_per_day ) + 1970 ) ;
2021-11-05 00:18:27 +00:00
auto year_t = time_from_year ( year ) ;
if ( year_t > t )
year - - ;
2022-05-06 17:10:34 +00:00
else if ( year_t + days_in_year ( year ) * ms_per_day < = t )
2021-11-05 00:18:27 +00:00
year + + ;
return year ;
2021-07-25 19:47:40 +00:00
}
// InLeapYear(t), https://tc39.es/ecma262/#eqn-InLeapYear
bool in_leap_year ( double t )
{
// +0𝔽 if DaysInYear(YearFromTime(t)) = 365𝔽
// 1𝔽 if DaysInYear(YearFromTime(t)) = 366𝔽
return days_in_year ( year_from_time ( t ) ) = = 366 ;
}
// MonthFromTime(t), https://tc39.es/ecma262/#eqn-MonthFromTime
u8 month_from_time ( double t )
{
auto in_leap_year = JS : : in_leap_year ( t ) ;
auto day_within_year = JS : : day_within_year ( t ) ;
// +0𝔽 if +0𝔽 ≤ DayWithinYear(t) < 31𝔽
if ( day_within_year < 31 )
return 0 ;
// 1𝔽 if 31𝔽 ≤ DayWithinYear(t) < 59𝔽 + InLeapYear(t)
if ( 31 < = day_within_year & & day_within_year < 59 + in_leap_year )
return 1 ;
// 2𝔽 if 59𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 90𝔽 + InLeapYear(t)
if ( 59 + in_leap_year < = day_within_year & & day_within_year < 90 + in_leap_year )
return 2 ;
// 3𝔽 if 90𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 120𝔽 + InLeapYear(t)
if ( 90 + in_leap_year < = day_within_year & & day_within_year < 120 + in_leap_year )
return 3 ;
// 4𝔽 if 120𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 151𝔽 + InLeapYear(t)
if ( 120 + in_leap_year < = day_within_year & & day_within_year < 151 + in_leap_year )
return 4 ;
// 5𝔽 if 151𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 181𝔽 + InLeapYear(t)
if ( 151 + in_leap_year < = day_within_year & & day_within_year < 181 + in_leap_year )
return 5 ;
// 6𝔽 if 181𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 212𝔽 + InLeapYear(t)
if ( 181 + in_leap_year < = day_within_year & & day_within_year < 212 + in_leap_year )
return 6 ;
// 7𝔽 if 212𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 243𝔽 + InLeapYear(t)
if ( 212 + in_leap_year < = day_within_year & & day_within_year < 243 + in_leap_year )
return 7 ;
// 8𝔽 if 243𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 273𝔽 + InLeapYear(t)
if ( 243 + in_leap_year < = day_within_year & & day_within_year < 273 + in_leap_year )
return 8 ;
// 9𝔽 if 273𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 304𝔽 + InLeapYear(t)
if ( 273 + in_leap_year < = day_within_year & & day_within_year < 304 + in_leap_year )
return 9 ;
// 10𝔽 if 304𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 334𝔽 + InLeapYear(t)
if ( 304 + in_leap_year < = day_within_year & & day_within_year < 334 + in_leap_year )
return 10 ;
// 11𝔽 if 334𝔽 + InLeapYear(t) ≤ DayWithinYear(t) < 365𝔽 + InLeapYear(t)
if ( 334 + in_leap_year < = day_within_year & & day_within_year < 365 + in_leap_year )
return 11 ;
VERIFY_NOT_REACHED ( ) ;
}
// HourFromTime(t), https://tc39.es/ecma262/#eqn-HourFromTime
u8 hour_from_time ( double t )
{
2022-01-16 06:15:46 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return 0 ;
2021-07-25 19:47:40 +00:00
// 𝔽 (floor(ℝ (t / msPerHour)) modulo HoursPerDay)
2022-05-06 17:10:34 +00:00
return static_cast < u8 > ( modulo ( floor ( t / ms_per_hour ) , hours_per_day ) ) ;
2021-07-25 19:47:40 +00:00
}
// MinFromTime(t), https://tc39.es/ecma262/#eqn-MinFromTime
u8 min_from_time ( double t )
{
2022-01-16 06:15:46 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return 0 ;
2021-07-25 19:47:40 +00:00
// 𝔽 (floor(ℝ (t / msPerMinute)) modulo MinutesPerHour)
2022-05-06 17:10:34 +00:00
return static_cast < u8 > ( modulo ( floor ( t / ms_per_minute ) , minutes_per_hour ) ) ;
2021-07-25 19:47:40 +00:00
}
// SecFromTime(t), https://tc39.es/ecma262/#eqn-SecFromTime
u8 sec_from_time ( double t )
{
2022-01-16 06:15:46 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return 0 ;
2021-07-25 19:47:40 +00:00
// 𝔽 (floor(ℝ (t / msPerSecond)) modulo SecondsPerMinute)
2022-05-06 17:10:34 +00:00
return static_cast < u8 > ( modulo ( floor ( t / ms_per_second ) , seconds_per_minute ) ) ;
2021-07-25 19:47:40 +00:00
}
// msFromTime(t), https://tc39.es/ecma262/#eqn-msFromTime
u16 ms_from_time ( double t )
{
2022-01-16 06:15:46 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return 0 ;
2022-05-03 19:23:57 +00:00
// 𝔽 (ℝ (t) modulo ℝ (msPerSecond))
2022-05-06 17:10:34 +00:00
return static_cast < u16 > ( modulo ( t , ms_per_second ) ) ;
2021-07-25 19:47:40 +00:00
}
2021-12-06 17:59:52 +00:00
// 21.4.1.6 Week Day, https://tc39.es/ecma262/#sec-week-day
u8 week_day ( double t )
{
2022-01-16 06:15:46 +00:00
if ( ! Value ( t ) . is_finite_number ( ) )
return 0 ;
2021-12-06 17:59:52 +00:00
// 𝔽 (ℝ (Day(t) + 4𝔽 ) modulo 7)
2021-12-21 20:22:38 +00:00
return static_cast < u8 > ( modulo ( day ( t ) + 4 , 7 ) ) ;
2021-12-06 17:59:52 +00:00
}
2022-10-14 13:23:43 +00:00
// 21.4.1.7 GetUTCEpochNanoseconds ( year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/ecma262/#sec-getutcepochnanoseconds
Crypto : : SignedBigInteger get_utc_epoch_nanoseconds ( i32 year , u8 month , u8 day , u8 hour , u8 minute , u8 second , u16 millisecond , u16 microsecond , u16 nanosecond )
{
// 1. Let date be MakeDay(𝔽 (year), 𝔽 (month - 1), 𝔽 (day)).
auto date = make_day ( year , month - 1 , day ) ;
// 2. Let time be MakeTime(𝔽 (hour), 𝔽 (minute), 𝔽 (second), 𝔽 (millisecond)).
auto time = make_time ( hour , minute , second , millisecond ) ;
// 3. Let ms be MakeDate(date, time).
auto ms = make_date ( date , time ) ;
// 4. Assert: ms is an integral Number.
VERIFY ( ms = = trunc ( ms ) ) ;
// 5. Return ℤ (ℝ (ms) × 10^6 + microsecond × 10^3 + nanosecond).
auto result = Crypto : : SignedBigInteger { ms } . multiplied_by ( s_one_million_bigint ) ;
result = result . plus ( Crypto : : SignedBigInteger { static_cast < i32 > ( microsecond ) } . multiplied_by ( s_one_thousand_bigint ) ) ;
result = result . plus ( Crypto : : SignedBigInteger { static_cast < i32 > ( nanosecond ) } ) ;
return result ;
}
static i64 clip_bigint_to_sane_time ( Crypto : : SignedBigInteger const & value )
{
static Crypto : : SignedBigInteger const min_bigint { NumericLimits < i64 > : : min ( ) } ;
static Crypto : : SignedBigInteger const max_bigint { NumericLimits < i64 > : : max ( ) } ;
2023-03-13 15:30:34 +00:00
// The provided epoch (nano)seconds value is potentially out of range for AK::Duration and subsequently
2022-10-14 13:23:43 +00:00
// 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_bigint )
return NumericLimits < i64 > : : min ( ) ;
if ( value > max_bigint )
return NumericLimits < i64 > : : max ( ) ;
// FIXME: Can we do this without string conversion?
2023-01-13 15:52:17 +00:00
return value . to_base_deprecated ( 10 ) . to_int < i64 > ( ) . value ( ) ;
2022-10-14 13:23:43 +00:00
}
// 21.4.1.8 GetNamedTimeZoneEpochNanoseconds ( timeZoneIdentifier, year, month, day, hour, minute, second, millisecond, microsecond, nanosecond ), https://tc39.es/ecma262/#sec-getnamedtimezoneepochnanoseconds
Vector < Crypto : : SignedBigInteger > get_named_time_zone_epoch_nanoseconds ( StringView time_zone_identifier , i32 year , u8 month , u8 day , u8 hour , u8 minute , u8 second , u16 millisecond , u16 microsecond , u16 nanosecond )
{
auto local_nanoseconds = get_utc_epoch_nanoseconds ( year , month , day , hour , minute , second , millisecond , microsecond , nanosecond ) ;
2023-03-13 21:28:08 +00:00
auto local_time = UnixDateTime : : from_nanoseconds_since_epoch ( clip_bigint_to_sane_time ( local_nanoseconds ) ) ;
2022-10-14 13:23:43 +00:00
// FIXME: LibTimeZone does not behave exactly as the spec expects. It does not consider repeated or skipped time points.
auto offset = TimeZone : : get_time_zone_offset ( time_zone_identifier , local_time ) ;
// Can only fail if the time zone identifier is invalid, which cannot be the case here.
VERIFY ( offset . has_value ( ) ) ;
2022-10-15 20:40:27 +00:00
return { local_nanoseconds . minus ( Crypto : : SignedBigInteger { offset - > seconds } . multiplied_by ( s_one_billion_bigint ) ) } ;
2022-10-14 13:23:43 +00:00
}
// 21.4.1.9 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds ), https://tc39.es/ecma262/#sec-getnamedtimezoneoffsetnanoseconds
i64 get_named_time_zone_offset_nanoseconds ( StringView time_zone_identifier , Crypto : : SignedBigInteger const & epoch_nanoseconds )
{
// Only called with validated time zone identifier as argument.
auto time_zone = TimeZone : : time_zone_from_string ( time_zone_identifier ) ;
VERIFY ( time_zone . has_value ( ) ) ;
2023-03-13 21:28:08 +00:00
// Since UnixDateTime::from_seconds_since_epoch() and UnixDateTime::from_nanoseconds_since_epoch() both take an i64, converting to
2022-10-14 13:23:43 +00:00
// seconds first gives us a greater range. The TZDB doesn't have sub-second offsets.
auto seconds = epoch_nanoseconds . divided_by ( s_one_billion_bigint ) . quotient ;
2023-03-13 21:28:08 +00:00
auto time = UnixDateTime : : from_seconds_since_epoch ( clip_bigint_to_sane_time ( seconds ) ) ;
2022-10-14 13:23:43 +00:00
auto offset = TimeZone : : get_time_zone_offset ( * time_zone , time ) ;
VERIFY ( offset . has_value ( ) ) ;
return offset - > seconds * 1'000'000'000 ;
}
// 21.4.1.10 DefaultTimeZone ( ), https://tc39.es/ecma262/#sec-defaulttimezone
2022-11-05 15:36:42 +00:00
// 6.4.3 DefaultTimeZone ( ), https://tc39.es/ecma402/#sup-defaulttimezone
2022-10-14 13:23:43 +00:00
StringView default_time_zone ( )
{
return TimeZone : : current_time_zone ( ) ;
}
// 21.4.1.11 LocalTime ( t ), https://tc39.es/ecma262/#sec-localtime
2022-01-14 18:10:07 +00:00
double local_time ( double time )
{
2022-10-14 13:23:43 +00:00
// 1. Let localTimeZone be DefaultTimeZone().
auto local_time_zone = default_time_zone ( ) ;
double offset_nanoseconds { 0 } ;
// 2. If IsTimeZoneOffsetString(localTimeZone) is true, then
if ( is_time_zone_offset_string ( local_time_zone ) ) {
// a. Let offsetNs be ParseTimeZoneOffsetString(localTimeZone).
offset_nanoseconds = parse_time_zone_offset_string ( local_time_zone ) ;
}
// 3. Else,
else {
// a. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(localTimeZone, ℤ (ℝ (t) × 10^6)).
auto time_bigint = Crypto : : SignedBigInteger { time } . multiplied_by ( s_one_million_bigint ) ;
offset_nanoseconds = get_named_time_zone_offset_nanoseconds ( local_time_zone , time_bigint ) ;
}
// 4. Let offsetMs be truncate(offsetNs / 10^6).
auto offset_milliseconds = trunc ( offset_nanoseconds / 1e6 ) ;
// 5. Return t + 𝔽 (offsetMs).
return time + offset_milliseconds ;
2022-01-14 18:10:07 +00:00
}
2022-10-14 13:23:43 +00:00
// 21.4.1.12 UTC ( t ), https://tc39.es/ecma262/#sec-utc-t
2022-01-14 18:10:07 +00:00
double utc_time ( double time )
{
2022-10-14 13:23:43 +00:00
// 1. Let localTimeZone be DefaultTimeZone().
auto local_time_zone = default_time_zone ( ) ;
double offset_nanoseconds { 0 } ;
// 2. If IsTimeZoneOffsetString(localTimeZone) is true, then
if ( is_time_zone_offset_string ( local_time_zone ) ) {
// a. Let offsetNs be ParseTimeZoneOffsetString(localTimeZone).
offset_nanoseconds = parse_time_zone_offset_string ( local_time_zone ) ;
}
// 3. Else,
else {
// a. Let possibleInstants be GetNamedTimeZoneEpochNanoseconds(localTimeZone, ℝ (YearFromTime(t)), ℝ (MonthFromTime(t)) + 1, ℝ (DateFromTime(t)), ℝ (HourFromTime(t)), ℝ (MinFromTime(t)), ℝ (SecFromTime(t)), ℝ (msFromTime(t)), 0, 0).
auto possible_instants = get_named_time_zone_epoch_nanoseconds ( local_time_zone , year_from_time ( time ) , month_from_time ( time ) + 1 , date_from_time ( time ) , hour_from_time ( time ) , min_from_time ( time ) , sec_from_time ( time ) , ms_from_time ( time ) , 0 , 0 ) ;
// b. NOTE: The following steps ensure that when t represents local time repeating multiple times at a negative time zone transition (e.g. when the daylight saving time ends or the time zone offset is decreased due to a time zone rule change) or skipped local time at a positive time zone transition (e.g. when the daylight saving time starts or the time zone offset is increased due to a time zone rule change), t is interpreted using the time zone offset before the transition.
Crypto : : SignedBigInteger disambiguated_instant ;
// c. If possibleInstants is not empty, then
if ( ! possible_instants . is_empty ( ) ) {
// i. Let disambiguatedInstant be possibleInstants[0].
disambiguated_instant = move ( possible_instants . first ( ) ) ;
}
// d. Else,
else {
// i. NOTE: t represents a local time skipped at a positive time zone transition (e.g. due to daylight saving time starting or a time zone rule change increasing the UTC offset).
// ii. Let possibleInstantsBefore be GetNamedTimeZoneEpochNanoseconds(localTimeZone, ℝ (YearFromTime(tBefore)), ℝ (MonthFromTime(tBefore)) + 1, ℝ (DateFromTime(tBefore)), ℝ (HourFromTime(tBefore)), ℝ (MinFromTime(tBefore)), ℝ (SecFromTime(tBefore)), ℝ (msFromTime(tBefore)), 0, 0), where tBefore is the largest integral Number < t for which possibleInstantsBefore is not empty (i.e., tBefore represents the last local time before the transition).
// iii. Let disambiguatedInstant be the last element of possibleInstantsBefore.
// FIXME: This branch currently cannot be reached with our implementation, because LibTimeZone does not handle skipped time points.
// When GetNamedTimeZoneEpochNanoseconds is updated to use a LibTimeZone API which does handle them, implement these steps.
VERIFY_NOT_REACHED ( ) ;
}
// e. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(localTimeZone, disambiguatedInstant).
offset_nanoseconds = get_named_time_zone_offset_nanoseconds ( local_time_zone , disambiguated_instant ) ;
}
// 4. Let offsetMs be truncate(offsetNs / 10^6).
auto offset_milliseconds = trunc ( offset_nanoseconds / 1e6 ) ;
// 5. Return t - 𝔽 (offsetMs).
return time - offset_milliseconds ;
2022-01-14 18:10:07 +00:00
}
2022-10-14 13:23:43 +00:00
// 21.4.1.14 MakeTime ( hour, min, sec, ms ), https://tc39.es/ecma262/#sec-maketime
2022-05-06 18:45:25 +00:00
double make_time ( double hour , double min , double sec , double ms )
2021-07-11 18:04:11 +00:00
{
// 1. If hour is not finite or min is not finite or sec is not finite or ms is not finite, return NaN.
2022-05-06 18:45:25 +00:00
if ( ! isfinite ( hour ) | | ! isfinite ( min ) | | ! isfinite ( sec ) | | ! isfinite ( ms ) )
return NAN ;
2021-07-11 18:04:11 +00:00
// 2. Let h be 𝔽 (! ToIntegerOrInfinity(hour)).
2022-05-06 18:45:25 +00:00
auto h = to_integer_or_infinity ( hour ) ;
2021-07-11 18:04:11 +00:00
// 3. Let m be 𝔽 (! ToIntegerOrInfinity(min)).
2022-05-06 18:45:25 +00:00
auto m = to_integer_or_infinity ( min ) ;
2021-07-11 18:04:11 +00:00
// 4. Let s be 𝔽 (! ToIntegerOrInfinity(sec)).
2022-05-06 18:45:25 +00:00
auto s = to_integer_or_infinity ( sec ) ;
2021-07-11 18:04:11 +00:00
// 5. Let milli be 𝔽 (! ToIntegerOrInfinity(ms)).
2022-05-06 18:45:25 +00:00
auto milli = to_integer_or_infinity ( ms ) ;
2021-07-11 18:04:11 +00:00
// 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: C++ arithmetic abides by IEEE 754 rules
2022-05-06 17:10:34 +00:00
auto t = ( ( h * ms_per_hour + m * ms_per_minute ) + s * ms_per_second ) + milli ;
2021-07-11 18:04:11 +00:00
// 7. Return t.
2022-05-06 18:45:25 +00:00
return t ;
2021-07-11 18:04:11 +00:00
}
2021-07-25 19:47:40 +00:00
// Day(t), https://tc39.es/ecma262/#eqn-Day
double day ( double time_value )
2021-07-11 18:04:11 +00:00
{
2022-05-06 17:10:34 +00:00
return floor ( time_value / ms_per_day ) ;
2021-07-11 18:04:11 +00:00
}
2022-01-14 18:10:07 +00:00
// TimeWithinDay(t), https://tc39.es/ecma262/#eqn-TimeWithinDay
double time_within_day ( double time )
{
// 𝔽 (ℝ (t) modulo ℝ (msPerDay))
2022-05-06 17:10:34 +00:00
return modulo ( time , ms_per_day ) ;
2022-01-14 18:10:07 +00:00
}
2022-10-14 13:23:43 +00:00
// 21.4.1.15 MakeDay ( year, month, date ), https://tc39.es/ecma262/#sec-makeday
2022-05-06 18:45:25 +00:00
double make_day ( double year , double month , double date )
2021-07-11 18:04:11 +00:00
{
// 1. If year is not finite or month is not finite or date is not finite, return NaN.
2022-05-06 18:45:25 +00:00
if ( ! isfinite ( year ) | | ! isfinite ( month ) | | ! isfinite ( date ) )
return NAN ;
2021-07-11 18:04:11 +00:00
// 2. Let y be 𝔽 (! ToIntegerOrInfinity(year)).
2022-05-06 18:45:25 +00:00
auto y = to_integer_or_infinity ( year ) ;
2021-07-11 18:04:11 +00:00
// 3. Let m be 𝔽 (! ToIntegerOrInfinity(month)).
2022-05-06 18:45:25 +00:00
auto m = to_integer_or_infinity ( month ) ;
2021-07-11 18:04:11 +00:00
// 4. Let dt be 𝔽 (! ToIntegerOrInfinity(date)).
2022-05-06 18:45:25 +00:00
auto dt = to_integer_or_infinity ( date ) ;
2021-07-11 18:04:11 +00:00
// 5. Let ym be y + 𝔽 (floor(ℝ (m) / 12)).
2022-01-05 17:26:15 +00:00
auto ym = y + floor ( m / 12 ) ;
2021-07-11 18:04:11 +00:00
// 6. If ym is not finite, return NaN.
2022-05-06 18:45:25 +00:00
if ( ! isfinite ( ym ) )
return NAN ;
2021-07-11 18:04:11 +00:00
// 7. Let mn be 𝔽 (ℝ (m) modulo 12).
2022-01-05 17:26:15 +00:00
auto mn = modulo ( m , 12 ) ;
2021-07-11 18:04:11 +00:00
// 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.
2022-01-05 17:26:15 +00:00
if ( ! AK : : is_within_range < int > ( ym ) | | ! AK : : is_within_range < int > ( mn + 1 ) )
2022-05-06 18:45:25 +00:00
return NAN ;
2023-01-02 18:51:15 +00:00
auto t = days_since_epoch ( static_cast < int > ( ym ) , static_cast < int > ( mn ) + 1 , 1 ) * ms_per_day ;
2022-01-05 17:26:15 +00:00
2021-07-11 18:04:11 +00:00
// 9. Return Day(t) + dt - 1𝔽 .
2022-05-06 18:45:25 +00:00
return day ( static_cast < double > ( t ) ) + dt - 1 ;
2021-07-11 18:04:11 +00:00
}
2022-10-14 13:23:43 +00:00
// 21.4.1.16 MakeDate ( day, time ), https://tc39.es/ecma262/#sec-makedate
2022-05-06 18:45:25 +00:00
double make_date ( double day , double time )
2021-07-11 18:04:11 +00:00
{
// 1. If day is not finite or time is not finite, return NaN.
2022-05-06 18:45:25 +00:00
if ( ! isfinite ( day ) | | ! isfinite ( time ) )
return NAN ;
2021-07-11 18:04:11 +00:00
// 2. Let tv be day × msPerDay + time.
2022-05-06 18:45:25 +00:00
auto tv = day * ms_per_day + time ;
2021-07-11 18:04:11 +00:00
// 3. If tv is not finite, return NaN.
2022-05-06 18:45:25 +00:00
if ( ! isfinite ( tv ) )
return NAN ;
2021-07-11 18:04:11 +00:00
// 4. Return tv.
return tv ;
}
2022-10-14 13:23:43 +00:00
// 21.4.1.17 TimeClip ( time ), https://tc39.es/ecma262/#sec-timeclip
2022-05-06 18:45:25 +00:00
double time_clip ( double time )
2021-12-04 23:55:59 +00:00
{
// 1. If time is not finite, return NaN.
2022-05-06 18:45:25 +00:00
if ( ! isfinite ( time ) )
return NAN ;
2021-12-04 23:55:59 +00:00
// 2. If abs(ℝ (time)) > 8.64 × 10^15, return NaN.
2022-05-06 18:45:25 +00:00
if ( fabs ( time ) > 8.64E15 )
return NAN ;
2021-12-04 23:55:59 +00:00
// 3. Return 𝔽 (! ToIntegerOrInfinity(time)).
2022-05-06 18:45:25 +00:00
return to_integer_or_infinity ( time ) ;
2021-12-04 23:55:59 +00:00
}
2022-10-14 13:23:43 +00:00
// 21.4.1.19.1 IsTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-istimezoneoffsetstring
bool is_time_zone_offset_string ( StringView offset_string )
{
// 1. Let parseResult be ParseText(StringToCodePoints(offsetString), UTCOffset).
auto parse_result = Temporal : : parse_iso8601 ( Temporal : : Production : : TimeZoneNumericUTCOffset , offset_string ) ;
// 2. If parseResult is a List of errors, return false.
// 3. Return true.
return parse_result . has_value ( ) ;
}
// 21.4.1.19.2 ParseTimeZoneOffsetString ( offsetString ), https://tc39.es/ecma262/#sec-parsetimezoneoffsetstring
double parse_time_zone_offset_string ( StringView offset_string )
{
// 1. Let parseResult be ParseText(StringToCodePoints(offsetString), UTCOffset).
auto parse_result = Temporal : : parse_iso8601 ( Temporal : : Production : : TimeZoneNumericUTCOffset , offset_string ) ;
// 2. Assert: parseResult is not a List of errors.
VERIFY ( parse_result . has_value ( ) ) ;
// 3. Assert: parseResult contains a TemporalSign Parse Node.
VERIFY ( parse_result - > time_zone_utc_offset_sign . has_value ( ) ) ;
// 4. Let parsedSign be the source text matched by the TemporalSign Parse Node contained within parseResult.
auto parsed_sign = * parse_result - > time_zone_utc_offset_sign ;
i8 sign { 0 } ;
// 5. If parsedSign is the single code point U+002D (HYPHEN-MINUS) or U+2212 (MINUS SIGN), then
if ( parsed_sign . is_one_of ( " - " sv , " \xE2 \x88 \x92 " sv ) ) {
// a. Let sign be -1.
sign = - 1 ;
}
// 6. Else,
else {
// a. Let sign be 1.
sign = 1 ;
}
// 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.
VERIFY ( parse_result - > time_zone_utc_offset_hour . has_value ( ) ) ;
// 9. Let parsedHours be the source text matched by the Hour Parse Node contained within parseResult.
auto parsed_hours = * parse_result - > time_zone_utc_offset_hour ;
// 10. Let hours be ℝ (StringToNumber(CodePointsToString(parsedHours))).
2023-03-01 14:48:12 +00:00
auto hours = string_to_number ( parsed_hours ) ;
2022-10-14 13:23:43 +00:00
double minutes { 0 } ;
double seconds { 0 } ;
double nanoseconds { 0 } ;
// 11. If parseResult does not contain a MinuteSecond Parse Node, then
if ( ! parse_result - > time_zone_utc_offset_minute . has_value ( ) ) {
// a. Let minutes be 0.
minutes = 0 ;
}
// 12. Else,
else {
// a. Let parsedMinutes be the source text matched by the first MinuteSecond Parse Node contained within parseResult.
auto parsed_minutes = * parse_result - > time_zone_utc_offset_minute ;
// b. Let minutes be ℝ (StringToNumber(CodePointsToString(parsedMinutes))).
2023-03-01 14:48:12 +00:00
minutes = string_to_number ( parsed_minutes ) ;
2022-10-14 13:23:43 +00:00
}
// 13. If parseResult does not contain two MinuteSecond Parse Nodes, then
if ( ! parse_result - > time_zone_utc_offset_second . has_value ( ) ) {
// a. Let seconds be 0.
seconds = 0 ;
}
// 14. Else,
else {
// a. Let parsedSeconds be the source text matched by the second secondSecond Parse Node contained within parseResult.
auto parsed_seconds = * parse_result - > time_zone_utc_offset_second ;
// b. Let seconds be ℝ (StringToNumber(CodePointsToString(parsedSeconds))).
2023-03-01 14:48:12 +00:00
seconds = string_to_number ( parsed_seconds ) ;
2022-10-14 13:23:43 +00:00
}
// 15. If parseResult does not contain a TemporalDecimalFraction Parse Node, then
if ( ! parse_result - > time_zone_utc_offset_fraction . has_value ( ) ) {
// a. Let nanoseconds be 0.
nanoseconds = 0 ;
}
// 16. Else,
else {
// a. Let parsedFraction be the source text matched by the TemporalDecimalFraction Parse Node contained within parseResult.
auto parsed_fraction = * parse_result - > time_zone_utc_offset_fraction ;
// b. Let fraction be the string-concatenation of CodePointsToString(parsedFraction) and "000000000".
2022-12-04 18:02:33 +00:00
auto fraction = DeprecatedString : : formatted ( " {}000000000 " , parsed_fraction ) ;
2022-10-14 13:23:43 +00:00
// c. Let nanosecondsString be the substring of fraction from 1 to 10.
auto nanoseconds_string = fraction . substring_view ( 1 , 9 ) ;
// d. Let nanoseconds be ℝ (StringToNumber(nanosecondsString)).
2023-03-01 14:48:12 +00:00
nanoseconds = string_to_number ( nanoseconds_string ) ;
2022-10-14 13:23:43 +00:00
}
// 17. Return sign × (((hours × 60 + minutes) × 60 + seconds) × 10^9 + nanoseconds).
// NOTE: Using scientific notation (1e9) ensures the result of this expression is a double,
// which is important - otherwise it's all integers and the result overflows!
return sign * ( ( ( hours * 60 + minutes ) * 60 + seconds ) * 1e9 + nanoseconds ) ;
}
2020-03-29 23:21:56 +00:00
}