|
@@ -11,11 +11,16 @@
|
|
|
#include <LibJS/Runtime/AbstractOperations.h>
|
|
|
#include <LibJS/Runtime/Date.h>
|
|
|
#include <LibJS/Runtime/GlobalObject.h>
|
|
|
+#include <LibJS/Runtime/Temporal/ISO8601.h>
|
|
|
#include <LibTimeZone/TimeZone.h>
|
|
|
#include <time.h>
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
+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 };
|
|
|
+
|
|
|
Date* Date::create(Realm& realm, double date_value)
|
|
|
{
|
|
|
return realm.heap().allocate<Date>(realm, date_value, *realm.intrinsics().date_prototype());
|
|
@@ -265,6 +270,7 @@ u8 week_day(double t)
|
|
|
}
|
|
|
|
|
|
|
|
|
+
|
|
|
double local_tza(double time, [[maybe_unused]] bool is_utc, Optional<StringView> time_zone_override)
|
|
|
{
|
|
|
|
|
@@ -285,21 +291,160 @@ double local_tza(double time, [[maybe_unused]] bool is_utc, Optional<StringView>
|
|
|
return maybe_offset.has_value() ? static_cast<double>(maybe_offset->seconds) * 1000 : 0;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
+Crypto::SignedBigInteger get_utc_epoch_nanoseconds(i32 year, u8 month, u8 day, u8 hour, u8 minute, u8 second, u16 millisecond, u16 microsecond, u16 nanosecond)
|
|
|
+{
|
|
|
+
|
|
|
+ auto date = make_day(year, month - 1, day);
|
|
|
+
|
|
|
+
|
|
|
+ auto time = make_time(hour, minute, second, millisecond);
|
|
|
+
|
|
|
+
|
|
|
+ auto ms = make_date(date, time);
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY(ms == trunc(ms));
|
|
|
+
|
|
|
+
|
|
|
+ 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() };
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ if (value < min_bigint)
|
|
|
+ return NumericLimits<i64>::min();
|
|
|
+ if (value > max_bigint)
|
|
|
+ return NumericLimits<i64>::max();
|
|
|
+
|
|
|
+
|
|
|
+ return value.to_base(10).to_int<i64>().value();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+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);
|
|
|
+ auto local_time = Time::from_nanoseconds(clip_bigint_to_sane_time(local_nanoseconds));
|
|
|
+
|
|
|
+
|
|
|
+ auto offset = TimeZone::get_time_zone_offset(time_zone_identifier, local_time);
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY(offset.has_value());
|
|
|
+
|
|
|
+ return { local_nanoseconds.plus(Crypto::SignedBigInteger { offset->seconds }.multiplied_by(s_one_billion_bigint)) };
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+i64 get_named_time_zone_offset_nanoseconds(StringView time_zone_identifier, Crypto::SignedBigInteger const& epoch_nanoseconds)
|
|
|
+{
|
|
|
+
|
|
|
+ auto time_zone = TimeZone::time_zone_from_string(time_zone_identifier);
|
|
|
+ VERIFY(time_zone.has_value());
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ auto seconds = epoch_nanoseconds.divided_by(s_one_billion_bigint).quotient;
|
|
|
+ auto time = Time::from_seconds(clip_bigint_to_sane_time(seconds));
|
|
|
+
|
|
|
+ auto offset = TimeZone::get_time_zone_offset(*time_zone, time);
|
|
|
+ VERIFY(offset.has_value());
|
|
|
+
|
|
|
+ return offset->seconds * 1'000'000'000;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+StringView default_time_zone()
|
|
|
+{
|
|
|
+ return TimeZone::current_time_zone();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
double local_time(double time)
|
|
|
{
|
|
|
-
|
|
|
- return time + local_tza(time, true);
|
|
|
+
|
|
|
+ auto local_time_zone = default_time_zone();
|
|
|
+
|
|
|
+ double offset_nanoseconds { 0 };
|
|
|
+
|
|
|
+
|
|
|
+ if (is_time_zone_offset_string(local_time_zone)) {
|
|
|
+
|
|
|
+ offset_nanoseconds = parse_time_zone_offset_string(local_time_zone);
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ 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);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ auto offset_milliseconds = trunc(offset_nanoseconds / 1e6);
|
|
|
+
|
|
|
+
|
|
|
+ return time + offset_milliseconds;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
double utc_time(double time)
|
|
|
{
|
|
|
-
|
|
|
- return time - local_tza(time, false);
|
|
|
+
|
|
|
+ auto local_time_zone = default_time_zone();
|
|
|
+
|
|
|
+ double offset_nanoseconds { 0 };
|
|
|
+
|
|
|
+
|
|
|
+ if (is_time_zone_offset_string(local_time_zone)) {
|
|
|
+
|
|
|
+ offset_nanoseconds = parse_time_zone_offset_string(local_time_zone);
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ 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);
|
|
|
+
|
|
|
+
|
|
|
+ Crypto::SignedBigInteger disambiguated_instant;
|
|
|
+
|
|
|
+
|
|
|
+ if (!possible_instants.is_empty()) {
|
|
|
+
|
|
|
+ disambiguated_instant = move(possible_instants.first());
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY_NOT_REACHED();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ offset_nanoseconds = get_named_time_zone_offset_nanoseconds(local_time_zone, disambiguated_instant);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ auto offset_milliseconds = trunc(offset_nanoseconds / 1e6);
|
|
|
+
|
|
|
+
|
|
|
+ return time - offset_milliseconds;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
double make_time(double hour, double min, double sec, double ms)
|
|
|
{
|
|
|
|
|
@@ -334,7 +479,7 @@ double time_within_day(double time)
|
|
|
return modulo(time, ms_per_day);
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
double make_day(double year, double month, double date)
|
|
|
{
|
|
|
|
|
@@ -367,7 +512,7 @@ double make_day(double year, double month, double date)
|
|
|
return day(static_cast<double>(t)) + dt - 1;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
double make_date(double day, double time)
|
|
|
{
|
|
|
|
|
@@ -385,7 +530,7 @@ double make_date(double day, double time)
|
|
|
return tv;
|
|
|
}
|
|
|
|
|
|
-
|
|
|
+
|
|
|
double time_clip(double time)
|
|
|
{
|
|
|
|
|
@@ -400,4 +545,111 @@ double time_clip(double time)
|
|
|
return to_integer_or_infinity(time);
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+bool is_time_zone_offset_string(StringView offset_string)
|
|
|
+{
|
|
|
+
|
|
|
+ auto parse_result = Temporal::parse_iso8601(Temporal::Production::TimeZoneNumericUTCOffset, offset_string);
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return parse_result.has_value();
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+double parse_time_zone_offset_string(StringView offset_string)
|
|
|
+{
|
|
|
+
|
|
|
+ auto parse_result = Temporal::parse_iso8601(Temporal::Production::TimeZoneNumericUTCOffset, offset_string);
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY(parse_result.has_value());
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY(parse_result->time_zone_utc_offset_sign.has_value());
|
|
|
+
|
|
|
+
|
|
|
+ auto parsed_sign = *parse_result->time_zone_utc_offset_sign;
|
|
|
+ i8 sign { 0 };
|
|
|
+
|
|
|
+
|
|
|
+ if (parsed_sign.is_one_of("-"sv, "\xE2\x88\x92"sv)) {
|
|
|
+
|
|
|
+ sign = -1;
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ sign = 1;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ VERIFY(parse_result->time_zone_utc_offset_hour.has_value());
|
|
|
+
|
|
|
+
|
|
|
+ auto parsed_hours = *parse_result->time_zone_utc_offset_hour;
|
|
|
+
|
|
|
+
|
|
|
+ auto hours = string_to_number(parsed_hours)->as_double();
|
|
|
+
|
|
|
+ double minutes { 0 };
|
|
|
+ double seconds { 0 };
|
|
|
+ double nanoseconds { 0 };
|
|
|
+
|
|
|
+
|
|
|
+ if (!parse_result->time_zone_utc_offset_minute.has_value()) {
|
|
|
+
|
|
|
+ minutes = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ auto parsed_minutes = *parse_result->time_zone_utc_offset_minute;
|
|
|
+
|
|
|
+
|
|
|
+ minutes = string_to_number(parsed_minutes)->as_double();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (!parse_result->time_zone_utc_offset_second.has_value()) {
|
|
|
+
|
|
|
+ seconds = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ auto parsed_seconds = *parse_result->time_zone_utc_offset_second;
|
|
|
+
|
|
|
+
|
|
|
+ seconds = string_to_number(parsed_seconds)->as_double();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ if (!parse_result->time_zone_utc_offset_fraction.has_value()) {
|
|
|
+
|
|
|
+ nanoseconds = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ else {
|
|
|
+
|
|
|
+ auto parsed_fraction = *parse_result->time_zone_utc_offset_fraction;
|
|
|
+
|
|
|
+
|
|
|
+ auto fraction = String::formatted("{}000000000", parsed_fraction);
|
|
|
+
|
|
|
+
|
|
|
+ auto nanoseconds_string = fraction.substring_view(1, 9);
|
|
|
+
|
|
|
+
|
|
|
+ nanoseconds = string_to_number(nanoseconds_string)->as_double();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ return sign * (((hours * 60 + minutes) * 60 + seconds) * 1e9 + nanoseconds);
|
|
|
+}
|
|
|
+
|
|
|
}
|