diff options
author | Timothy Flynn <trflynn89@pm.me> | 2022-01-24 12:36:17 -0500 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-01-25 18:39:36 +0000 |
commit | 7103012c7d447c1ba3d18597b086ae868661f82e (patch) | |
tree | ea203c8accdd873c23ea23972766e18fb5983f7d /Meta | |
parent | fc0c88344b04c7b1a849f1f1e0fe63558112db7b (diff) | |
download | serenity-7103012c7d447c1ba3d18597b086ae868661f82e.zip |
LibTimeZone: Add an API to retrieve both daylight and standard offsets
This API will also include the formatted name of the time zone, with
respect for DST (e.g. EST vs EDT for America/New_York).
Diffstat (limited to 'Meta')
-rw-r--r-- | Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp | 32 |
1 files changed, 32 insertions, 0 deletions
diff --git a/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp b/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp index 47bf85b275..264e523bb7 100644 --- a/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp +++ b/Meta/Lagom/Tools/CodeGenerators/LibTimeZone/GenerateTimeZoneData.cpp @@ -582,6 +582,38 @@ Optional<Offset> get_time_zone_offset(TimeZone time_zone, AK::Time time) return dst_offset; } +Optional<Array<NamedOffset, 2>> get_named_time_zone_offsets(TimeZone time_zone, AK::Time time) +{ + auto const& time_zone_offset = find_time_zone_offset(time_zone, time); + Array<NamedOffset, 2> named_offsets; + + auto format_name = [](auto format, auto offset) -> String { + if (offset == 0) + return s_string_list[format].replace("{}"sv, ""sv); + return String::formatted(s_string_list[format], s_string_list[offset]); + }; + + auto set_named_offset = [&](auto& named_offset, auto dst_offset, auto in_dst, auto format, auto offset) { + named_offset.seconds = time_zone_offset.offset + dst_offset; + named_offset.in_dst = in_dst; + named_offset.name = format_name(format, offset); + }; + + if (time_zone_offset.dst_rule != -1) { + auto offsets = find_dst_offsets(time_zone_offset, time); + auto in_dst = offsets[1]->offset == 0 ? InDST::No : InDST::Yes; + + set_named_offset(named_offsets[0], offsets[0]->offset, InDST::No, time_zone_offset.standard_format, offsets[0]->format); + set_named_offset(named_offsets[1], offsets[1]->offset, in_dst, time_zone_offset.daylight_format, offsets[1]->format); + } else { + auto in_dst = time_zone_offset.dst_offset == 0 ? InDST::No : InDST::Yes; + set_named_offset(named_offsets[0], time_zone_offset.dst_offset, in_dst, time_zone_offset.standard_format, 0); + set_named_offset(named_offsets[1], time_zone_offset.dst_offset, in_dst, time_zone_offset.daylight_format, 0); + } + + return named_offsets; +} + Span<StringView const> all_time_zones() { static constexpr auto all_time_zones = Array { |