1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:27:35 +00:00

LibCore+LibTimeZone: Support time zone names in Core::DateTime::parse

LibCore currently cannot depend on LibTimeZone directly. All build-time
code generators depend on LibCore, so there'd be a circular dependency:
LibCore -> LibTimeZone -> GenerateTZData -> LibCore.

So to support parsing time zone names and applying their offsets, add a
couple of weakly-defined helper functions. These work similar to the way
AK::String declares some methods that LibUnicode defines. Any user who
wants to parse time zone names (from outside of LibCore itself) can link
against LibTimeZone to receive full support.
This commit is contained in:
Timothy Flynn 2023-11-06 20:50:11 -05:00 committed by Andreas Kling
parent fd0075083a
commit 350fdf1e43
7 changed files with 169 additions and 4 deletions

View file

@ -0,0 +1,38 @@
/*
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/GenericLexer.h>
#include <LibTimeZone/DateTime.h>
#include <LibTimeZone/TimeZone.h>
namespace Core {
Optional<StringView> parse_time_zone_name(GenericLexer& lexer)
{
auto start_position = lexer.tell();
Optional<StringView> canonicalized_time_zone;
lexer.ignore_until([&](auto) {
auto time_zone = lexer.input().substring_view(start_position, lexer.tell() - start_position + 1);
canonicalized_time_zone = TimeZone::canonicalize_time_zone(time_zone);
return canonicalized_time_zone.has_value();
});
if (canonicalized_time_zone.has_value())
lexer.ignore();
return canonicalized_time_zone;
}
void apply_time_zone_offset(StringView time_zone, UnixDateTime& time)
{
if (auto offset = TimeZone::get_time_zone_offset(time_zone, time); offset.has_value())
time -= Duration::from_seconds(offset->seconds);
}
}