mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-09-03 16:16:43 +00:00
LibJS+LibUnicode: Support ambiguous time zone transitions
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macOS, macos-15, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, Linux, blacksmith-16vcpu-ubuntu-2404, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, Linux, blacksmith-16vcpu-ubuntu-2404, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, Linux, blacksmith-16vcpu-ubuntu-2404, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macOS, macOS-arm64, macos-15) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, Linux, Linux-x86_64, blacksmith-8vcpu-ubuntu-2404) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
Some checks are pending
CI / Lagom (arm64, Sanitizer_CI, false, macOS, macos-15, Clang) (push) Waiting to run
CI / Lagom (x86_64, Fuzzers_CI, false, Linux, blacksmith-16vcpu-ubuntu-2404, Clang) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, false, Linux, blacksmith-16vcpu-ubuntu-2404, GNU) (push) Waiting to run
CI / Lagom (x86_64, Sanitizer_CI, true, Linux, blacksmith-16vcpu-ubuntu-2404, Clang) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (arm64, macOS, macOS-arm64, macos-15) (push) Waiting to run
Package the js repl as a binary artifact / build-and-package (x86_64, Linux, Linux-x86_64, blacksmith-8vcpu-ubuntu-2404) (push) Waiting to run
Run test262 and test-wasm / run_and_update_results (push) Waiting to run
Lint Code / lint (push) Waiting to run
Label PRs with merge conflicts / auto-labeler (push) Waiting to run
Push notes / build (push) Waiting to run
For example, time zone transitions can result in repeated or skipped wall times. Temporal wants us to handle these transitions.
This commit is contained in:
parent
c8b4dc4847
commit
8145572180
Notes:
github-actions[bot]
2025-06-02 21:10:26 +00:00
Author: https://github.com/trflynn89
Commit: 8145572180
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4966
4 changed files with 92 additions and 12 deletions
|
@ -403,13 +403,15 @@ Vector<Crypto::SignedBigInteger> get_named_time_zone_epoch_nanoseconds(StringVie
|
||||||
auto local_nanoseconds = get_utc_epoch_nanoseconds(iso_date_time);
|
auto local_nanoseconds = get_utc_epoch_nanoseconds(iso_date_time);
|
||||||
auto local_time = UnixDateTime::from_nanoseconds_since_epoch(clip_bigint_to_sane_time(local_nanoseconds));
|
auto local_time = UnixDateTime::from_nanoseconds_since_epoch(clip_bigint_to_sane_time(local_nanoseconds));
|
||||||
|
|
||||||
// FIXME: LibUnicode does not behave exactly as the spec expects. It does not consider repeated or skipped time points.
|
auto offsets = Unicode::disambiguated_time_zone_offsets(time_zone_identifier, local_time);
|
||||||
auto offset = Unicode::time_zone_offset(time_zone_identifier, local_time);
|
|
||||||
|
|
||||||
// Can only fail if the time zone identifier is invalid, which cannot be the case here.
|
Vector<Crypto::SignedBigInteger> result;
|
||||||
VERIFY(offset.has_value());
|
result.ensure_capacity(offsets.size());
|
||||||
|
|
||||||
return { local_nanoseconds.minus(Crypto::SignedBigInteger { offset->offset.to_nanoseconds() }) };
|
for (auto const& offset : offsets)
|
||||||
|
result.unchecked_append(local_nanoseconds.minus(Crypto::SignedBigInteger { offset.offset.to_nanoseconds() }));
|
||||||
|
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds ), https://tc39.es/ecma262/#sec-getnamedtimezoneoffsetnanoseconds
|
// 21.4.1.21 GetNamedTimeZoneOffsetNanoseconds ( timeZoneIdentifier, epochNanoseconds ), https://tc39.es/ecma262/#sec-getnamedtimezoneoffsetnanoseconds
|
||||||
|
|
|
@ -68,7 +68,7 @@ describe("correct behavior", () => {
|
||||||
expect(result).toBe(-1);
|
expect(result).toBe(-1);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("sub-minute time zone offset", () => {
|
test("sub-minute time zone offset (unambiguous time zone transition)", () => {
|
||||||
const duration1 = new Temporal.Duration(0, 0, 0, 31);
|
const duration1 = new Temporal.Duration(0, 0, 0, 31);
|
||||||
const duration2 = new Temporal.Duration(0, 1);
|
const duration2 = new Temporal.Duration(0, 1);
|
||||||
|
|
||||||
|
@ -82,6 +82,26 @@ describe("correct behavior", () => {
|
||||||
});
|
});
|
||||||
expect(result).toBe(0);
|
expect(result).toBe(0);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("sub-minute time zone offset (ambiguous time zone transition)", () => {
|
||||||
|
const duration1 = new Temporal.Duration(0, 0, 0, 0, 24);
|
||||||
|
const duration2 = new Temporal.Duration(0, 0, 0, 1);
|
||||||
|
|
||||||
|
let result = Temporal.Duration.compare(duration1, duration2, {
|
||||||
|
relativeTo: "1952-10-15T23:59:59-11:19:40[Pacific/Niue]",
|
||||||
|
});
|
||||||
|
expect(result).toBe(-1);
|
||||||
|
|
||||||
|
result = Temporal.Duration.compare(duration1, duration2, {
|
||||||
|
relativeTo: "1952-10-15T23:59:59-11:20[Pacific/Niue]",
|
||||||
|
});
|
||||||
|
expect(result).toBe(-1);
|
||||||
|
|
||||||
|
result = Temporal.Duration.compare(duration1, duration2, {
|
||||||
|
relativeTo: "1952-10-15T23:59:59-11:20:00[Pacific/Niue]",
|
||||||
|
});
|
||||||
|
expect(result).toBe(0);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("errors", () => {
|
describe("errors", () => {
|
||||||
|
@ -148,7 +168,7 @@ describe("errors", () => {
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("sub-minute time zone offset mismatch", () => {
|
test("sub-minute time zone offset mismatch (unambiguous time zone transition)", () => {
|
||||||
const duration1 = new Temporal.Duration(0, 0, 0, 31);
|
const duration1 = new Temporal.Duration(0, 0, 0, 31);
|
||||||
const duration2 = new Temporal.Duration(0, 1);
|
const duration2 = new Temporal.Duration(0, 1);
|
||||||
|
|
||||||
|
@ -170,4 +190,18 @@ describe("errors", () => {
|
||||||
"Invalid offset for the provided date and time in the current time zone"
|
"Invalid offset for the provided date and time in the current time zone"
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("sub-minute time zone offset mismatch (ambiguous time zone transition)", () => {
|
||||||
|
const duration1 = new Temporal.Duration(0, 0, 0, 0, 24);
|
||||||
|
const duration2 = new Temporal.Duration(0, 0, 0, 1);
|
||||||
|
|
||||||
|
expect(() => {
|
||||||
|
Temporal.Duration.compare(duration1, duration2, {
|
||||||
|
relativeTo: "1952-10-15T23:59:59-11:19:50[Pacific/Niue]",
|
||||||
|
});
|
||||||
|
}).toThrowWithMessage(
|
||||||
|
RangeError,
|
||||||
|
"Invalid offset for the provided date and time in the current time zone"
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include <LibUnicode/ICU.h>
|
#include <LibUnicode/ICU.h>
|
||||||
#include <LibUnicode/TimeZone.h>
|
#include <LibUnicode/TimeZone.h>
|
||||||
|
|
||||||
|
#include <unicode/basictz.h>
|
||||||
#include <unicode/timezone.h>
|
#include <unicode/timezone.h>
|
||||||
#include <unicode/ucal.h>
|
#include <unicode/ucal.h>
|
||||||
|
|
||||||
|
@ -139,6 +140,15 @@ Optional<String> resolve_primary_time_zone(StringView time_zone)
|
||||||
return icu_string_to_string(iana_id);
|
return icu_string_to_string(iana_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static UDate to_icu_time(UnixDateTime time)
|
||||||
|
{
|
||||||
|
// We must clamp the time we provide to ICU such that the result of converting milliseconds to days fits in an i32.
|
||||||
|
// Further, that conversion must still be valid after applying DST offsets to the time we provide.
|
||||||
|
static constexpr auto min_time = (static_cast<UDate>(AK::NumericLimits<i32>::min()) + U_MILLIS_PER_DAY) * U_MILLIS_PER_DAY;
|
||||||
|
static constexpr auto max_time = (static_cast<UDate>(AK::NumericLimits<i32>::max()) - U_MILLIS_PER_DAY) * U_MILLIS_PER_DAY;
|
||||||
|
return clamp(static_cast<UDate>(time.milliseconds_since_epoch()), min_time, max_time);
|
||||||
|
}
|
||||||
|
|
||||||
Optional<TimeZoneOffset> time_zone_offset(StringView time_zone, UnixDateTime time)
|
Optional<TimeZoneOffset> time_zone_offset(StringView time_zone, UnixDateTime time)
|
||||||
{
|
{
|
||||||
UErrorCode status = U_ZERO_ERROR;
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
|
@ -150,11 +160,7 @@ Optional<TimeZoneOffset> time_zone_offset(StringView time_zone, UnixDateTime tim
|
||||||
i32 raw_offset = 0;
|
i32 raw_offset = 0;
|
||||||
i32 dst_offset = 0;
|
i32 dst_offset = 0;
|
||||||
|
|
||||||
// We must clamp the time we provide to ICU such that the result of converting milliseconds to days fits in an i32.
|
auto icu_time = to_icu_time(time);
|
||||||
// Further, that conversion must still be valid after applying DST offsets to the time we provide.
|
|
||||||
static constexpr auto min_time = (static_cast<UDate>(AK::NumericLimits<i32>::min()) + U_MILLIS_PER_DAY) * U_MILLIS_PER_DAY;
|
|
||||||
static constexpr auto max_time = (static_cast<UDate>(AK::NumericLimits<i32>::max()) - U_MILLIS_PER_DAY) * U_MILLIS_PER_DAY;
|
|
||||||
auto icu_time = clamp(static_cast<UDate>(time.milliseconds_since_epoch()), min_time, max_time);
|
|
||||||
|
|
||||||
time_zone_data->time_zone().getOffset(icu_time, 0, raw_offset, dst_offset, status);
|
time_zone_data->time_zone().getOffset(icu_time, 0, raw_offset, dst_offset, status);
|
||||||
if (icu_failure(status))
|
if (icu_failure(status))
|
||||||
|
@ -166,4 +172,41 @@ Optional<TimeZoneOffset> time_zone_offset(StringView time_zone, UnixDateTime tim
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Vector<TimeZoneOffset> disambiguated_time_zone_offsets(StringView time_zone, UnixDateTime time)
|
||||||
|
{
|
||||||
|
UErrorCode status = U_ZERO_ERROR;
|
||||||
|
|
||||||
|
auto time_zone_data = TimeZoneData::for_time_zone(time_zone);
|
||||||
|
if (!time_zone_data.has_value())
|
||||||
|
return {};
|
||||||
|
|
||||||
|
auto& basic_time_zone = as<icu::BasicTimeZone>(time_zone_data->time_zone());
|
||||||
|
auto icu_time = to_icu_time(time);
|
||||||
|
|
||||||
|
auto get_offset = [&](auto disambiguation_option) -> Optional<TimeZoneOffset> {
|
||||||
|
i32 raw_offset = 0;
|
||||||
|
i32 dst_offset = 0;
|
||||||
|
|
||||||
|
basic_time_zone.getOffsetFromLocal(icu_time, disambiguation_option, disambiguation_option, raw_offset, dst_offset, status);
|
||||||
|
if (icu_failure(status))
|
||||||
|
return {};
|
||||||
|
|
||||||
|
return TimeZoneOffset {
|
||||||
|
.offset = AK::Duration::from_milliseconds(raw_offset + dst_offset),
|
||||||
|
.in_dst = dst_offset == 0 ? TimeZoneOffset::InDST::No : TimeZoneOffset::InDST::Yes,
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
auto former = get_offset(UCAL_TZ_LOCAL_FORMER);
|
||||||
|
auto latter = get_offset(UCAL_TZ_LOCAL_LATTER);
|
||||||
|
|
||||||
|
Vector<TimeZoneOffset> offsets;
|
||||||
|
if (former.has_value())
|
||||||
|
offsets.append(*former);
|
||||||
|
if (latter.has_value() && latter->offset != former->offset)
|
||||||
|
offsets.append(*latter);
|
||||||
|
|
||||||
|
return offsets;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -31,5 +31,6 @@ Vector<String> const& available_time_zones();
|
||||||
Vector<String> available_time_zones_in_region(StringView region);
|
Vector<String> available_time_zones_in_region(StringView region);
|
||||||
Optional<String> resolve_primary_time_zone(StringView time_zone);
|
Optional<String> resolve_primary_time_zone(StringView time_zone);
|
||||||
Optional<TimeZoneOffset> time_zone_offset(StringView time_zone, UnixDateTime time);
|
Optional<TimeZoneOffset> time_zone_offset(StringView time_zone, UnixDateTime time);
|
||||||
|
Vector<TimeZoneOffset> disambiguated_time_zone_offsets(StringView time_zone, UnixDateTime time);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue