LibTimeZone: Remove LibTimeZone and TZDB data

All users have been ported to the ICU implementation.
This commit is contained in:
Timothy Flynn 2024-06-25 15:45:24 -04:00 committed by Andreas Kling
parent 672a555f98
commit bd73dd316d
Notes: sideshowbarker 2024-07-17 20:19:08 +09:00
15 changed files with 0 additions and 1586 deletions

View file

@ -68,14 +68,6 @@ runs:
CCACHE_DIR=${{ inputs.serenity_ccache_path }} ccache -s
CCACHE_DIR=${{ inputs.serenity_ccache_path }} ccache -z
- name: 'TimeZoneData cache'
uses: actions/cache@v4
with:
path: ${{ inputs.download_cache_path }}/TZDB
key: TimeZoneData-${{ hashFiles('Meta/CMake/time_zone_data.cmake') }}-${{ steps.date-stamp.outputs.timestamp }}
restore-keys: |
TimeZoneData-${{ hashFiles('Meta/CMake/time_zone_data.cmake') }}
- name: Export vcpkg GitHub Actions cache environment variables
uses: actions/github-script@v7
with:

View file

@ -16,7 +16,6 @@ serenity_option(ENABLE_ALL_DEBUG_FACILITIES OFF CACHE BOOL "Enable all noisy deb
serenity_option(ENABLE_ADOBE_ICC_PROFILES_DOWNLOAD ON CACHE BOOL "Enable download of Adobe's ICC profiles")
serenity_option(ENABLE_COMPILETIME_HEADER_CHECK OFF CACHE BOOL "Enable compiletime check that each library header compiles stand-alone")
serenity_option(ENABLE_TIME_ZONE_DATABASE_DOWNLOAD ON CACHE BOOL "Enable download of the IANA Time Zone Database at build time")
serenity_option(ENABLE_PUBLIC_SUFFIX_DOWNLOAD ON CACHE BOOL "Enable download of the Public Suffix List at build time")
serenity_option(INCLUDE_WASM_SPEC_TESTS OFF CACHE BOOL "Download and include the WebAssembly spec testsuite")
serenity_option(INCLUDE_FLAC_SPEC_TESTS OFF CACHE BOOL "Download and include the FLAC spec testsuite")

View file

@ -1,78 +0,0 @@
include(${CMAKE_CURRENT_LIST_DIR}/utils.cmake)
set(TZDB_VERSION "2024a")
set(TZDB_SHA256 "0d0434459acbd2059a7a8da1f3304a84a86591f6ed69c6248fffa502b6edffe3")
set(TZDB_PATH "${SERENITY_CACHE_DIR}/TZDB" CACHE PATH "Download location for TZDB files")
set(TZDB_VERSION_FILE "${TZDB_PATH}/version.txt")
set(TZDB_ZIP_URL "https://data.iana.org/time-zones/releases/tzdata${TZDB_VERSION}.tar.gz")
set(TZDB_ZIP_PATH "${TZDB_PATH}/tzdb.tar.gz")
set(TZDB_AFRICA_SOURCE africa)
set(TZDB_AFRICA_PATH "${TZDB_PATH}/${TZDB_AFRICA_SOURCE}")
set(TZDB_ANTARCTICA_SOURCE antarctica)
set(TZDB_ANTARCTICA_PATH "${TZDB_PATH}/${TZDB_ANTARCTICA_SOURCE}")
set(TZDB_ASIA_SOURCE asia)
set(TZDB_ASIA_PATH "${TZDB_PATH}/${TZDB_ASIA_SOURCE}")
set(TZDB_AUSTRALASIA_SOURCE australasia)
set(TZDB_AUSTRALASIA_PATH "${TZDB_PATH}/${TZDB_AUSTRALASIA_SOURCE}")
set(TZDB_BACKWARD_SOURCE backward)
set(TZDB_BACKWARD_PATH "${TZDB_PATH}/${TZDB_BACKWARD_SOURCE}")
set(TZDB_ETCETERA_SOURCE etcetera)
set(TZDB_ETCETERA_PATH "${TZDB_PATH}/${TZDB_ETCETERA_SOURCE}")
set(TZDB_EUROPE_SOURCE europe)
set(TZDB_EUROPE_PATH "${TZDB_PATH}/${TZDB_EUROPE_SOURCE}")
set(TZDB_NORTH_AMERICA_SOURCE northamerica)
set(TZDB_NORTH_AMERICA_PATH "${TZDB_PATH}/${TZDB_NORTH_AMERICA_SOURCE}")
set(TZDB_SOUTH_AMERICA_SOURCE southamerica)
set(TZDB_SOUTH_AMERICA_PATH "${TZDB_PATH}/${TZDB_SOUTH_AMERICA_SOURCE}")
set(TZDB_ZONE_1970_SOURCE zone1970.tab)
set(TZDB_ZONE_1970_PATH "${TZDB_PATH}/${TZDB_ZONE_1970_SOURCE}")
function(extract_tzdb_file source path)
extract_path("${TZDB_PATH}" "${TZDB_ZIP_PATH}" "${source}" "${path}")
endfunction()
if (ENABLE_TIME_ZONE_DATABASE_DOWNLOAD)
remove_path_if_version_changed("${TZDB_VERSION}" "${TZDB_VERSION_FILE}" "${TZDB_PATH}")
if (ENABLE_NETWORK_DOWNLOADS)
download_file("${TZDB_ZIP_URL}" "${TZDB_ZIP_PATH}" SHA256 "${TZDB_SHA256}")
extract_tzdb_file("${TZDB_AFRICA_SOURCE}" "${TZDB_AFRICA_PATH}")
extract_tzdb_file("${TZDB_ANTARCTICA_SOURCE}" "${TZDB_ANTARCTICA_PATH}")
extract_tzdb_file("${TZDB_ASIA_SOURCE}" "${TZDB_ASIA_PATH}")
extract_tzdb_file("${TZDB_AUSTRALASIA_SOURCE}" "${TZDB_AUSTRALASIA_PATH}")
extract_tzdb_file("${TZDB_BACKWARD_SOURCE}" "${TZDB_BACKWARD_PATH}")
extract_tzdb_file("${TZDB_ETCETERA_SOURCE}" "${TZDB_ETCETERA_PATH}")
extract_tzdb_file("${TZDB_EUROPE_SOURCE}" "${TZDB_EUROPE_PATH}")
extract_tzdb_file("${TZDB_NORTH_AMERICA_SOURCE}" "${TZDB_NORTH_AMERICA_PATH}")
extract_tzdb_file("${TZDB_SOUTH_AMERICA_SOURCE}" "${TZDB_SOUTH_AMERICA_PATH}")
extract_tzdb_file("${TZDB_ZONE_1970_SOURCE}" "${TZDB_ZONE_1970_PATH}")
else()
message(STATUS "Skipping download of ${TZDB_ZIP_URL}, expecting the archive to have been extracted to ${TZDB_PATH}")
endif()
invoke_generator(
"TimeZoneData"
Lagom::GenerateTimeZoneData
"${TZDB_VERSION_FILE}"
"TimeZoneData.h"
"TimeZoneData.cpp"
arguments -z "${TZDB_ZONE_1970_PATH}" "${TZDB_AFRICA_PATH}" "${TZDB_ANTARCTICA_PATH}" "${TZDB_ASIA_PATH}" "${TZDB_AUSTRALASIA_PATH}" "${TZDB_BACKWARD_PATH}" "${TZDB_ETCETERA_PATH}" "${TZDB_EUROPE_PATH}" "${TZDB_NORTH_AMERICA_PATH}" "${TZDB_SOUTH_AMERICA_PATH}"
)
set(TIME_ZONE_DATA_SOURCES
"TimeZoneData.h"
"TimeZoneData.cpp"
)
endif()

View file

@ -395,7 +395,6 @@ set(lagom_standard_libraries
Syntax
TextCodec
Threading
TimeZone
TLS
Unicode
URL
@ -508,7 +507,6 @@ if (BUILD_TESTING)
LibTest
LibTextCodec
LibTTF
LibTimeZone
LibUnicode
LibURL
LibWeb

View file

@ -1,6 +1,5 @@
add_subdirectory(IPCCompiler)
add_subdirectory(LibTextCodec)
add_subdirectory(LibTimeZone)
add_subdirectory(LibUnicode)
add_subdirectory(LibWeb)
add_subdirectory(LibWebView)

View file

@ -1 +0,0 @@
lagom_tool(GenerateTimeZoneData SOURCES GenerateTimeZoneData.cpp LIBS LibMain)

View file

@ -1,751 +0,0 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "../LibUnicode/GeneratorUtil.h" // FIXME: Move this somewhere common.
#include <AK/ByteString.h>
#include <AK/DateConstants.h>
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/HashMap.h>
#include <AK/SourceGenerator.h>
#include <AK/StringBuilder.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibTimeZone/TimeZone.h>
namespace {
struct DateTime {
u16 year { 0 };
Optional<u8> month;
Optional<u8> day;
Optional<u8> last_weekday;
Optional<u8> after_weekday;
Optional<u8> before_weekday;
Optional<u8> hour;
Optional<u8> minute;
Optional<u8> second;
};
struct TimeZoneOffset {
i64 offset { 0 };
Optional<DateTime> until;
Optional<ByteString> dst_rule;
Optional<i32> dst_rule_index;
i64 dst_offset { 0 };
size_t standard_format { 0 };
size_t daylight_format { 0 };
};
struct DaylightSavingsOffset {
i64 offset { 0 };
u16 year_from { 0 };
Optional<u16> year_to;
DateTime in_effect;
size_t format { 0 };
};
struct TimeZoneData {
UniqueStringStorage unique_strings;
HashMap<ByteString, Vector<TimeZoneOffset>> time_zones;
Vector<ByteString> time_zone_names;
Vector<Alias> time_zone_aliases;
HashMap<ByteString, Vector<DaylightSavingsOffset>> dst_offsets;
Vector<ByteString> dst_offset_names;
HashMap<ByteString, Vector<size_t>> time_zone_regions;
Vector<ByteString> time_zone_region_names;
Vector<TimeZone::TimeZoneIdentifier> time_zones_and_links;
};
}
template<>
struct AK::Formatter<DateTime> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, DateTime const& date_time)
{
return Formatter<FormatString>::format(builder,
"{{ {}, {}, {}, {}, {}, {}, {}, {}, {} }}"sv,
date_time.year,
date_time.month.value_or(1),
date_time.day.value_or(1),
date_time.last_weekday.value_or(0),
date_time.after_weekday.value_or(0),
date_time.before_weekday.value_or(0),
date_time.hour.value_or(0),
date_time.minute.value_or(0),
date_time.second.value_or(0));
}
};
template<>
struct AK::Formatter<TimeZoneOffset> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, TimeZoneOffset const& time_zone_offset)
{
return Formatter<FormatString>::format(builder,
"{{ {}, {}, {}, {}, {}, {}, {} }}"sv,
time_zone_offset.offset,
time_zone_offset.until.value_or({}),
time_zone_offset.until.has_value(),
time_zone_offset.dst_rule_index.value_or(-1),
time_zone_offset.dst_offset,
time_zone_offset.standard_format,
time_zone_offset.daylight_format);
}
};
template<>
struct AK::Formatter<DaylightSavingsOffset> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, DaylightSavingsOffset const& dst_offset)
{
auto format_time = [&](auto year) {
return ByteString::formatted("AK::UnixDateTime::from_unix_time_parts({}, 1, 1, 0, 0, 0, 0)", year);
};
static ByteString max_year_as_time("max_year_as_time"sv);
return Formatter<FormatString>::format(builder,
"{{ {}, {}, {}, {}, {} }}"sv,
dst_offset.offset,
format_time(dst_offset.year_from),
dst_offset.year_to.has_value()
? format_time(*dst_offset.year_to + 1)
: max_year_as_time,
dst_offset.in_effect,
dst_offset.format);
}
};
template<>
struct AK::Formatter<TimeZone::TimeZoneIdentifier> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, TimeZone::TimeZoneIdentifier const& time_zone)
{
return Formatter<FormatString>::format(builder,
"{{ \"{}\"sv, IsLink::{} }}"sv,
time_zone.name,
time_zone.is_link == TimeZone::IsLink::Yes ? "Yes"sv : "No"sv);
}
};
static Optional<DateTime> parse_date_time(ReadonlySpan<StringView> segments)
{
auto comment_index = find_index(segments.begin(), segments.end(), "#"sv);
if (comment_index != segments.size())
segments = segments.slice(0, comment_index);
if (segments.is_empty())
return {};
DateTime date_time {};
date_time.year = segments[0].to_number<unsigned>().value();
if (segments.size() > 1)
date_time.month = find_index(short_month_names.begin(), short_month_names.end(), segments[1]) + 1;
if (segments.size() > 2) {
if (segments[2].starts_with("last"sv)) {
auto weekday = segments[2].substring_view("last"sv.length());
date_time.last_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
} else if (auto index = segments[2].find(">="sv); index.has_value()) {
auto weekday = segments[2].substring_view(0, *index);
date_time.after_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
auto day = segments[2].substring_view(*index + ">="sv.length());
date_time.day = day.to_number<unsigned>().value();
} else if (auto index = segments[2].find("<="sv); index.has_value()) {
auto weekday = segments[2].substring_view(0, *index);
date_time.before_weekday = find_index(short_day_names.begin(), short_day_names.end(), weekday);
auto day = segments[2].substring_view(*index + "<="sv.length());
date_time.day = day.to_number<unsigned>().value();
} else {
date_time.day = segments[2].to_number<unsigned>().value();
}
}
if (segments.size() > 3) {
// FIXME: Some times end with a letter, e.g. "2:00u" and "2:00s". Figure out what this means and handle it.
auto time_segments = segments[3].split_view(':');
date_time.hour = time_segments[0].to_number<int>().value();
date_time.minute = time_segments.size() > 1 ? time_segments[1].substring_view(0, 2).to_number<unsigned>().value() : 0;
date_time.second = time_segments.size() > 2 ? time_segments[2].substring_view(0, 2).to_number<unsigned>().value() : 0;
}
return date_time;
}
static i64 parse_time_offset(StringView segment)
{
auto segments = segment.split_view(':');
i64 hours = segments[0].to_number<int>().value();
i64 minutes = segments.size() > 1 ? segments[1].to_number<unsigned>().value() : 0;
i64 seconds = segments.size() > 2 ? segments[2].to_number<unsigned>().value() : 0;
i64 sign = ((hours < 0) || (segments[0] == "-0"sv)) ? -1 : 1;
return (hours * 3600) + sign * ((minutes * 60) + seconds);
}
static void parse_dst_rule(StringView segment, TimeZoneOffset& time_zone)
{
if (segment.contains(':'))
time_zone.dst_offset = parse_time_offset(segment);
else if (segment != "-"sv)
time_zone.dst_rule = segment;
}
static void parse_format(StringView format, TimeZoneData& time_zone_data, TimeZoneOffset& time_zone)
{
auto formats = format.replace("%s"sv, "{}"sv, ReplaceMode::FirstOnly).split('/');
VERIFY(formats.size() <= 2);
time_zone.standard_format = time_zone_data.unique_strings.ensure(formats[0]);
if (formats.size() == 2)
time_zone.daylight_format = time_zone_data.unique_strings.ensure(formats[1]);
else
time_zone.daylight_format = time_zone.standard_format;
}
static Vector<TimeZoneOffset>& parse_zone(StringView zone_line, TimeZoneData& time_zone_data)
{
auto segments = zone_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
// "Zone" NAME STDOFF RULES FORMAT [UNTIL]
VERIFY(segments[0] == "Zone"sv);
auto name = segments[1];
TimeZoneOffset time_zone {};
time_zone.offset = parse_time_offset(segments[2]);
parse_dst_rule(segments[3], time_zone);
parse_format(segments[4], time_zone_data, time_zone);
if (segments.size() > 5)
time_zone.until = parse_date_time(segments.span().slice(5));
auto& time_zones = time_zone_data.time_zones.ensure(name);
time_zones.append(move(time_zone));
if (!time_zone_data.time_zone_names.contains_slow(name)) {
time_zone_data.time_zone_names.append(name);
time_zone_data.time_zones_and_links.append({ time_zone_data.time_zone_names.last(), TimeZone::IsLink::No });
}
return time_zones;
}
static void parse_zone_continuation(StringView zone_line, TimeZoneData& time_zone_data, Vector<TimeZoneOffset>& time_zones)
{
auto segments = zone_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
// STDOFF RULES FORMAT [UNTIL]
TimeZoneOffset time_zone {};
time_zone.offset = parse_time_offset(segments[0]);
parse_dst_rule(segments[1], time_zone);
parse_format(segments[2], time_zone_data, time_zone);
if (segments.size() > 3)
time_zone.until = parse_date_time(segments.span().slice(3));
time_zones.append(move(time_zone));
}
static void parse_link(StringView link_line, TimeZoneData& time_zone_data)
{
auto segments = link_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
// Link TARGET LINK-NAME
VERIFY(segments[0] == "Link"sv);
auto target = segments[1];
auto alias = segments[2];
time_zone_data.time_zone_aliases.append({ target, alias });
time_zone_data.time_zones_and_links.append({ time_zone_data.time_zone_aliases.last().alias, TimeZone::IsLink::Yes });
}
static void parse_rule(StringView rule_line, TimeZoneData& time_zone_data)
{
auto segments = rule_line.split_view_if([](char ch) { return (ch == '\t') || (ch == ' '); });
// Rule NAME FROM TO TYPE IN ON AT SAVE LETTER/S
VERIFY(segments[0] == "Rule"sv);
auto name = segments[1];
DaylightSavingsOffset dst_offset {};
dst_offset.offset = parse_time_offset(segments[8]);
dst_offset.year_from = segments[2].to_number<unsigned>().value();
if (segments[3] == "only")
dst_offset.year_to = dst_offset.year_from;
else if (segments[3] != "max"sv)
dst_offset.year_to = segments[3].to_number<unsigned>().value();
auto in_effect = Array { "0"sv, segments[5], segments[6], segments[7] };
dst_offset.in_effect = parse_date_time(in_effect).release_value();
if (segments[9] != "-"sv)
dst_offset.format = time_zone_data.unique_strings.ensure(segments[9]);
auto& dst_offsets = time_zone_data.dst_offsets.ensure(name);
dst_offsets.append(move(dst_offset));
if (!time_zone_data.dst_offset_names.contains_slow(name))
time_zone_data.dst_offset_names.append(name);
}
static ErrorOr<void> parse_time_zones(StringView time_zone_path, TimeZoneData& time_zone_data)
{
// For reference, the man page for `zic` has the best documentation of the TZDB file format.
auto file = TRY(open_file(time_zone_path, Core::File::OpenMode::Read));
Array<u8, 1024> buffer {};
Vector<TimeZoneOffset>* last_parsed_zone = nullptr;
while (TRY(file->can_read_line())) {
auto line = TRY(file->read_line(buffer));
if (line.is_empty() || line.trim_whitespace(TrimMode::Left).starts_with('#'))
continue;
if (line.starts_with("Zone"sv)) {
last_parsed_zone = &parse_zone(line, time_zone_data);
} else if (line.starts_with('\t')) {
VERIFY(last_parsed_zone != nullptr);
parse_zone_continuation(line, time_zone_data, *last_parsed_zone);
} else {
last_parsed_zone = nullptr;
if (line.starts_with("Link"sv))
parse_link(line, time_zone_data);
else if (line.starts_with("Rule"sv))
parse_rule(line, time_zone_data);
}
}
return {};
}
static ErrorOr<void> parse_time_zone_coordinates(Core::InputBufferedFile& file, TimeZoneData& time_zone_data)
{
Array<u8, 1024> buffer {};
while (TRY(file.can_read_line())) {
auto line = TRY(file.read_line(buffer));
if (line.is_empty() || line.trim_whitespace(TrimMode::Left).starts_with('#'))
continue;
auto segments = line.split_view('\t');
auto regions = segments[0];
auto zone = segments[2];
VERIFY(time_zone_data.time_zones.contains(zone));
TRY(regions.for_each_split_view(',', SplitBehavior::Nothing, [&](auto region) -> ErrorOr<void> {
auto index = time_zone_data.unique_strings.ensure(zone);
TRY(time_zone_data.time_zone_regions.ensure(region).try_append(index));
if (!time_zone_data.time_zone_region_names.contains_slow(region))
TRY(time_zone_data.time_zone_region_names.try_append(region));
return {};
}));
}
return {};
}
static void set_dst_rule_indices(TimeZoneData& time_zone_data)
{
for (auto& time_zone : time_zone_data.time_zones) {
for (auto& time_zone_offset : time_zone.value) {
if (!time_zone_offset.dst_rule.has_value())
continue;
auto dst_rule_index = time_zone_data.dst_offset_names.find_first_index(*time_zone_offset.dst_rule);
time_zone_offset.dst_rule_index = static_cast<i32>(dst_rule_index.value());
}
}
}
static ByteString format_identifier(StringView owner, ByteString identifier)
{
constexpr auto gmt_time_zones = Array { "Etc/GMT"sv, "GMT"sv };
for (auto gmt_time_zone : gmt_time_zones) {
if (identifier.starts_with(gmt_time_zone)) {
auto offset = identifier.substring_view(gmt_time_zone.length());
if (offset.starts_with('+'))
identifier = ByteString::formatted("{}_Ahead_{}", gmt_time_zone, offset.substring_view(1));
else if (offset.starts_with('-'))
identifier = ByteString::formatted("{}_Behind_{}", gmt_time_zone, offset.substring_view(1));
}
}
identifier = identifier.replace("-"sv, "_"sv, ReplaceMode::All);
identifier = identifier.replace("/"sv, "_"sv, ReplaceMode::All);
if (all_of(identifier, is_ascii_digit))
return ByteString::formatted("{}_{}", owner[0], identifier);
if (is_ascii_lower_alpha(identifier[0]))
return ByteString::formatted("{:c}{}", to_ascii_uppercase(identifier[0]), identifier.substring_view(1));
return identifier;
}
static ErrorOr<void> generate_time_zone_data_header(Core::InputBufferedFile& file, TimeZoneData& time_zone_data)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.append(R"~~~(
#pragma once
#include <AK/Types.h>
namespace TimeZone {
)~~~");
generate_enum(generator, format_identifier, "TimeZone"sv, {}, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases);
generate_enum(generator, format_identifier, "DaylightSavingsRule"sv, {}, time_zone_data.dst_offset_names);
generate_enum(generator, format_identifier, "Region"sv, {}, time_zone_data.time_zone_region_names);
generator.append(R"~~~(
}
)~~~");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};
}
static ErrorOr<void> generate_time_zone_data_implementation(Core::InputBufferedFile& file, TimeZoneData& time_zone_data)
{
StringBuilder builder;
SourceGenerator generator { builder };
generator.set("string_index_type"sv, time_zone_data.unique_strings.type_that_fits());
set_dst_rule_indices(time_zone_data);
generator.append(R"~~~(
#include <AK/Array.h>
#include <AK/BinarySearch.h>
#include <AK/Optional.h>
#include <AK/Span.h>
#include <AK/NumericLimits.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <LibTimeZone/TimeZone.h>
#include <LibTimeZone/TimeZoneData.h>
namespace TimeZone {
static constexpr auto max_year_as_time = AK::UnixDateTime::from_unix_time_parts(NumericLimits<u16>::max(), 1, 1, 0, 0, 0, 0);
struct DateTime {
AK::UnixDateTime time_since_epoch() const
{
// FIXME: This implementation does not take last_weekday, after_weekday, or before_weekday into account.
return AK::UnixDateTime::from_unix_time_parts(year, month, day, hour, minute, second, 0);
}
u16 year { 0 };
u8 month { 1 };
u8 day { 1 };
u8 last_weekday { 0 };
u8 after_weekday { 0 };
u8 before_weekday { 0 };
u8 hour { 0 };
u8 minute { 0 };
u8 second { 0 };
};
struct TimeZoneOffset {
i64 offset { 0 };
DateTime until {};
bool has_until { false };
i32 dst_rule { -1 };
i64 dst_offset { 0 };
@string_index_type@ standard_format { 0 };
@string_index_type@ daylight_format { 0 };
};
struct DaylightSavingsOffset {
AK::UnixDateTime time_in_effect(AK::UnixDateTime time) const
{
auto in_effect = this->in_effect;
in_effect.year = seconds_since_epoch_to_year(time.seconds_since_epoch());
return in_effect.time_since_epoch();
}
i64 offset { 0 };
AK::UnixDateTime year_from {};
AK::UnixDateTime year_to {};
DateTime in_effect {};
@string_index_type@ format { 0 };
};
)~~~");
time_zone_data.unique_strings.generate(generator);
auto append_offsets = [&](auto const& name, auto type, auto const& offsets) {
generator.set("name", name);
generator.set("type", type);
generator.set("size", ByteString::number(offsets.size()));
generator.append(R"~~~(
static constexpr Array<@type@, @size@> @name@ { {
)~~~");
for (auto const& offset : offsets)
generator.append(ByteString::formatted(" {},\n", offset));
generator.append("} };\n");
};
generate_mapping(generator, time_zone_data.time_zone_names, "TimeZoneOffset"sv, "s_time_zone_offsets"sv, "s_time_zone_offsets_{}"sv, format_identifier,
[&](auto const& name, auto const& value) {
auto const& time_zone_offsets = time_zone_data.time_zones.find(value)->value;
append_offsets(name, "TimeZoneOffset"sv, time_zone_offsets);
});
generate_mapping(generator, time_zone_data.dst_offset_names, "DaylightSavingsOffset"sv, "s_dst_offsets"sv, "s_dst_offsets_{}"sv, format_identifier,
[&](auto const& name, auto const& value) {
auto const& dst_offsets = time_zone_data.dst_offsets.find(value)->value;
append_offsets(name, "DaylightSavingsOffset"sv, dst_offsets);
});
generate_mapping(generator, time_zone_data.time_zone_region_names, time_zone_data.unique_strings.type_that_fits(), "s_regional_time_zones"sv, "s_regional_time_zones_{}"sv, format_identifier,
[&](auto const& name, auto const& value) {
auto const& time_zones = time_zone_data.time_zone_regions.find(value)->value;
generator.set("name", name);
generator.set("size", ByteString::number(time_zones.size()));
generator.append(R"~~~(
static constexpr Array<@string_index_type@, @size@> @name@ { {)~~~");
bool first = true;
for (auto const& time_zone : time_zones) {
generator.append(first ? " "sv : ", "sv);
generator.append(ByteString::number(time_zone));
first = false;
}
generator.append(" } };");
});
auto append_string_conversions = [&](StringView enum_title, StringView enum_snake, auto const& values, Vector<Alias> const& aliases = {}) -> ErrorOr<void> {
HashValueMap<ByteString> hashes;
TRY(hashes.try_ensure_capacity(values.size()));
auto hash = [](auto const& value) {
return CaseInsensitiveASCIIStringViewTraits::hash(value);
};
for (auto const& value : values)
hashes.set(hash(value), format_identifier(enum_title, value));
for (auto const& alias : aliases)
hashes.set(hash(alias.alias), format_identifier(enum_title, alias.alias));
ValueFromStringOptions options {};
options.sensitivity = CaseSensitivity::CaseInsensitive;
generate_value_from_string(generator, "{}_from_string"sv, enum_title, enum_snake, move(hashes), options);
generate_value_to_string(generator, "{}_to_string"sv, enum_title, enum_snake, format_identifier, values);
return {};
};
TRY(append_string_conversions("TimeZone"sv, "time_zone"sv, time_zone_data.time_zone_names, time_zone_data.time_zone_aliases));
TRY(append_string_conversions("Region"sv, "region"sv, time_zone_data.time_zone_region_names));
generator.append(R"~~~(
static Array<DaylightSavingsOffset const*, 2> find_dst_offsets(TimeZoneOffset const& time_zone_offset, AK::UnixDateTime time)
{
auto const& dst_rules = s_dst_offsets[time_zone_offset.dst_rule];
DaylightSavingsOffset const* standard_offset = nullptr;
DaylightSavingsOffset const* daylight_offset = nullptr;
DaylightSavingsOffset const* last_offset = nullptr;
auto preferred_rule = [&](auto* current_offset, auto& new_offset) {
if (!current_offset)
return &new_offset;
auto new_time_in_effect = new_offset.time_in_effect(time);
return (time >= new_time_in_effect) ? &new_offset : current_offset;
};
for (size_t index = 0; (index < dst_rules.size()) && (!standard_offset || !daylight_offset); ++index) {
auto const& dst_rule = dst_rules[index];
if (last_offset == nullptr)
last_offset = &dst_rule;
else if (dst_rule.time_in_effect(dst_rule.year_to) > last_offset->time_in_effect(last_offset->year_to))
last_offset = &dst_rule;
if ((time < dst_rule.year_from) || (time >= dst_rule.year_to))
continue;
if (dst_rule.offset == 0)
standard_offset = preferred_rule(standard_offset, dst_rule);
else
daylight_offset = preferred_rule(daylight_offset, dst_rule);
}
// If there isn't a standard or daylight rule in effect, fall back to the last rule given in the TZDB.
if (!standard_offset) {
VERIFY(last_offset != nullptr);
standard_offset = last_offset;
}
return { standard_offset, daylight_offset ? daylight_offset : standard_offset };
}
static Offset get_active_dst_offset(TimeZoneOffset const& time_zone_offset, AK::UnixDateTime time)
{
auto offsets = find_dst_offsets(time_zone_offset, time);
if (offsets[0] == offsets[1])
return { offsets[0]->offset, InDST::No };
auto standard_time_in_effect = offsets[0]->time_in_effect(time);
auto daylight_time_in_effect = offsets[1]->time_in_effect(time);
if (daylight_time_in_effect < standard_time_in_effect) {
if ((time < daylight_time_in_effect) || (time >= standard_time_in_effect))
return { offsets[0]->offset, InDST::No };
} else {
if ((time >= standard_time_in_effect) && (time < daylight_time_in_effect))
return { offsets[0]->offset, InDST::No };
}
return { offsets[1]->offset, InDST::Yes };
}
static TimeZoneOffset const& find_time_zone_offset(TimeZone time_zone, AK::UnixDateTime time)
{
auto const& time_zone_offsets = s_time_zone_offsets[to_underlying(time_zone)];
size_t index = 0;
for (; index < time_zone_offsets.size(); ++index) {
auto const& time_zone_offset = time_zone_offsets[index];
if (!time_zone_offset.has_until || (time_zone_offset.until.time_since_epoch() > time))
break;
}
VERIFY(index < time_zone_offsets.size());
return time_zone_offsets[index];
}
Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::UnixDateTime time)
{
auto const& time_zone_offset = find_time_zone_offset(time_zone, time);
Offset dst_offset {};
if (time_zone_offset.dst_rule != -1) {
dst_offset = get_active_dst_offset(time_zone_offset, time);
} else {
auto in_dst = time_zone_offset.dst_offset == 0 ? InDST::No : InDST::Yes;
dst_offset = { time_zone_offset.dst_offset, in_dst };
}
dst_offset.seconds += time_zone_offset.offset;
return dst_offset;
}
Vector<StringView> time_zones_in_region(StringView region)
{
auto region_value = region_from_string(region);
if (!region_value.has_value())
return {};
auto region_index = to_underlying(*region_value);
auto const& regional_time_zones = s_regional_time_zones[region_index];
Vector<StringView> time_zones;
time_zones.ensure_capacity(regional_time_zones.size());
for (auto time_zone : regional_time_zones)
time_zones.unchecked_append(decode_string(time_zone));
return time_zones;
}
)~~~");
quick_sort(time_zone_data.time_zones_and_links, [](auto const& lhs, auto const& rhs) {
return lhs.name < rhs.name;
});
generator.set("time_zones_and_links_size", MUST(String::number(time_zone_data.time_zones_and_links.size())));
generator.append(R"~~~(
ReadonlySpan<TimeZoneIdentifier> all_time_zones()
{
static constexpr Array<TimeZoneIdentifier, @time_zones_and_links_size@> time_zones_and_links { {)~~~");
bool first = true;
for (auto const& zone : time_zone_data.time_zones_and_links) {
generator.append(first ? " "sv : ", "sv);
generator.append(MUST(String::formatted("{}", zone)));
first = false;
}
generator.append(R"~~~( } };
return time_zones_and_links.span();
}
}
)~~~");
TRY(file.write_until_depleted(generator.as_string_view().bytes()));
return {};
}
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
StringView generated_header_path;
StringView generated_implementation_path;
StringView time_zone_coordinates_path;
Vector<StringView> time_zone_paths;
Core::ArgsParser args_parser;
args_parser.add_option(generated_header_path, "Path to the time zone data header file to generate", "generated-header-path", 'h', "generated-header-path");
args_parser.add_option(generated_implementation_path, "Path to the time zone data implementation file to generate", "generated-implementation-path", 'c', "generated-implementation-path");
args_parser.add_option(time_zone_coordinates_path, "Path to the time zone data coordinates file", "time-zone-coordinates-path", 'z', "time-zone-coordinates-path");
args_parser.add_positional_argument(time_zone_paths, "Paths to the time zone database files", "time-zone-paths");
args_parser.parse(arguments);
auto generated_header_file = TRY(open_file(generated_header_path, Core::File::OpenMode::Write));
auto generated_implementation_file = TRY(open_file(generated_implementation_path, Core::File::OpenMode::Write));
auto time_zone_coordinates_file = TRY(open_file(time_zone_coordinates_path, Core::File::OpenMode::Read));
TimeZoneData time_zone_data {};
for (auto time_zone_path : time_zone_paths)
TRY(parse_time_zones(time_zone_path, time_zone_data));
TRY(parse_time_zone_coordinates(*time_zone_coordinates_file, time_zone_data));
TRY(generate_time_zone_data_header(*generated_header_file, time_zone_data));
TRY(generate_time_zone_data_implementation(*generated_implementation_file, time_zone_data));
return 0;
}

View file

@ -9,8 +9,6 @@
#include <AK/ByteString.h>
#include <AK/HashMap.h>
#include <AK/NumericLimits.h>
#include <AK/Optional.h>
#include <AK/QuickSort.h>
#include <AK/SourceGenerator.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
@ -40,17 +38,6 @@ public:
return storage_index;
}
StorageType const& get(size_t index) const
{
if (index == 0) {
static StorageType empty {};
return empty;
}
VERIFY(index <= m_storage.size());
return m_storage.at(index - 1);
}
StringView type_that_fits() const
{
if (m_storage.size() <= NumericLimits<u8>::max())
@ -155,11 +142,6 @@ static constexpr StringView decode_string(size_t index)
}
};
struct Alias {
ByteString name;
ByteString alias;
};
inline ErrorOr<NonnullOwnPtr<Core::InputBufferedFile>> open_file(StringView path, Core::File::OpenMode mode)
{
if (path.is_empty())
@ -168,232 +150,3 @@ inline ErrorOr<NonnullOwnPtr<Core::InputBufferedFile>> open_file(StringView path
auto file = TRY(Core::File::open(path, mode));
return Core::InputBufferedFile::create(move(file));
}
inline void ensure_from_string_types_are_generated(SourceGenerator& generator)
{
static bool generated_from_string_types = false;
if (generated_from_string_types)
return;
generator.append(R"~~~(
template <typename ValueType>
struct HashValuePair {
unsigned hash { 0 };
ValueType value {};
};
template <typename ValueType>
struct HashValueComparator
{
constexpr int operator()(unsigned hash, HashValuePair<ValueType> const& pair)
{
if (hash > pair.hash)
return 1;
if (hash < pair.hash)
return -1;
return 0;
}
};
)~~~");
generated_from_string_types = true;
}
template<typename ValueType>
using HashValueMap = HashMap<unsigned, ValueType>;
struct ValueFromStringOptions {
Optional<StringView> return_type {};
StringView return_format { "{}"sv };
CaseSensitivity sensitivity { CaseSensitivity::CaseSensitive };
};
template<typename ValueType>
void generate_value_from_string(SourceGenerator& generator, StringView method_name_format, StringView value_type, StringView value_name, HashValueMap<ValueType> hashes, ValueFromStringOptions options = {})
{
ensure_from_string_types_are_generated(generator);
generator.set("method_name", ByteString::formatted(method_name_format, value_name));
generator.set("value_type", value_type);
generator.set("value_name", value_name);
generator.set("return_type", options.return_type.has_value() ? *options.return_type : value_type);
generator.set("size", ByteString::number(hashes.size()));
generator.append(R"~~~(
Optional<@return_type@> @method_name@(StringView key)
{
constexpr Array<HashValuePair<@value_type@>, @size@> hash_pairs { {
)~~~");
auto hash_keys = hashes.keys();
quick_sort(hash_keys);
constexpr size_t max_values_per_row = 10;
size_t values_in_current_row = 0;
for (auto hash_key : hash_keys) {
if (values_in_current_row++ > 0)
generator.append(" ");
if constexpr (IsIntegral<ValueType>)
generator.set("value"sv, ByteString::number(hashes.get(hash_key).value()));
else
generator.set("value"sv, ByteString::formatted("{}::{}", value_type, hashes.get(hash_key).value()));
generator.set("hash"sv, ByteString::number(hash_key));
generator.append("{ @hash@U, @value@ },"sv);
if (values_in_current_row == max_values_per_row) {
generator.append("\n ");
values_in_current_row = 0;
}
}
generator.set("return_statement", ByteString::formatted(options.return_format, "value->value"sv));
generator.append(R"~~~(
} };
)~~~");
if (options.sensitivity == CaseSensitivity::CaseSensitive) {
generator.append(R"~~~(
auto hash = key.hash();
)~~~");
} else {
generator.append(R"~~~(
auto hash = CaseInsensitiveASCIIStringViewTraits::hash(key);
)~~~");
}
generator.append(R"~~~(
if (auto const* value = binary_search(hash_pairs, hash, nullptr, HashValueComparator<@value_type@> {}))
return @return_statement@;
return {};
}
)~~~");
}
template<typename IdentifierFormatter>
void generate_value_to_string(SourceGenerator& generator, StringView method_name_format, StringView value_type, StringView value_name, IdentifierFormatter&& format_identifier, ReadonlySpan<ByteString> values)
{
generator.set("method_name", ByteString::formatted(method_name_format, value_name));
generator.set("value_type", value_type);
generator.set("value_name", value_name);
generator.append(R"~~~(
StringView @method_name@(@value_type@ @value_name@)
{
using enum @value_type@;
switch (@value_name@) {)~~~");
for (auto const& value : values) {
generator.set("enum_value", format_identifier(value_type, value));
generator.set("string_value", value);
generator.append(R"~~~(
case @enum_value@:
return "@string_value@"sv;)~~~");
}
generator.append(R"~~~(
}
VERIFY_NOT_REACHED();
}
)~~~");
}
template<typename IdentifierFormatter>
void generate_enum(SourceGenerator& generator, IdentifierFormatter&& format_identifier, StringView name, StringView default_, Vector<ByteString>& values, Vector<Alias> aliases = {})
{
quick_sort(values, [](auto const& value1, auto const& value2) { return value1.to_lowercase() < value2.to_lowercase(); });
quick_sort(aliases, [](auto const& alias1, auto const& alias2) { return alias1.alias.to_lowercase() < alias2.alias.to_lowercase(); });
generator.set("name", name);
generator.set("underlying", ((values.size() + !default_.is_empty()) < 256) ? "u8"sv : "u16"sv);
generator.append(R"~~~(
enum class @name@ : @underlying@ {)~~~");
if (!default_.is_empty()) {
generator.set("default", default_);
generator.append(R"~~~(
@default@,)~~~");
}
for (auto const& value : values) {
generator.set("value", format_identifier(name, value));
generator.append(R"~~~(
@value@,)~~~");
}
for (auto const& alias : aliases) {
generator.set("alias", format_identifier(name, alias.alias));
generator.set("value", format_identifier(name, alias.name));
generator.append(R"~~~(
@alias@ = @value@,)~~~");
}
generator.append(R"~~~(
};
)~~~");
}
template<typename LocalesType, typename IdentifierFormatter, typename ListFormatter>
void generate_mapping(SourceGenerator& generator, LocalesType const& locales, StringView type, StringView name, StringView format, IdentifierFormatter&& format_identifier, ListFormatter&& format_list)
{
auto format_mapping_name = [&](StringView format, StringView name) {
ByteString mapping_name;
if constexpr (IsNullPointer<IdentifierFormatter>)
mapping_name = name.replace("-"sv, "_"sv, ReplaceMode::All);
else
mapping_name = format_identifier(type, name);
return ByteString::formatted(format, mapping_name.to_lowercase());
};
Vector<ByteString> mapping_names;
for (auto const& locale : locales) {
ByteString mapping_name;
if constexpr (requires { locale.key; }) {
mapping_name = format_mapping_name(format, locale.key);
format_list(mapping_name, locale.value);
} else {
mapping_name = format_mapping_name(format, locale);
format_list(mapping_name, locale);
}
mapping_names.append(move(mapping_name));
}
quick_sort(mapping_names);
generator.set("type", type);
generator.set("name", name);
generator.set("size", ByteString::number(locales.size()));
generator.append(R"~~~(
static constexpr Array<ReadonlySpan<@type@>, @size@> @name@ { {
)~~~");
constexpr size_t max_values_per_row = 10;
size_t values_in_current_row = 0;
for (auto& mapping_name : mapping_names) {
if (values_in_current_row++ > 0)
generator.append(" ");
generator.set("name", move(mapping_name));
generator.append("@name@.span(),");
if (values_in_current_row == max_values_per_row) {
values_in_current_row = 0;
generator.append("\n ");
}
}
generator.append(R"~~~(
} };
)~~~");
}

View file

@ -9,7 +9,6 @@ add_subdirectory(LibRegex)
add_subdirectory(LibTest)
add_subdirectory(LibTextCodec)
add_subdirectory(LibThreading)
add_subdirectory(LibTimeZone)
add_subdirectory(LibUnicode)
add_subdirectory(LibURL)
add_subdirectory(LibMedia)

View file

@ -1,10 +0,0 @@
set(TEST_SOURCES
TestTimeZoneDeprecated.cpp
)
foreach(source IN LISTS TEST_SOURCES)
serenity_test("${source}" LibTimeZone LIBS LibCore LibTimeZone)
get_filename_component(target "${source}" NAME_WLE)
target_compile_definitions("${target}" PRIVATE ENABLE_TIME_ZONE_DATA=$<BOOL:${ENABLE_TIME_ZONE_DATABASE_DOWNLOAD}>)
endforeach()

View file

@ -1,203 +0,0 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibTest/TestCase.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <LibTimeZone/TimeZone.h>
using enum TimeZone::InDST;
static void test_offset(StringView time_zone, i64 time, i64 expected_offset, TimeZone::InDST expected_in_dst)
{
auto actual_offset = TimeZone::get_time_zone_offset(time_zone, AK::UnixDateTime::from_seconds_since_epoch(time));
VERIFY(actual_offset.has_value());
EXPECT_EQ(actual_offset->seconds, expected_offset);
EXPECT_EQ(actual_offset->in_dst, expected_in_dst);
}
#if ENABLE_TIME_ZONE_DATA
# include <LibTimeZone/TimeZoneData.h>
class TimeZoneGuard {
public:
explicit TimeZoneGuard(char const* tz)
: m_tz(getenv("TZ"))
{
setenv("TZ", tz, 1);
}
~TimeZoneGuard()
{
if (m_tz)
setenv("TZ", m_tz, 1);
else
unsetenv("TZ");
}
private:
char const* m_tz { nullptr };
};
TEST_CASE(time_zone_from_string)
{
EXPECT_EQ(TimeZone::time_zone_from_string("America/New_York"sv), TimeZone::TimeZone::America_New_York);
EXPECT_EQ(TimeZone::time_zone_from_string("Europe/Paris"sv), TimeZone::TimeZone::Europe_Paris);
EXPECT_EQ(TimeZone::time_zone_from_string("Etc/GMT+2"sv), TimeZone::TimeZone::Etc_GMT_Ahead_2);
EXPECT_EQ(TimeZone::time_zone_from_string("Etc/GMT-5"sv), TimeZone::TimeZone::Etc_GMT_Behind_5);
EXPECT(!TimeZone::time_zone_from_string("I don't exist"sv).has_value());
}
TEST_CASE(time_zone_from_string_link)
{
auto test_link = [](auto tz1, auto tz2) {
auto result1 = TimeZone::time_zone_from_string(tz1);
EXPECT(result1.has_value());
auto result2 = TimeZone::time_zone_from_string(tz2);
EXPECT(result2.has_value());
EXPECT_EQ(*result1, *result2);
};
test_link("America/New_York"sv, "US/Eastern"sv);
test_link("Etc/GMT"sv, "GMT"sv);
test_link("Etc/GMT+0"sv, "GMT"sv);
test_link("Etc/GMT-0"sv, "GMT"sv);
test_link("Etc/UTC"sv, "UTC"sv);
test_link("Etc/Universal"sv, "UTC"sv);
test_link("Universal"sv, "UTC"sv);
}
TEST_CASE(case_insensitive_time_zone_from_string)
{
EXPECT_EQ(TimeZone::time_zone_from_string("UTC"sv), TimeZone::TimeZone::UTC);
EXPECT_EQ(TimeZone::time_zone_from_string("utc"sv), TimeZone::TimeZone::UTC);
EXPECT_EQ(TimeZone::time_zone_from_string("uTc"sv), TimeZone::TimeZone::UTC);
}
TEST_CASE(time_zone_to_string)
{
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::America_New_York), "America/New_York"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Europe_Paris), "Europe/Paris"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_GMT_Ahead_2), "Etc/GMT+2"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_GMT_Behind_5), "Etc/GMT-5"sv);
}
TEST_CASE(time_zone_to_string_link)
{
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_UTC), "Etc/UTC"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::UTC), "Etc/UTC"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Universal), "Etc/UTC"sv);
EXPECT_EQ(TimeZone::time_zone_to_string(TimeZone::TimeZone::Etc_Universal), "Etc/UTC"sv);
}
TEST_CASE(canonicalize_time_zone)
{
EXPECT_EQ(TimeZone::canonicalize_time_zone("America/New_York"sv), "America/New_York"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("AmErIcA/NeW_YoRk"sv), "America/New_York"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("UTC"sv), "UTC"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("GMT"sv), "UTC"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("GMT+0"sv), "UTC"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("GMT-0"sv), "UTC"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("Etc/UTC"sv), "UTC"sv);
EXPECT_EQ(TimeZone::canonicalize_time_zone("Etc/GMT"sv), "UTC"sv);
EXPECT(!TimeZone::canonicalize_time_zone("I don't exist"sv).has_value());
}
TEST_CASE(invalid_time_zone)
{
TimeZoneGuard guard { "ladybird" };
EXPECT_EQ(TimeZone::current_time_zone(), "UTC"sv);
}
static i64 offset(i64 sign, i64 hours, i64 minutes, i64 seconds)
{
return sign * ((hours * 3600) + (minutes * 60) + seconds);
}
TEST_CASE(get_time_zone_offset)
{
test_offset("America/Chicago"sv, -2717647201, offset(-1, 5, 50, 36), No); // Sunday, November 18, 1883 5:59:59 PM
test_offset("America/Chicago"sv, -2717647200, offset(-1, 6, 00, 00), No); // Sunday, November 18, 1883 6:00:00 PM
test_offset("America/Chicago"sv, -1067810460, offset(-1, 6, 00, 00), No); // Sunday, March 1, 1936 1:59:00 AM
test_offset("America/Chicago"sv, -1067810400, offset(-1, 5, 00, 00), No); // Sunday, March 1, 1936 2:00:00 AM
test_offset("America/Chicago"sv, -1045432860, offset(-1, 5, 00, 00), No); // Sunday, November 15, 1936 1:59:00 AM
test_offset("America/Chicago"sv, -1045432800, offset(-1, 6, 00, 00), No); // Sunday, November 15, 1936 2:00:00 AM
test_offset("Europe/London"sv, -3852662401, offset(-1, 0, 01, 15), No); // Tuesday, November 30, 1847 11:59:59 PM
test_offset("Europe/London"sv, -3852662400, offset(+1, 0, 00, 00), No); // Wednesday, December 1, 1847 12:00:00 AM
test_offset("Europe/London"sv, -37238401, offset(+1, 0, 00, 00), No); // Saturday, October 26, 1968 11:59:59 PM
test_offset("Europe/London"sv, -37238400, offset(+1, 1, 00, 00), No); // Sunday, October 27, 1968 12:00:00 AM
test_offset("Europe/London"sv, 57722399, offset(+1, 1, 00, 00), No); // Sunday, October 31, 1971 1:59:59 AM
test_offset("Europe/London"sv, 57722400, offset(+1, 0, 00, 00), No); // Sunday, October 31, 1971 2:00:00 AM
test_offset("UTC"sv, -1641846268, offset(+1, 0, 00, 00), No);
test_offset("UTC"sv, 0, offset(+1, 0, 00, 00), No);
test_offset("UTC"sv, 1641846268, offset(+1, 0, 00, 00), No);
test_offset("Etc/GMT+4"sv, -1641846268, offset(-1, 4, 00, 00), No);
test_offset("Etc/GMT+5"sv, 0, offset(-1, 5, 00, 00), No);
test_offset("Etc/GMT+6"sv, 1641846268, offset(-1, 6, 00, 00), No);
test_offset("Etc/GMT-12"sv, -1641846268, offset(+1, 12, 00, 00), No);
test_offset("Etc/GMT-13"sv, 0, offset(+1, 13, 00, 00), No);
test_offset("Etc/GMT-14"sv, 1641846268, offset(+1, 14, 00, 00), No);
EXPECT(!TimeZone::get_time_zone_offset("I don't exist"sv, {}).has_value());
}
TEST_CASE(get_time_zone_offset_with_dst)
{
test_offset("America/New_York"sv, 1642558528, offset(-1, 5, 00, 00), No); // Wednesday, January 19, 2022 2:15:28 AM
test_offset("America/New_York"sv, 1663553728, offset(-1, 4, 00, 00), Yes); // Monday, September 19, 2022 2:15:28 AM
test_offset("America/New_York"sv, 1671453238, offset(-1, 5, 00, 00), No); // Monday, December 19, 2022 12:33:58 PM
// Phoenix does not observe DST.
test_offset("America/Phoenix"sv, 1642558528, offset(-1, 7, 00, 00), No); // Wednesday, January 19, 2022 2:15:28 AM
test_offset("America/Phoenix"sv, 1663553728, offset(-1, 7, 00, 00), No); // Monday, September 19, 2022 2:15:28 AM
test_offset("America/Phoenix"sv, 1671453238, offset(-1, 7, 00, 00), No); // Monday, December 19, 2022 12:33:58 PM
// Moscow's observed DST changed several times in 1919.
test_offset("Europe/Moscow"sv, -1609459200, offset(+1, 2, 31, 19), No); // Wednesday, January 1, 1919 12:00:00 AM
test_offset("Europe/Moscow"sv, -1596412800, offset(+1, 4, 31, 19), Yes); // Sunday, June 1, 1919 12:00:00 AM
test_offset("Europe/Moscow"sv, -1592611200, offset(+1, 4, 00, 00), Yes); // Tuesday, July 15, 1919 12:00:00 AM
test_offset("Europe/Moscow"sv, -1589068800, offset(+1, 3, 00, 00), No); // Monday, August 25, 1919 12:00:00 AM
// Paraguay begins the year in DST.
test_offset("America/Asuncion"sv, 1642558528, offset(-1, 3, 00, 00), Yes); // Wednesday, January 19, 2022 2:15:28 AM
test_offset("America/Asuncion"sv, 1663553728, offset(-1, 4, 00, 00), No); // Monday, September 19, 2022 2:15:28 AM
test_offset("America/Asuncion"sv, 1671453238, offset(-1, 3, 00, 00), Yes); // Monday, December 19, 2022 12:33:58 PM
}
#else
TEST_CASE(time_zone_from_string)
{
EXPECT(TimeZone::time_zone_from_string("UTC"sv).has_value());
EXPECT(!TimeZone::time_zone_from_string("Europe/Paris"sv).has_value());
EXPECT(!TimeZone::time_zone_from_string("Etc/UTC"sv).has_value());
EXPECT(!TimeZone::time_zone_from_string("I don't exist"sv).has_value());
}
TEST_CASE(get_time_zone_offset)
{
test_offset("UTC"sv, 123456, 0, No);
EXPECT(!TimeZone::get_time_zone_offset("Europe/Paris"sv, {}).has_value());
EXPECT(!TimeZone::get_time_zone_offset("Etc/UTC"sv, {}).has_value());
EXPECT(!TimeZone::get_time_zone_offset("I don't exist"sv, {}).has_value());
}
#endif

View file

@ -1,11 +0,0 @@
include(${SerenityOS_SOURCE_DIR}/Meta/CMake/time_zone_data.cmake)
set(SOURCES
${TIME_ZONE_DATA_SOURCES}
TimeZone.cpp
)
set(GENERATED_SOURCES ${CURRENT_LIB_GENERATED})
serenity_lib(LibTimeZone timezone)
target_compile_definitions(LibTimeZone PRIVATE ENABLE_TIME_ZONE_DATA=$<BOOL:${ENABLE_TIME_ZONE_DATABASE_DOWNLOAD}>)

View file

@ -1,17 +0,0 @@
/*
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Types.h>
namespace TimeZone {
enum class DaylightSavingsRule : u8;
enum class Region : u8;
enum class TimeZone : u16;
}

View file

@ -1,193 +0,0 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <AK/Debug.h>
#include <AK/ScopeGuard.h>
#include <LibTimeZone/TimeZone.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
namespace TimeZone {
// NOTE: Without ENABLE_TIME_ZONE_DATA LibTimeZone operates in a UTC-only mode and only recognizes
// the 'UTC' time zone, which is slightly more useful than a bunch of dummy functions that
// can't do anything. When we build with time zone data, these weakly linked functions are
// replaced with their proper counterparts.
#if !ENABLE_TIME_ZONE_DATA
enum class TimeZone : u16 {
UTC,
};
#endif
class TimeZoneFile {
public:
TimeZoneFile(char const* mode)
: m_file(fopen("/etc/timezone", mode))
{
if (m_file)
flockfile(m_file);
}
~TimeZoneFile()
{
if (m_file) {
funlockfile(m_file);
fclose(m_file);
}
}
ErrorOr<ByteString> read_time_zone()
{
if (!m_file)
return Error::from_string_literal("Could not open /etc/timezone");
Array<u8, 128> buffer;
size_t bytes = fread(buffer.data(), 1, buffer.size(), m_file);
if (bytes == 0)
return Error::from_string_literal("Could not read time zone from /etc/timezone");
return ByteString(buffer.span().slice(0, bytes)).trim_whitespace();
}
ErrorOr<void> write_time_zone(StringView time_zone)
{
if (!m_file)
return Error::from_string_literal("Could not open /etc/timezone");
auto bytes = fwrite(time_zone.characters_without_null_termination(), 1, time_zone.length(), m_file);
if (bytes != time_zone.length())
return Error::from_string_literal("Could not write new time zone to /etc/timezone");
return {};
}
private:
FILE* m_file { nullptr };
};
static StringView system_time_zone()
{
TimeZoneFile time_zone_file("r");
auto time_zone = time_zone_file.read_time_zone();
// FIXME: Propagate the error to existing callers.
if (time_zone.is_error()) {
dbgln_if(TIME_ZONE_DEBUG, "{}", time_zone.error());
return "UTC"sv;
}
return canonicalize_time_zone(time_zone.value()).value_or("UTC"sv);
}
StringView current_time_zone()
{
if (char* tz = getenv("TZ"); tz != nullptr) {
// FIXME: Actually parse the TZ environment variable, described here:
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap08.html#tag_08
StringView time_zone { tz, strlen(tz) };
if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value())
return *maybe_time_zone;
dbgln_if(TIME_ZONE_DEBUG, "Could not determine time zone from TZ environment: {}", time_zone);
return "UTC"sv;
}
static constexpr auto zoneinfo = "/zoneinfo"sv;
char* real_path = realpath("/etc/localtime", nullptr);
ScopeGuard free_path = [real_path]() { free(real_path); };
if (real_path) {
auto time_zone = StringView { real_path, strlen(real_path) };
// The zoneinfo file may be located in paths like /usr/share/zoneinfo/ or /usr/share/zoneinfo.default/.
// We want to strip any such prefix from the path to arrive at the time zone name.
if (auto index = time_zone.find(zoneinfo); index.has_value())
time_zone = time_zone.substring_view(*index + zoneinfo.length());
if (auto index = time_zone.find('/'); index.has_value())
time_zone = time_zone.substring_view(*index + 1);
if (auto maybe_time_zone = canonicalize_time_zone(time_zone); maybe_time_zone.has_value())
return *maybe_time_zone;
dbgln_if(TIME_ZONE_DEBUG, "Could not determine time zone from /etc/localtime: {}", time_zone);
} else {
dbgln_if(TIME_ZONE_DEBUG, "Could not read the /etc/localtime link: {}", strerror(errno));
}
return system_time_zone();
}
ReadonlySpan<TimeZoneIdentifier> __attribute__((weak)) all_time_zones()
{
#if !ENABLE_TIME_ZONE_DATA
static constexpr auto utc = Array { TimeZoneIdentifier { "UTC"sv, IsLink::No } };
return utc;
#else
return {};
#endif
}
Optional<TimeZone> __attribute__((weak)) time_zone_from_string([[maybe_unused]] StringView time_zone)
{
#if !ENABLE_TIME_ZONE_DATA
if (time_zone.equals_ignoring_ascii_case("UTC"sv))
return TimeZone::UTC;
#endif
return {};
}
StringView __attribute__((weak)) time_zone_to_string([[maybe_unused]] TimeZone time_zone)
{
#if !ENABLE_TIME_ZONE_DATA
VERIFY(time_zone == TimeZone::UTC);
return "UTC"sv;
#else
return {};
#endif
}
Optional<StringView> canonicalize_time_zone(StringView time_zone)
{
auto maybe_time_zone = time_zone_from_string(time_zone);
if (!maybe_time_zone.has_value())
return {};
auto canonical_time_zone = time_zone_to_string(*maybe_time_zone);
if (canonical_time_zone.is_one_of("Etc/UTC"sv, "Etc/GMT"sv, "GMT"sv))
return "UTC"sv;
return canonical_time_zone;
}
Optional<Offset> __attribute__((weak)) get_time_zone_offset([[maybe_unused]] TimeZone time_zone, AK::UnixDateTime)
{
#if !ENABLE_TIME_ZONE_DATA
VERIFY(time_zone == TimeZone::UTC);
return Offset {};
#else
return {};
#endif
}
Optional<Offset> get_time_zone_offset(StringView time_zone, AK::UnixDateTime 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);
return {};
}
Optional<Region> __attribute__((weak)) region_from_string(StringView) { return {}; }
StringView __attribute__((weak)) region_to_string(Region) { return {}; }
Vector<StringView> __attribute__((weak)) time_zones_in_region(StringView) { return {}; }
}

View file

@ -1,62 +0,0 @@
/*
* Copyright (c) 2022-2024, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Error.h>
#include <AK/Format.h>
#include <AK/Optional.h>
#include <AK/StringView.h>
#include <AK/Time.h>
#include <AK/Types.h>
#include <AK/Vector.h>
#include <LibTimeZone/Forward.h>
namespace TimeZone {
enum class IsLink {
No,
Yes,
};
struct TimeZoneIdentifier {
StringView name;
IsLink is_link { IsLink::No };
};
enum class InDST {
No,
Yes,
};
struct Offset {
i64 seconds { 0 };
InDST in_dst { InDST::No };
};
StringView current_time_zone();
ReadonlySpan<TimeZoneIdentifier> all_time_zones();
Optional<TimeZone> time_zone_from_string(StringView time_zone);
StringView time_zone_to_string(TimeZone time_zone);
Optional<StringView> canonicalize_time_zone(StringView time_zone);
Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::UnixDateTime time);
Optional<Offset> get_time_zone_offset(StringView time_zone, AK::UnixDateTime time);
Optional<Region> region_from_string(StringView region);
StringView region_to_string(Region region);
Vector<StringView> time_zones_in_region(StringView region);
}
template<>
struct AK::Formatter<TimeZone::TimeZone> : Formatter<FormatString> {
ErrorOr<void> format(FormatBuilder& builder, TimeZone::TimeZone const& time_zone)
{
return Formatter<FormatString>::format(builder, TimeZone::time_zone_to_string(time_zone));
}
};