1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 10:37:45 +00:00

LibJS+LibTimeZone+LibUnicode: Indicate whether a time zone is in DST

Return whether the time zone is in DST during the provided time from
TimeZone::get_time_zone_offset,
This commit is contained in:
Timothy Flynn 2022-01-19 14:18:02 -05:00 committed by Linus Groh
parent 42c9c57141
commit 70f49d0696
7 changed files with 86 additions and 69 deletions

View file

@ -286,7 +286,8 @@ double local_tza(double time, [[maybe_unused]] bool is_utc, Optional<StringView>
auto time_since_epoch = Value(time).is_finite_number() ? AK::Time::from_milliseconds(time) : AK::Time::max();
auto maybe_offset = TimeZone::get_time_zone_offset(time_zone, time_since_epoch);
return maybe_offset.value_or(0) * 1000;
return maybe_offset.has_value() ? static_cast<double>(maybe_offset->seconds) * 1000 : 0;
}
// 21.4.1.8 LocalTime ( t ), https://tc39.es/ecma262/#sec-localtime

View file

@ -175,10 +175,10 @@ i64 get_iana_time_zone_offset_nanoseconds(BigInt const& epoch_nanoseconds, Strin
else
time = Time::from_seconds(*seconds.to_base(10).to_int<i64>());
auto offset_seconds = ::TimeZone::get_time_zone_offset(*time_zone, time);
VERIFY(offset_seconds.has_value());
auto offset = ::TimeZone::get_time_zone_offset(*time_zone, time);
VERIFY(offset.has_value());
return *offset_seconds * 1'000'000'000;
return offset->seconds * 1'000'000'000;
}
// 11.6.5 GetIANATimeZoneNextTransition ( epochNanoseconds, timeZoneIdentifier ), https://tc39.es/proposal-temporal/#sec-temporal-getianatimezonenexttransition

View file

@ -66,17 +66,17 @@ Optional<StringView> canonicalize_time_zone(StringView time_zone)
Optional<DaylightSavingsRule> __attribute__((weak)) daylight_savings_rule_from_string(StringView) { return {}; }
StringView __attribute__((weak)) daylight_savings_rule_to_string(DaylightSavingsRule) { return {}; }
Optional<i64> __attribute__((weak)) get_time_zone_offset([[maybe_unused]] TimeZone time_zone, AK::Time)
Optional<Offset> __attribute__((weak)) get_time_zone_offset([[maybe_unused]] TimeZone time_zone, AK::Time)
{
#if !ENABLE_TIME_ZONE_DATA
VERIFY(time_zone == TimeZone::UTC);
return 0;
return Offset {};
#else
return {};
#endif
}
Optional<i64> get_time_zone_offset(StringView time_zone, AK::Time time)
Optional<Offset> get_time_zone_offset(StringView time_zone, AK::Time time)
{
if (auto maybe_time_zone = time_zone_from_string(time_zone); maybe_time_zone.has_value())
return get_time_zone_offset(*maybe_time_zone, time);

View file

@ -14,6 +14,16 @@
namespace TimeZone {
enum class InDST {
No,
Yes,
};
struct Offset {
i64 seconds { 0 };
InDST in_dst { InDST::No };
};
StringView current_time_zone();
Optional<TimeZone> time_zone_from_string(StringView time_zone);
@ -23,7 +33,7 @@ Optional<StringView> canonicalize_time_zone(StringView time_zone);
Optional<DaylightSavingsRule> daylight_savings_rule_from_string(StringView daylight_savings_rule);
StringView daylight_savings_rule_to_string(DaylightSavingsRule daylight_savings_rule);
Optional<i64> get_time_zone_offset(TimeZone time_zone, AK::Time time);
Optional<i64> get_time_zone_offset(StringView time_zone, AK::Time time);
Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::Time time);
Optional<Offset> get_time_zone_offset(StringView time_zone, AK::Time time);
}

View file

@ -208,21 +208,21 @@ static Optional<String> format_time_zone_offset(StringView locale, StringView ti
if (!number_system.has_value())
return {};
auto offset_seconds = TimeZone::get_time_zone_offset(time_zone, time);
if (!offset_seconds.has_value())
auto offset = TimeZone::get_time_zone_offset(time_zone, time);
if (!offset.has_value())
return {};
if (*offset_seconds == 0)
if (offset->seconds == 0)
return formats->gmt_zero_format;
auto sign = *offset_seconds > 0 ? formats->symbol_ahead_sign : formats->symbol_behind_sign;
auto separator = *offset_seconds > 0 ? formats->symbol_ahead_separator : formats->symbol_behind_separator;
*offset_seconds = llabs(*offset_seconds);
auto sign = offset->seconds > 0 ? formats->symbol_ahead_sign : formats->symbol_behind_sign;
auto separator = offset->seconds > 0 ? formats->symbol_ahead_separator : formats->symbol_behind_separator;
offset->seconds = llabs(offset->seconds);
auto offset_hours = *offset_seconds / 3'600;
*offset_seconds %= 3'600;
auto offset_hours = offset->seconds / 3'600;
offset->seconds %= 3'600;
auto offset_minutes = *offset_seconds / 60;
*offset_seconds %= 60;
auto offset_minutes = offset->seconds / 60;
offset->seconds %= 60;
StringBuilder builder;
builder.append(sign);
@ -231,8 +231,8 @@ static Optional<String> format_time_zone_offset(StringView locale, StringView ti
// The long format always uses 2-digit hours field and minutes field, with optional 2-digit seconds field.
case CalendarPatternStyle::LongOffset:
builder.appendff("{:02}{}{:02}", offset_hours, separator, offset_minutes);
if (*offset_seconds > 0)
builder.appendff("{}{:02}", separator, *offset_seconds);
if (offset->seconds > 0)
builder.appendff("{}{:02}", separator, offset->seconds);
break;
// The short format is intended for the shortest representation and uses hour fields without leading zero, with optional 2-digit minutes and seconds fields.
@ -240,8 +240,8 @@ static Optional<String> format_time_zone_offset(StringView locale, StringView ti
builder.appendff("{}", offset_hours);
if (offset_minutes > 0) {
builder.appendff("{}{:02}", separator, offset_minutes);
if (*offset_seconds > 0)
builder.appendff("{}{:02}", separator, *offset_seconds);
if (offset->seconds > 0)
builder.appendff("{}{:02}", separator, offset->seconds);
}
break;
@ -250,8 +250,8 @@ static Optional<String> format_time_zone_offset(StringView locale, StringView ti
}
// The digits used for hours, minutes and seconds fields in this format are the locale's default decimal digits.
auto offset = replace_digits_for_number_system(*number_system, builder.build());
return formats->gmt_format.replace("{0}"sv, offset);
auto result = replace_digits_for_number_system(*number_system, builder.build());
return formats->gmt_format.replace("{0}"sv, result);
}
// https://unicode.org/reports/tr35/tr35-dates.html#Time_Zone_Format_Terminology