Truly independent web browser
LibJS: Use the pre-transition offset for local Date values
sideshowbarkerd9a936731616
diff --git a/Libraries/LibJS/Rust/src/runtime/date.rs b/Libraries/LibJS/Rust/src/runtime/date.rsindex f8fc1765db3..19b87ce4392 100644--- a/Libraries/LibJS/Rust/src/runtime/date.rs+++ b/Libraries/LibJS/Rust/src/runtime/date.rs@@ -29,14 +29,15 @@ use crate::runtime::intl::abstract_operations::get_available_named_time_zone_ideuse crate::runtime::object::MayInterfereWithIndexedPropertyAccess;use crate::runtime::realm::Realm;use crate::runtime::temporal::instant::{- NANOSECONDS_PER_DAY, NANOSECONDS_PER_MICROSECOND, NANOSECONDS_PER_MILLISECOND, NANOSECONDS_PER_SECOND,+ 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::plain_date_time::time_value_to_iso_date_time_record;-use crate::runtime::temporal::time_zone::{get_named_time_zone_next_transition, parse_time_zone_identifier};+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, UnixDateTime};+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`.@@ -517,7 +518,7 @@ pub fn get_named_time_zone_offset_nanoseconds(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).expect("offset.has_value()")+ 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@@ -529,7 +530,7 @@ pub fn get_named_time_zone_offset_milliseconds(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).expect("offset.has_value()")+ 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.@@ -677,44 +678,13 @@ pub fn utc_time(time: f64) -> f64 {}// 5. Else,else {- // a. Let isoDateTime be TimeValueToISODateTimeRecord(t).- let iso_date_time = time_value_to_iso_date_time_record(time);-- // b. Let possibleInstants be GetNamedTimeZoneEpochNanoseconds(systemTimeZoneIdentifier, isoDateTime).- let possible_instants = get_named_time_zone_epoch_nanoseconds(system_time_zone_identifier, &iso_date_time);-- // c. 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.- let disambiguated_instant = if let Some(first) = possible_instants.into_iter().next() {- // d. If possibleInstants is not empty, then- // i. Let disambiguatedInstant be possibleInstants[0].- first- }- // e. 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 {- // 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(systemTimeZoneIdentifier, TimeValueToISODateTimeRecord(tBefore)),- // 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).- // NB: We implement this by finding the next UTC offset transition after one day before the skipped time,- // which is guaranteed to be before the gap. The last valid instant before the transition is one- // nanosecond before the transition instant.- let epoch_nanoseconds = get_utc_epoch_nanoseconds(&iso_date_time);- let day_before = epoch_nanoseconds - NANOSECONDS_PER_DAY.clone();- let transition = get_named_time_zone_next_transition(system_time_zone_identifier, &day_before)- .expect("transition.has_value()");-- // iii. Let disambiguatedInstant be the last element of possibleInstantsBefore.- transition - 1+ return f64::NAN;};-- // f. Let offsetNs be GetNamedTimeZoneOffsetNanoseconds(systemTimeZoneIdentifier, disambiguatedInstant).- let offset = get_named_time_zone_offset_nanoseconds(system_time_zone_identifier, &disambiguated_instant);offset.offset_nanoseconds as f64};diff --git a/Libraries/LibJS/Rust/src/unicode/time_zone.rs b/Libraries/LibJS/Rust/src/unicode/time_zone.rsindex 345d91be57e..619054db512 100644--- a/Libraries/LibJS/Rust/src/unicode/time_zone.rs+++ b/Libraries/LibJS/Rust/src/unicode/time_zone.rs@@ -32,6 +32,7 @@ unsafe extern "C" {length: usize,seconds: i64,nanoseconds: u32,+ is_local: bool,offset_nanoseconds: *mut i64,in_dst: *mut bool,) -> bool;@@ -179,8 +180,17 @@ pub fn resolve_primary_time_zone(time_zone: Utf16View<'_>) -> Option<Utf16Stringresolved.then(|| Utf16String::from_utf16(&primary))}+pub enum TimeZoneTime {+ Utc,+ Local,+}+/// Unicode::time_zone_offset: the offset from UTC of `time_zone` at `time`.-pub fn time_zone_offset(time_zone: Utf16View<'_>, time: UnixDateTime) -> Option<TimeZoneOffset> {+pub fn time_zone_offset(+ time_zone: Utf16View<'_>,+ time: UnixDateTime,+ time_type: TimeZoneTime,+) -> Option<TimeZoneOffset> {let time_zone = code_units_of(time_zone);let mut offset_nanoseconds = 0;let mut in_dst = false;@@ -191,6 +201,7 @@ pub fn time_zone_offset(time_zone: Utf16View<'_>, time: UnixDateTime) -> Option<time_zone.len(),time.seconds,time.nanoseconds,+ matches!(time_type, TimeZoneTime::Local),&raw mut offset_nanoseconds,&raw mut in_dst,)diff --git a/Libraries/LibUnicode/TextMapping.h b/Libraries/LibUnicode/TextMapping.hindex 7e22dcbf45f..92515e51586 100644--- a/Libraries/LibUnicode/TextMapping.h+++ b/Libraries/LibUnicode/TextMapping.h@@ -35,7 +35,7 @@ void unicode_current_time_zone(UnicodeTextMappingOutput);bool unicode_set_current_time_zone(u16 const* time_zone, size_t length);void unicode_available_time_zones(void* context, void (*append)(void*, u16 const*, size_t));bool unicode_resolve_primary_time_zone(u16 const* time_zone, size_t length, UnicodeTextMappingOutput);-bool unicode_time_zone_offset(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, i64* offset_nanoseconds, bool* in_dst);+bool unicode_time_zone_offset(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, bool is_local, i64* offset_nanoseconds, bool* in_dst);size_t unicode_disambiguated_time_zone_offsets(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, i64* offset_nanoseconds, bool* in_dst, size_t capacity);bool unicode_time_zone_transition(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, u8 direction, bool include_given_time, u8 transition_rule, i64* transition_milliseconds);bool unicode_time_zone_display_name(u8 const* locale, size_t locale_length, u8 const* time_zone, size_t time_zone_length, bool in_dst, double time, UnicodeTextMappingOutput);diff --git a/Libraries/LibUnicode/TimeZone.cpp b/Libraries/LibUnicode/TimeZone.cppindex 64c50d4b6d2..949c3d12e98 100644--- a/Libraries/LibUnicode/TimeZone.cpp+++ b/Libraries/LibUnicode/TimeZone.cpp@@ -199,7 +199,7 @@ static UDate to_icu_time(UnixDateTime time)return clamp(static_cast<UDate>(time.milliseconds_since_epoch()), min_time, max_time);}-Optional<TimeZoneOffset> time_zone_offset(Utf16View time_zone, UnixDateTime time)+Optional<TimeZoneOffset> time_zone_offset(Utf16View time_zone, UnixDateTime time, TimeZoneTime time_type){UErrorCode status = U_ZERO_ERROR;@@ -212,7 +212,10 @@ Optional<TimeZoneOffset> time_zone_offset(Utf16View time_zone, UnixDateTime timeauto icu_time = to_icu_time(time);- time_zone_data->time_zone().getOffset(icu_time, 0, raw_offset, dst_offset, status);+ if (time_type == TimeZoneTime::Local)+ as<icu::BasicTimeZone>(time_zone_data->time_zone()).getOffsetFromLocal(icu_time, UCAL_TZ_LOCAL_FORMER, UCAL_TZ_LOCAL_FORMER, raw_offset, dst_offset, status);+ else+ time_zone_data->time_zone().getOffset(icu_time, false, raw_offset, dst_offset, status);if (icu_failure(status))return {};@@ -363,9 +366,9 @@ extern "C" bool unicode_resolve_primary_time_zone(u16 const* time_zone, size_t lreturn true;}-extern "C" bool unicode_time_zone_offset(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, i64* offset_nanoseconds, bool* in_dst)+extern "C" bool unicode_time_zone_offset(u16 const* time_zone, size_t length, i64 seconds, u32 nanoseconds, bool is_local, i64* offset_nanoseconds, bool* in_dst){- auto offset = Unicode::time_zone_offset(Utf16View { reinterpret_cast<char16_t const*>(time_zone), length }, unix_date_time_from_parts(seconds, nanoseconds));+ auto offset = Unicode::time_zone_offset(Utf16View { reinterpret_cast<char16_t const*>(time_zone), length }, unix_date_time_from_parts(seconds, nanoseconds), is_local ? Unicode::TimeZoneTime::Local : Unicode::TimeZoneTime::UTC);if (!offset.has_value())return false;*offset_nanoseconds = offset->offset.to_nanoseconds();diff --git a/Libraries/LibUnicode/TimeZone.h b/Libraries/LibUnicode/TimeZone.hindex 8f8eb2d78e4..1f41658ca81 100644--- a/Libraries/LibUnicode/TimeZone.h+++ b/Libraries/LibUnicode/TimeZone.h@@ -57,7 +57,9 @@ void clear_system_time_zone_cache();Vector<Utf16String> const& available_time_zones();Vector<Utf16String> available_time_zones_in_region(Utf16View region);Optional<Utf16String> resolve_primary_time_zone(Utf16View time_zone);-Optional<TimeZoneOffset> time_zone_offset(Utf16View time_zone, UnixDateTime time);+enum class TimeZoneTime { UTC,+ Local };+Optional<TimeZoneOffset> time_zone_offset(Utf16View time_zone, UnixDateTime time, TimeZoneTime = TimeZoneTime::UTC);Vector<TimeZoneOffset> disambiguated_time_zone_offsets(Utf16View time_zone, UnixDateTime time);Optional<TimeZoneTransition> get_time_zone_transition(Utf16View time_zone, UnixDateTime time, TimeZoneTransition::Options options);diff --git a/Tests/LibJS/Runtime/builtins/Date/Date.js b/Tests/LibJS/Runtime/builtins/Date/Date.jsindex c8a9b29af04..c6c3d222c84 100644--- a/Tests/LibJS/Runtime/builtins/Date/Date.js+++ b/Tests/LibJS/Runtime/builtins/Date/Date.js@@ -117,3 +117,13 @@ test("toString shows the UTC offset of the time value, not of the local time", (setTimeZone(originalTimeZone);});++test("local time in a gap or overlap in 2500", () => {+ const originalTimeZone = setTimeZone("Australia/Sydney");+ try {+ expect(new Date(2500, 3, 4, 2, 30).toISOString()).toBe("2500-04-03T15:30:00.000Z");+ expect(new Date(2500, 9, 3, 2, 30).toISOString()).toBe("2500-10-02T16:30:00.000Z");+ } finally {+ setTimeZone(originalTimeZone);+ }+});