xref: /freebsd-src/contrib/llvm-project/libcxx/src/experimental/time_zone.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
2*0fca6ea1SDimitry Andric //
3*0fca6ea1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0fca6ea1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0fca6ea1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0fca6ea1SDimitry Andric //
7*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
8*0fca6ea1SDimitry Andric 
9*0fca6ea1SDimitry Andric // For information see https://libcxx.llvm.org/DesignDocs/TimeZone.html
10*0fca6ea1SDimitry Andric 
11*0fca6ea1SDimitry Andric // TODO TZDB look at optimizations
12*0fca6ea1SDimitry Andric //
13*0fca6ea1SDimitry Andric // The current algorithm is correct but not efficient. For example, in a named
14*0fca6ea1SDimitry Andric // rule based continuation finding the next rule does quite a bit of work,
15*0fca6ea1SDimitry Andric // returns the next rule and "forgets" its state. This could be better.
16*0fca6ea1SDimitry Andric //
17*0fca6ea1SDimitry Andric // It would be possible to cache lookups. If a time for a zone is calculated its
18*0fca6ea1SDimitry Andric // sys_info could be kept and the next lookup could test whether the time is in
19*0fca6ea1SDimitry Andric // a "known" sys_info. The wording in the Standard hints at this slowness by
20*0fca6ea1SDimitry Andric // "suggesting" this could be implemented on the user's side.
21*0fca6ea1SDimitry Andric 
22*0fca6ea1SDimitry Andric // TODO TZDB look at removing quirks
23*0fca6ea1SDimitry Andric //
24*0fca6ea1SDimitry Andric // The code has some special rules to adjust the timing at the continuation
25*0fca6ea1SDimitry Andric // switches. This works correctly, but some of the places feel odd. It would be
26*0fca6ea1SDimitry Andric // good to investigate this further and see whether all quirks are needed or
27*0fca6ea1SDimitry Andric // that there are better fixes.
28*0fca6ea1SDimitry Andric //
29*0fca6ea1SDimitry Andric // These quirks often use a 12h interval; this is the scan interval of zdump,
30*0fca6ea1SDimitry Andric // which implies there are no sys_info objects with a duration of less than 12h.
31*0fca6ea1SDimitry Andric 
32*0fca6ea1SDimitry Andric #include <algorithm>
33*0fca6ea1SDimitry Andric #include <cctype>
34*0fca6ea1SDimitry Andric #include <chrono>
35*0fca6ea1SDimitry Andric #include <expected>
36*0fca6ea1SDimitry Andric #include <map>
37*0fca6ea1SDimitry Andric #include <numeric>
38*0fca6ea1SDimitry Andric #include <ranges>
39*0fca6ea1SDimitry Andric 
40*0fca6ea1SDimitry Andric #include "include/tzdb/time_zone_private.h"
41*0fca6ea1SDimitry Andric #include "include/tzdb/tzdb_list_private.h"
42*0fca6ea1SDimitry Andric 
43*0fca6ea1SDimitry Andric // TODO TZDB remove debug printing
44*0fca6ea1SDimitry Andric #ifdef PRINT
45*0fca6ea1SDimitry Andric #  include <print>
46*0fca6ea1SDimitry Andric #endif
47*0fca6ea1SDimitry Andric 
48*0fca6ea1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
49*0fca6ea1SDimitry Andric 
50*0fca6ea1SDimitry Andric #ifdef PRINT
51*0fca6ea1SDimitry Andric template <>
52*0fca6ea1SDimitry Andric struct formatter<chrono::sys_info, char> {
53*0fca6ea1SDimitry Andric   template <class ParseContext>
54*0fca6ea1SDimitry Andric   constexpr typename ParseContext::iterator parse(ParseContext& ctx) {
55*0fca6ea1SDimitry Andric     return ctx.begin();
56*0fca6ea1SDimitry Andric   }
57*0fca6ea1SDimitry Andric 
58*0fca6ea1SDimitry Andric   template <class FormatContext>
59*0fca6ea1SDimitry Andric   typename FormatContext::iterator format(const chrono::sys_info& info, FormatContext& ctx) const {
60*0fca6ea1SDimitry Andric     return std::format_to(
61*0fca6ea1SDimitry Andric         ctx.out(), "[{}, {}) {:%Q%q} {:%Q%q} {}", info.begin, info.end, info.offset, info.save, info.abbrev);
62*0fca6ea1SDimitry Andric   }
63*0fca6ea1SDimitry Andric };
64*0fca6ea1SDimitry Andric #endif
65*0fca6ea1SDimitry Andric 
66*0fca6ea1SDimitry Andric namespace chrono {
67*0fca6ea1SDimitry Andric 
68*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
69*0fca6ea1SDimitry Andric //                           Details
70*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
71*0fca6ea1SDimitry Andric 
72*0fca6ea1SDimitry Andric struct __sys_info {
73*0fca6ea1SDimitry Andric   sys_info __info;
74*0fca6ea1SDimitry Andric   bool __can_merge; // Can the returned sys_info object be merged with
75*0fca6ea1SDimitry Andric };
76*0fca6ea1SDimitry Andric 
77*0fca6ea1SDimitry Andric // Return type for helper function to get a sys_info.
78*0fca6ea1SDimitry Andric // - The expected result returns the "best" sys_info object. This object can be
79*0fca6ea1SDimitry Andric //   before the requested time. Sometimes sys_info objects from different
80*0fca6ea1SDimitry Andric //   continuations share their offset, save, and abbrev and these objects are
81*0fca6ea1SDimitry Andric //   merged to one sys_info object. The __can_merge flag determines whether the
82*0fca6ea1SDimitry Andric //   current result can be merged with the next result.
83*0fca6ea1SDimitry Andric // - The unexpected result means no sys_info object was found and the time is
84*0fca6ea1SDimitry Andric //   the time to be used for the next search iteration.
85*0fca6ea1SDimitry Andric using __sys_info_result = expected<__sys_info, sys_seconds>;
86*0fca6ea1SDimitry Andric 
87*0fca6ea1SDimitry Andric template <ranges::forward_range _Range,
88*0fca6ea1SDimitry Andric           class _Type,
89*0fca6ea1SDimitry Andric           class _Proj                                                                                  = identity,
90*0fca6ea1SDimitry Andric           indirect_strict_weak_order<const _Type*, projected<ranges::iterator_t<_Range>, _Proj>> _Comp = ranges::less>
91*0fca6ea1SDimitry Andric [[nodiscard]] static ranges::borrowed_iterator_t<_Range>
92*0fca6ea1SDimitry Andric __binary_find(_Range&& __r, const _Type& __value, _Comp __comp = {}, _Proj __proj = {}) {
93*0fca6ea1SDimitry Andric   auto __end = ranges::end(__r);
94*0fca6ea1SDimitry Andric   auto __ret = ranges::lower_bound(ranges::begin(__r), __end, __value, __comp, __proj);
95*0fca6ea1SDimitry Andric   if (__ret == __end)
96*0fca6ea1SDimitry Andric     return __end;
97*0fca6ea1SDimitry Andric 
98*0fca6ea1SDimitry Andric   // When the value does not match the predicate it's equal and a valid result
99*0fca6ea1SDimitry Andric   // was found.
100*0fca6ea1SDimitry Andric   return !std::invoke(__comp, __value, std::invoke(__proj, *__ret)) ? __ret : __end;
101*0fca6ea1SDimitry Andric }
102*0fca6ea1SDimitry Andric 
103*0fca6ea1SDimitry Andric // Format based on https://data.iana.org/time-zones/tz-how-to.html
104*0fca6ea1SDimitry Andric //
105*0fca6ea1SDimitry Andric // 1  a time zone abbreviation that is a string of three or more characters that
106*0fca6ea1SDimitry Andric //    are either ASCII alphanumerics, "+", or "-"
107*0fca6ea1SDimitry Andric // 2  the string "%z", in which case the "%z" will be replaced by a numeric time
108*0fca6ea1SDimitry Andric //    zone abbreviation
109*0fca6ea1SDimitry Andric // 3  a pair of time zone abbreviations separated by a slash ('/'), in which
110*0fca6ea1SDimitry Andric //    case the first string is the abbreviation for the standard time name and
111*0fca6ea1SDimitry Andric //    the second string is the abbreviation for the daylight saving time name
112*0fca6ea1SDimitry Andric // 4  a string containing "%s", in which case the "%s" will be replaced by the
113*0fca6ea1SDimitry Andric //    text in the appropriate Rule's LETTER column, and the resulting string
114*0fca6ea1SDimitry Andric //    should be a time zone abbreviation
115*0fca6ea1SDimitry Andric //
116*0fca6ea1SDimitry Andric // Rule 1 is not strictly validated since America/Barbados uses a two letter
117*0fca6ea1SDimitry Andric // abbreviation AT.
118*0fca6ea1SDimitry Andric [[nodiscard]] static string
119*0fca6ea1SDimitry Andric __format(const __tz::__continuation& __continuation, const string& __letters, seconds __save) {
120*0fca6ea1SDimitry Andric   bool __shift = false;
121*0fca6ea1SDimitry Andric   string __result;
122*0fca6ea1SDimitry Andric   for (char __c : __continuation.__format) {
123*0fca6ea1SDimitry Andric     if (__shift) {
124*0fca6ea1SDimitry Andric       switch (__c) {
125*0fca6ea1SDimitry Andric       case 's':
126*0fca6ea1SDimitry Andric         std::ranges::copy(__letters, std::back_inserter(__result));
127*0fca6ea1SDimitry Andric         break;
128*0fca6ea1SDimitry Andric 
129*0fca6ea1SDimitry Andric       case 'z': {
130*0fca6ea1SDimitry Andric         if (__continuation.__format.size() != 2)
131*0fca6ea1SDimitry Andric           std::__throw_runtime_error(
132*0fca6ea1SDimitry Andric               std::format("corrupt tzdb FORMAT field: %z should be the entire contents, instead contains '{}'",
133*0fca6ea1SDimitry Andric                           __continuation.__format)
134*0fca6ea1SDimitry Andric                   .c_str());
135*0fca6ea1SDimitry Andric         chrono::hh_mm_ss __offset{__continuation.__stdoff + __save};
136*0fca6ea1SDimitry Andric         if (__offset.is_negative()) {
137*0fca6ea1SDimitry Andric           __result += '-';
138*0fca6ea1SDimitry Andric           __offset = chrono::hh_mm_ss{-(__continuation.__stdoff + __save)};
139*0fca6ea1SDimitry Andric         } else
140*0fca6ea1SDimitry Andric           __result += '+';
141*0fca6ea1SDimitry Andric 
142*0fca6ea1SDimitry Andric         if (__offset.minutes() != 0min)
143*0fca6ea1SDimitry Andric           std::format_to(std::back_inserter(__result), "{:%H%M}", __offset);
144*0fca6ea1SDimitry Andric         else
145*0fca6ea1SDimitry Andric           std::format_to(std::back_inserter(__result), "{:%H}", __offset);
146*0fca6ea1SDimitry Andric       } break;
147*0fca6ea1SDimitry Andric 
148*0fca6ea1SDimitry Andric       default:
149*0fca6ea1SDimitry Andric         std::__throw_runtime_error(
150*0fca6ea1SDimitry Andric             std::format("corrupt tzdb FORMAT field: invalid sequence '%{}' found, expected %s or %z", __c).c_str());
151*0fca6ea1SDimitry Andric       }
152*0fca6ea1SDimitry Andric       __shift = false;
153*0fca6ea1SDimitry Andric 
154*0fca6ea1SDimitry Andric     } else if (__c == '/') {
155*0fca6ea1SDimitry Andric       if (__save != 0s)
156*0fca6ea1SDimitry Andric         __result.clear();
157*0fca6ea1SDimitry Andric       else
158*0fca6ea1SDimitry Andric         break;
159*0fca6ea1SDimitry Andric 
160*0fca6ea1SDimitry Andric     } else if (__c == '%') {
161*0fca6ea1SDimitry Andric       __shift = true;
162*0fca6ea1SDimitry Andric     } else if (__c == '+' || __c == '-' || std::isalnum(__c)) {
163*0fca6ea1SDimitry Andric       __result.push_back(__c);
164*0fca6ea1SDimitry Andric     } else {
165*0fca6ea1SDimitry Andric       std::__throw_runtime_error(
166*0fca6ea1SDimitry Andric           std::format(
167*0fca6ea1SDimitry Andric               "corrupt tzdb FORMAT field: invalid character '{}' found, expected +, -, or an alphanumeric value", __c)
168*0fca6ea1SDimitry Andric               .c_str());
169*0fca6ea1SDimitry Andric     }
170*0fca6ea1SDimitry Andric   }
171*0fca6ea1SDimitry Andric 
172*0fca6ea1SDimitry Andric   if (__shift)
173*0fca6ea1SDimitry Andric     std::__throw_runtime_error("corrupt tzdb FORMAT field: input ended with the start of the escape sequence '%'");
174*0fca6ea1SDimitry Andric 
175*0fca6ea1SDimitry Andric   if (__result.empty())
176*0fca6ea1SDimitry Andric     std::__throw_runtime_error("corrupt tzdb FORMAT field: result is empty");
177*0fca6ea1SDimitry Andric 
178*0fca6ea1SDimitry Andric   return __result;
179*0fca6ea1SDimitry Andric }
180*0fca6ea1SDimitry Andric 
181*0fca6ea1SDimitry Andric [[nodiscard]] static sys_seconds __to_sys_seconds(year_month_day __ymd, seconds __seconds) {
182*0fca6ea1SDimitry Andric   seconds __result = static_cast<sys_days>(__ymd).time_since_epoch() + __seconds;
183*0fca6ea1SDimitry Andric   return sys_seconds{__result};
184*0fca6ea1SDimitry Andric }
185*0fca6ea1SDimitry Andric 
186*0fca6ea1SDimitry Andric [[nodiscard]] static seconds __at_to_sys_seconds(const __tz::__continuation& __continuation) {
187*0fca6ea1SDimitry Andric   switch (__continuation.__at.__clock) {
188*0fca6ea1SDimitry Andric   case __tz::__clock::__local:
189*0fca6ea1SDimitry Andric     return __continuation.__at.__time - __continuation.__stdoff -
190*0fca6ea1SDimitry Andric            std::visit(
191*0fca6ea1SDimitry Andric                [](const auto& __value) {
192*0fca6ea1SDimitry Andric                  using _Tp = decay_t<decltype(__value)>;
193*0fca6ea1SDimitry Andric                  if constexpr (same_as<_Tp, monostate>)
194*0fca6ea1SDimitry Andric                    return chrono::seconds{0};
195*0fca6ea1SDimitry Andric                  else if constexpr (same_as<_Tp, __tz::__save>)
196*0fca6ea1SDimitry Andric                    return chrono::duration_cast<seconds>(__value.__time);
197*0fca6ea1SDimitry Andric                  else if constexpr (same_as<_Tp, std::string>)
198*0fca6ea1SDimitry Andric                    // For a named rule based continuation the SAVE depends on the RULE
199*0fca6ea1SDimitry Andric                    // active at the end. This should be determined separately.
200*0fca6ea1SDimitry Andric                    return chrono::seconds{0};
201*0fca6ea1SDimitry Andric                  else
202*0fca6ea1SDimitry Andric                    static_assert(sizeof(_Tp) == 0); // TODO TZDB static_assert(false); after droping clang-16 support
203*0fca6ea1SDimitry Andric 
204*0fca6ea1SDimitry Andric                  std::__libcpp_unreachable();
205*0fca6ea1SDimitry Andric                },
206*0fca6ea1SDimitry Andric                __continuation.__rules);
207*0fca6ea1SDimitry Andric 
208*0fca6ea1SDimitry Andric   case __tz::__clock::__universal:
209*0fca6ea1SDimitry Andric     return __continuation.__at.__time;
210*0fca6ea1SDimitry Andric 
211*0fca6ea1SDimitry Andric   case __tz::__clock::__standard:
212*0fca6ea1SDimitry Andric     return __continuation.__at.__time - __continuation.__stdoff;
213*0fca6ea1SDimitry Andric   }
214*0fca6ea1SDimitry Andric   std::__libcpp_unreachable();
215*0fca6ea1SDimitry Andric }
216*0fca6ea1SDimitry Andric 
217*0fca6ea1SDimitry Andric [[nodiscard]] static year_month_day __to_year_month_day(year __year, month __month, __tz::__on __on) {
218*0fca6ea1SDimitry Andric   return std::visit(
219*0fca6ea1SDimitry Andric       [&](const auto& __value) {
220*0fca6ea1SDimitry Andric         using _Tp = decay_t<decltype(__value)>;
221*0fca6ea1SDimitry Andric         if constexpr (same_as<_Tp, chrono::day>)
222*0fca6ea1SDimitry Andric           return year_month_day{__year, __month, __value};
223*0fca6ea1SDimitry Andric         else if constexpr (same_as<_Tp, weekday_last>)
224*0fca6ea1SDimitry Andric           return year_month_day{static_cast<sys_days>(year_month_weekday_last{__year, __month, __value})};
225*0fca6ea1SDimitry Andric         else if constexpr (same_as<_Tp, __tz::__constrained_weekday>)
226*0fca6ea1SDimitry Andric           return __value(__year, __month);
227*0fca6ea1SDimitry Andric         else
228*0fca6ea1SDimitry Andric           static_assert(sizeof(_Tp) == 0); // TODO TZDB static_assert(false); after droping clang-16 support
229*0fca6ea1SDimitry Andric 
230*0fca6ea1SDimitry Andric         std::__libcpp_unreachable();
231*0fca6ea1SDimitry Andric       },
232*0fca6ea1SDimitry Andric       __on);
233*0fca6ea1SDimitry Andric }
234*0fca6ea1SDimitry Andric 
235*0fca6ea1SDimitry Andric [[nodiscard]] static sys_seconds __until_to_sys_seconds(const __tz::__continuation& __continuation) {
236*0fca6ea1SDimitry Andric   // Does UNTIL contain the magic value for the last continuation?
237*0fca6ea1SDimitry Andric   if (__continuation.__year == chrono::year::min())
238*0fca6ea1SDimitry Andric     return sys_seconds::max();
239*0fca6ea1SDimitry Andric 
240*0fca6ea1SDimitry Andric   year_month_day __ymd = chrono::__to_year_month_day(__continuation.__year, __continuation.__in, __continuation.__on);
241*0fca6ea1SDimitry Andric   return chrono::__to_sys_seconds(__ymd, chrono::__at_to_sys_seconds(__continuation));
242*0fca6ea1SDimitry Andric }
243*0fca6ea1SDimitry Andric 
244*0fca6ea1SDimitry Andric // Holds the UNTIL time for a continuation with a named rule.
245*0fca6ea1SDimitry Andric //
246*0fca6ea1SDimitry Andric // Unlike continuations with an fixed SAVE named rules have a variable SAVE.
247*0fca6ea1SDimitry Andric // This means when the UNTIL uses the local wall time the actual UNTIL value can
248*0fca6ea1SDimitry Andric // only be determined when the SAVE is known. This class holds that abstraction.
249*0fca6ea1SDimitry Andric class __named_rule_until {
250*0fca6ea1SDimitry Andric public:
251*0fca6ea1SDimitry Andric   explicit __named_rule_until(const __tz::__continuation& __continuation)
252*0fca6ea1SDimitry Andric       : __until_{chrono::__until_to_sys_seconds(__continuation)},
253*0fca6ea1SDimitry Andric         __needs_adjustment_{
254*0fca6ea1SDimitry Andric             // The last continuation of a ZONE has no UNTIL which basically is
255*0fca6ea1SDimitry Andric             // until the end of _local_ time. This value is expressed by
256*0fca6ea1SDimitry Andric             // sys_seconds::max(). Subtracting the SAVE leaves large value.
257*0fca6ea1SDimitry Andric             // However SAVE can be negative, which would add a value to maximum
258*0fca6ea1SDimitry Andric             // leading to undefined behaviour. In practice this often results in
259*0fca6ea1SDimitry Andric             // an overflow to a very small value.
260*0fca6ea1SDimitry Andric             __until_ != sys_seconds::max() && __continuation.__at.__clock == __tz::__clock::__local} {}
261*0fca6ea1SDimitry Andric 
262*0fca6ea1SDimitry Andric   // Gives the unadjusted until value, this is useful when the SAVE is not known
263*0fca6ea1SDimitry Andric   // at all.
264*0fca6ea1SDimitry Andric   sys_seconds __until() const noexcept { return __until_; }
265*0fca6ea1SDimitry Andric 
266*0fca6ea1SDimitry Andric   bool __needs_adjustment() const noexcept { return __needs_adjustment_; }
267*0fca6ea1SDimitry Andric 
268*0fca6ea1SDimitry Andric   // Returns the UNTIL adjusted for SAVE.
269*0fca6ea1SDimitry Andric   sys_seconds operator()(seconds __save) const noexcept { return __until_ - __needs_adjustment_ * __save; }
270*0fca6ea1SDimitry Andric 
271*0fca6ea1SDimitry Andric private:
272*0fca6ea1SDimitry Andric   sys_seconds __until_;
273*0fca6ea1SDimitry Andric   bool __needs_adjustment_;
274*0fca6ea1SDimitry Andric };
275*0fca6ea1SDimitry Andric 
276*0fca6ea1SDimitry Andric [[nodiscard]] static seconds __at_to_seconds(seconds __stdoff, const __tz::__rule& __rule) {
277*0fca6ea1SDimitry Andric   switch (__rule.__at.__clock) {
278*0fca6ea1SDimitry Andric   case __tz::__clock::__local:
279*0fca6ea1SDimitry Andric     // Local time and standard time behave the same. This is not
280*0fca6ea1SDimitry Andric     // correct. Local time needs to adjust for the current saved time.
281*0fca6ea1SDimitry Andric     // To know the saved time the rules need to be known and sorted.
282*0fca6ea1SDimitry Andric     // This needs a time so to avoid the chicken and egg adjust the
283*0fca6ea1SDimitry Andric     // saving of the local time later.
284*0fca6ea1SDimitry Andric     return __rule.__at.__time - __stdoff;
285*0fca6ea1SDimitry Andric 
286*0fca6ea1SDimitry Andric   case __tz::__clock::__universal:
287*0fca6ea1SDimitry Andric     return __rule.__at.__time;
288*0fca6ea1SDimitry Andric 
289*0fca6ea1SDimitry Andric   case __tz::__clock::__standard:
290*0fca6ea1SDimitry Andric     return __rule.__at.__time - __stdoff;
291*0fca6ea1SDimitry Andric   }
292*0fca6ea1SDimitry Andric   std::__libcpp_unreachable();
293*0fca6ea1SDimitry Andric }
294*0fca6ea1SDimitry Andric 
295*0fca6ea1SDimitry Andric [[nodiscard]] static sys_seconds __from_to_sys_seconds(seconds __stdoff, const __tz::__rule& __rule, year __year) {
296*0fca6ea1SDimitry Andric   year_month_day __ymd = chrono::__to_year_month_day(__year, __rule.__in, __rule.__on);
297*0fca6ea1SDimitry Andric 
298*0fca6ea1SDimitry Andric   seconds __at = chrono::__at_to_seconds(__stdoff, __rule);
299*0fca6ea1SDimitry Andric   return chrono::__to_sys_seconds(__ymd, __at);
300*0fca6ea1SDimitry Andric }
301*0fca6ea1SDimitry Andric 
302*0fca6ea1SDimitry Andric [[nodiscard]] static sys_seconds __from_to_sys_seconds(seconds __stdoff, const __tz::__rule& __rule) {
303*0fca6ea1SDimitry Andric   return chrono::__from_to_sys_seconds(__stdoff, __rule, __rule.__from);
304*0fca6ea1SDimitry Andric }
305*0fca6ea1SDimitry Andric 
306*0fca6ea1SDimitry Andric [[nodiscard]] static const vector<__tz::__rule>&
307*0fca6ea1SDimitry Andric __get_rules(const __tz::__rules_storage_type& __rules_db, const string& __rule_name) {
308*0fca6ea1SDimitry Andric   auto __result = chrono::__binary_find(__rules_db, __rule_name, {}, [](const auto& __p) { return __p.first; });
309*0fca6ea1SDimitry Andric   if (__result == std::end(__rules_db))
310*0fca6ea1SDimitry Andric     std::__throw_runtime_error(("corrupt tzdb: rule '" + __rule_name + " 'does not exist").c_str());
311*0fca6ea1SDimitry Andric 
312*0fca6ea1SDimitry Andric   return __result->second;
313*0fca6ea1SDimitry Andric }
314*0fca6ea1SDimitry Andric 
315*0fca6ea1SDimitry Andric // Returns the letters field for a time before the first rule.
316*0fca6ea1SDimitry Andric //
317*0fca6ea1SDimitry Andric // Per https://data.iana.org/time-zones/tz-how-to.html
318*0fca6ea1SDimitry Andric // One wrinkle, not fully explained in zic.8.txt, is what happens when switching
319*0fca6ea1SDimitry Andric // to a named rule. To what values should the SAVE and LETTER data be
320*0fca6ea1SDimitry Andric // initialized?
321*0fca6ea1SDimitry Andric //
322*0fca6ea1SDimitry Andric // 1 If at least one transition has happened, use the SAVE and LETTER data from
323*0fca6ea1SDimitry Andric //   the most recent.
324*0fca6ea1SDimitry Andric // 2 If switching to a named rule before any transition has happened, assume
325*0fca6ea1SDimitry Andric //   standard time (SAVE zero), and use the LETTER data from the earliest
326*0fca6ea1SDimitry Andric //   transition with a SAVE of zero.
327*0fca6ea1SDimitry Andric //
328*0fca6ea1SDimitry Andric // This function implements case 2.
329*0fca6ea1SDimitry Andric [[nodiscard]] static string __letters_before_first_rule(const vector<__tz::__rule>& __rules) {
330*0fca6ea1SDimitry Andric   auto __letters =
331*0fca6ea1SDimitry Andric       __rules                                                                                //
332*0fca6ea1SDimitry Andric       | views::filter([](const __tz::__rule& __rule) { return __rule.__save.__time == 0s; }) //
333*0fca6ea1SDimitry Andric       | views::transform([](const __tz::__rule& __rule) { return __rule.__letters; })        //
334*0fca6ea1SDimitry Andric       | views::take(1);
335*0fca6ea1SDimitry Andric 
336*0fca6ea1SDimitry Andric   if (__letters.empty())
337*0fca6ea1SDimitry Andric     std::__throw_runtime_error("corrupt tzdb: rule has zero entries");
338*0fca6ea1SDimitry Andric 
339*0fca6ea1SDimitry Andric   return __letters.front();
340*0fca6ea1SDimitry Andric }
341*0fca6ea1SDimitry Andric 
342*0fca6ea1SDimitry Andric // Determines the information based on the continuation and the rules.
343*0fca6ea1SDimitry Andric //
344*0fca6ea1SDimitry Andric // There are several special cases to take into account
345*0fca6ea1SDimitry Andric //
346*0fca6ea1SDimitry Andric // === Entries before the first rule becomes active ===
347*0fca6ea1SDimitry Andric // Asia/Hong_Kong
348*0fca6ea1SDimitry Andric //   9 - JST 1945 N 18 2        // (1)
349*0fca6ea1SDimitry Andric //   8 HK HK%sT                 // (2)
350*0fca6ea1SDimitry Andric //   R HK 1946 o - Ap 21 0 1 S  // (3)
351*0fca6ea1SDimitry Andric // There (1) is active until Novemer 18th 1945 at 02:00, after this time
352*0fca6ea1SDimitry Andric // (2) becomes active. The first rule entry for HK (3) becomes active
353*0fca6ea1SDimitry Andric // from April 21st 1945 at 01:00. In the period between (2) is active.
354*0fca6ea1SDimitry Andric // This entry has an offset.
355*0fca6ea1SDimitry Andric // This entry has no save, letters, or dst flag. So in the period
356*0fca6ea1SDimitry Andric // after (1) and until (3) no rule entry is associated with the time.
357*0fca6ea1SDimitry Andric 
358*0fca6ea1SDimitry Andric [[nodiscard]] static sys_info __get_sys_info_before_first_rule(
359*0fca6ea1SDimitry Andric     sys_seconds __begin,
360*0fca6ea1SDimitry Andric     sys_seconds __end,
361*0fca6ea1SDimitry Andric     const __tz::__continuation& __continuation,
362*0fca6ea1SDimitry Andric     const vector<__tz::__rule>& __rules) {
363*0fca6ea1SDimitry Andric   return sys_info{
364*0fca6ea1SDimitry Andric       __begin,
365*0fca6ea1SDimitry Andric       __end,
366*0fca6ea1SDimitry Andric       __continuation.__stdoff,
367*0fca6ea1SDimitry Andric       chrono::minutes(0),
368*0fca6ea1SDimitry Andric       chrono::__format(__continuation, __letters_before_first_rule(__rules), 0s)};
369*0fca6ea1SDimitry Andric }
370*0fca6ea1SDimitry Andric 
371*0fca6ea1SDimitry Andric // Returns the sys_info object for a time before the first rule.
372*0fca6ea1SDimitry Andric // When this first rule has a SAVE of 0s the sys_info for the time before the
373*0fca6ea1SDimitry Andric // first rule and for the first rule are identical and will be merged.
374*0fca6ea1SDimitry Andric [[nodiscard]] static sys_info __get_sys_info_before_first_rule(
375*0fca6ea1SDimitry Andric     sys_seconds __begin,
376*0fca6ea1SDimitry Andric     sys_seconds __rule_end, // The end used when SAVE != 0s
377*0fca6ea1SDimitry Andric     sys_seconds __next_end, // The end used when SAVE == 0s the times are merged
378*0fca6ea1SDimitry Andric     const __tz::__continuation& __continuation,
379*0fca6ea1SDimitry Andric     const vector<__tz::__rule>& __rules,
380*0fca6ea1SDimitry Andric     vector<__tz::__rule>::const_iterator __rule) {
381*0fca6ea1SDimitry Andric   if (__rule->__save.__time != 0s)
382*0fca6ea1SDimitry Andric     return __get_sys_info_before_first_rule(__begin, __rule_end, __continuation, __rules);
383*0fca6ea1SDimitry Andric 
384*0fca6ea1SDimitry Andric   return sys_info{
385*0fca6ea1SDimitry Andric       __begin, __next_end, __continuation.__stdoff, 0min, chrono::__format(__continuation, __rule->__letters, 0s)};
386*0fca6ea1SDimitry Andric }
387*0fca6ea1SDimitry Andric 
388*0fca6ea1SDimitry Andric [[nodiscard]] static seconds __at_to_seconds(seconds __stdoff, seconds __save, const __tz::__rule& __rule) {
389*0fca6ea1SDimitry Andric   switch (__rule.__at.__clock) {
390*0fca6ea1SDimitry Andric   case __tz::__clock::__local:
391*0fca6ea1SDimitry Andric     return __rule.__at.__time - __stdoff - __save;
392*0fca6ea1SDimitry Andric 
393*0fca6ea1SDimitry Andric   case __tz::__clock::__universal:
394*0fca6ea1SDimitry Andric     return __rule.__at.__time;
395*0fca6ea1SDimitry Andric 
396*0fca6ea1SDimitry Andric   case __tz::__clock::__standard:
397*0fca6ea1SDimitry Andric     return __rule.__at.__time - __stdoff;
398*0fca6ea1SDimitry Andric   }
399*0fca6ea1SDimitry Andric   std::__libcpp_unreachable();
400*0fca6ea1SDimitry Andric }
401*0fca6ea1SDimitry Andric 
402*0fca6ea1SDimitry Andric [[nodiscard]] static sys_seconds
403*0fca6ea1SDimitry Andric __rule_to_sys_seconds(seconds __stdoff, seconds __save, const __tz::__rule& __rule, year __year) {
404*0fca6ea1SDimitry Andric   year_month_day __ymd = chrono::__to_year_month_day(__year, __rule.__in, __rule.__on);
405*0fca6ea1SDimitry Andric 
406*0fca6ea1SDimitry Andric   seconds __at = chrono::__at_to_seconds(__stdoff, __save, __rule);
407*0fca6ea1SDimitry Andric   return chrono::__to_sys_seconds(__ymd, __at);
408*0fca6ea1SDimitry Andric }
409*0fca6ea1SDimitry Andric 
410*0fca6ea1SDimitry Andric // Returns the first rule after __time.
411*0fca6ea1SDimitry Andric // Note that a rule can be "active" in multiple years, this may result in an
412*0fca6ea1SDimitry Andric // infinite loop where the same rule is returned every time, use __current to
413*0fca6ea1SDimitry Andric // guard against that.
414*0fca6ea1SDimitry Andric //
415*0fca6ea1SDimitry Andric // When no next rule exists the returned time will be sys_seconds::max(). This
416*0fca6ea1SDimitry Andric // can happen in practice. For example,
417*0fca6ea1SDimitry Andric //
418*0fca6ea1SDimitry Andric //   R So 1945 o - May 24 2 2 M
419*0fca6ea1SDimitry Andric //   R So 1945 o - S 24 3 1 S
420*0fca6ea1SDimitry Andric //   R So 1945 o - N 18 2s 0 -
421*0fca6ea1SDimitry Andric //
422*0fca6ea1SDimitry Andric // Has 3 rules that are all only active in 1945.
423*0fca6ea1SDimitry Andric [[nodiscard]] static pair<sys_seconds, vector<__tz::__rule>::const_iterator>
424*0fca6ea1SDimitry Andric __next_rule(sys_seconds __time,
425*0fca6ea1SDimitry Andric             seconds __stdoff,
426*0fca6ea1SDimitry Andric             seconds __save,
427*0fca6ea1SDimitry Andric             const vector<__tz::__rule>& __rules,
428*0fca6ea1SDimitry Andric             vector<__tz::__rule>::const_iterator __current) {
429*0fca6ea1SDimitry Andric   year __year = year_month_day{chrono::floor<days>(__time)}.year();
430*0fca6ea1SDimitry Andric 
431*0fca6ea1SDimitry Andric   // Note it would probably be better to store the pairs in a vector and then
432*0fca6ea1SDimitry Andric   // use min() to get the smallest element
433*0fca6ea1SDimitry Andric   map<sys_seconds, vector<__tz::__rule>::const_iterator> __candidates;
434*0fca6ea1SDimitry Andric   // Note this evaluates all rules which is a waste of effort; when the entries
435*0fca6ea1SDimitry Andric   // are beyond the current year's "next year" (where "next year" is not always
436*0fca6ea1SDimitry Andric   // year + 1) the algorithm should end.
437*0fca6ea1SDimitry Andric   for (auto __it = __rules.begin(); __it != __rules.end(); ++__it) {
438*0fca6ea1SDimitry Andric     for (year __y = __it->__from; __y <= __it->__to; ++__y) {
439*0fca6ea1SDimitry Andric       // Adding the current entry for the current year may lead to infinite
440*0fca6ea1SDimitry Andric       // loops due to the SAVE adjustment. Skip these entries.
441*0fca6ea1SDimitry Andric       if (__y == __year && __it == __current)
442*0fca6ea1SDimitry Andric         continue;
443*0fca6ea1SDimitry Andric 
444*0fca6ea1SDimitry Andric       sys_seconds __t = chrono::__rule_to_sys_seconds(__stdoff, __save, *__it, __y);
445*0fca6ea1SDimitry Andric       if (__t <= __time)
446*0fca6ea1SDimitry Andric         continue;
447*0fca6ea1SDimitry Andric 
448*0fca6ea1SDimitry Andric       _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(!__candidates.contains(__t), "duplicated rule");
449*0fca6ea1SDimitry Andric       __candidates[__t] = __it;
450*0fca6ea1SDimitry Andric       break;
451*0fca6ea1SDimitry Andric     }
452*0fca6ea1SDimitry Andric   }
453*0fca6ea1SDimitry Andric 
454*0fca6ea1SDimitry Andric   if (!__candidates.empty()) [[likely]] {
455*0fca6ea1SDimitry Andric     auto __it = __candidates.begin();
456*0fca6ea1SDimitry Andric 
457*0fca6ea1SDimitry Andric     // When no rule is selected the time before the first rule and the first rule
458*0fca6ea1SDimitry Andric     // should not be merged.
459*0fca6ea1SDimitry Andric     if (__time == sys_seconds::min())
460*0fca6ea1SDimitry Andric       return *__it;
461*0fca6ea1SDimitry Andric 
462*0fca6ea1SDimitry Andric     // There can be two constitutive rules that are the same. For example,
463*0fca6ea1SDimitry Andric     // Hong Kong
464*0fca6ea1SDimitry Andric     //
465*0fca6ea1SDimitry Andric     // R HK 1973 o - D 30 3:30 1 S          (R1)
466*0fca6ea1SDimitry Andric     // R HK 1965 1976 - Ap Su>=16 3:30 1 S  (R2)
467*0fca6ea1SDimitry Andric     //
468*0fca6ea1SDimitry Andric     // 1973-12-29 19:30:00 R1 becomes active.
469*0fca6ea1SDimitry Andric     // 1974-04-20 18:30:00 R2 becomes active.
470*0fca6ea1SDimitry Andric     // Both rules have a SAVE of 1 hour and LETTERS are S for both of them.
471*0fca6ea1SDimitry Andric     while (__it != __candidates.end()) {
472*0fca6ea1SDimitry Andric       if (__current->__save.__time != __it->second->__save.__time || __current->__letters != __it->second->__letters)
473*0fca6ea1SDimitry Andric         return *__it;
474*0fca6ea1SDimitry Andric 
475*0fca6ea1SDimitry Andric       ++__it;
476*0fca6ea1SDimitry Andric     }
477*0fca6ea1SDimitry Andric   }
478*0fca6ea1SDimitry Andric 
479*0fca6ea1SDimitry Andric   return {sys_seconds::max(), __rules.end()};
480*0fca6ea1SDimitry Andric }
481*0fca6ea1SDimitry Andric 
482*0fca6ea1SDimitry Andric // Returns the first rule of a set of rules.
483*0fca6ea1SDimitry Andric // This is not always the first of the listed rules. For example
484*0fca6ea1SDimitry Andric //   R Sa 2008 2009 - Mar Su>=8 0 0 -
485*0fca6ea1SDimitry Andric //   R Sa 2007 2008 - O Su>=8 0 1 -
486*0fca6ea1SDimitry Andric // The transition in October 2007 happens before the transition in March 2008.
487*0fca6ea1SDimitry Andric [[nodiscard]] static vector<__tz::__rule>::const_iterator
488*0fca6ea1SDimitry Andric __first_rule(seconds __stdoff, const vector<__tz::__rule>& __rules) {
489*0fca6ea1SDimitry Andric   return chrono::__next_rule(sys_seconds::min(), __stdoff, 0s, __rules, __rules.end()).second;
490*0fca6ea1SDimitry Andric }
491*0fca6ea1SDimitry Andric 
492*0fca6ea1SDimitry Andric [[nodiscard]] static __sys_info_result __get_sys_info_rule(
493*0fca6ea1SDimitry Andric     sys_seconds __time,
494*0fca6ea1SDimitry Andric     sys_seconds __continuation_begin,
495*0fca6ea1SDimitry Andric     const __tz::__continuation& __continuation,
496*0fca6ea1SDimitry Andric     const vector<__tz::__rule>& __rules) {
497*0fca6ea1SDimitry Andric   auto __rule = chrono::__first_rule(__continuation.__stdoff, __rules);
498*0fca6ea1SDimitry Andric   _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(__rule != __rules.end(), "the set of rules has no first rule");
499*0fca6ea1SDimitry Andric 
500*0fca6ea1SDimitry Andric   // Avoid selecting a time before the start of the continuation
501*0fca6ea1SDimitry Andric   __time = std::max(__time, __continuation_begin);
502*0fca6ea1SDimitry Andric 
503*0fca6ea1SDimitry Andric   sys_seconds __rule_begin = chrono::__from_to_sys_seconds(__continuation.__stdoff, *__rule);
504*0fca6ea1SDimitry Andric 
505*0fca6ea1SDimitry Andric   // The time sought is very likely inside the current rule.
506*0fca6ea1SDimitry Andric   // When the continuation's UNTIL uses the local clock there are edge cases
507*0fca6ea1SDimitry Andric   // where this is not true.
508*0fca6ea1SDimitry Andric   //
509*0fca6ea1SDimitry Andric   // Start to walk the rules to find the proper one.
510*0fca6ea1SDimitry Andric   //
511*0fca6ea1SDimitry Andric   // For now we just walk all the rules TODO TZDB investigate whether a smarter
512*0fca6ea1SDimitry Andric   // algorithm would work.
513*0fca6ea1SDimitry Andric   auto __next = chrono::__next_rule(__rule_begin, __continuation.__stdoff, __rule->__save.__time, __rules, __rule);
514*0fca6ea1SDimitry Andric 
515*0fca6ea1SDimitry Andric   // Ignore small steps, this happens with America/Punta_Arenas for the
516*0fca6ea1SDimitry Andric   // transition
517*0fca6ea1SDimitry Andric   // -4:42:46 - SMT 1927 S
518*0fca6ea1SDimitry Andric   // -5 x -05/-04 1932 S
519*0fca6ea1SDimitry Andric   // ...
520*0fca6ea1SDimitry Andric   //
521*0fca6ea1SDimitry Andric   // R x 1927 1931 - S 1 0 1 -
522*0fca6ea1SDimitry Andric   // R x 1928 1932 - Ap 1 0 0 -
523*0fca6ea1SDimitry Andric   //
524*0fca6ea1SDimitry Andric   // America/Punta_Arenas  Thu Sep  1 04:42:45 1927 UT = Thu Sep  1 00:42:45 1927 -04 isdst=1 gmtoff=-14400
525*0fca6ea1SDimitry Andric   // America/Punta_Arenas  Sun Apr  1 03:59:59 1928 UT = Sat Mar 31 23:59:59 1928 -04 isdst=1 gmtoff=-14400
526*0fca6ea1SDimitry Andric   // America/Punta_Arenas  Sun Apr  1 04:00:00 1928 UT = Sat Mar 31 23:00:00 1928 -05 isdst=0 gmtoff=-18000
527*0fca6ea1SDimitry Andric   //
528*0fca6ea1SDimitry Andric   // Without this there will be a transition
529*0fca6ea1SDimitry Andric   //   [1927-09-01 04:42:45, 1927-09-01 05:00:00) -05:00:00 0min -05
530*0fca6ea1SDimitry Andric 
531*0fca6ea1SDimitry Andric   if (sys_seconds __begin = __rule->__save.__time != 0s ? __rule_begin : __next.first; __time < __begin) {
532*0fca6ea1SDimitry Andric     if (__continuation_begin == sys_seconds::min() || __begin - __continuation_begin > 12h)
533*0fca6ea1SDimitry Andric       return __sys_info{__get_sys_info_before_first_rule(
534*0fca6ea1SDimitry Andric                             __continuation_begin, __rule_begin, __next.first, __continuation, __rules, __rule),
535*0fca6ea1SDimitry Andric                         false};
536*0fca6ea1SDimitry Andric 
537*0fca6ea1SDimitry Andric     // Europe/Berlin
538*0fca6ea1SDimitry Andric     // 1 c CE%sT 1945 May 24 2          (C1)
539*0fca6ea1SDimitry Andric     // 1 So CE%sT 1946                  (C2)
540*0fca6ea1SDimitry Andric     //
541*0fca6ea1SDimitry Andric     // R c 1944 1945 - Ap M>=1 2s 1 S   (R1)
542*0fca6ea1SDimitry Andric     //
543*0fca6ea1SDimitry Andric     // R So 1945 o - May 24 2 2 M       (R2)
544*0fca6ea1SDimitry Andric     //
545*0fca6ea1SDimitry Andric     // When C2 becomes active the time would be before the first rule R2,
546*0fca6ea1SDimitry Andric     // giving a 1 hour sys_info.
547*0fca6ea1SDimitry Andric     seconds __save = __rule->__save.__time;
548*0fca6ea1SDimitry Andric     __named_rule_until __continuation_end{__continuation};
549*0fca6ea1SDimitry Andric     sys_seconds __sys_info_end = std::min(__continuation_end(__save), __next.first);
550*0fca6ea1SDimitry Andric 
551*0fca6ea1SDimitry Andric     return __sys_info{
552*0fca6ea1SDimitry Andric         sys_info{__continuation_begin,
553*0fca6ea1SDimitry Andric                  __sys_info_end,
554*0fca6ea1SDimitry Andric                  __continuation.__stdoff + __save,
555*0fca6ea1SDimitry Andric                  chrono::duration_cast<minutes>(__save),
556*0fca6ea1SDimitry Andric                  chrono::__format(__continuation, __rule->__letters, __save)},
557*0fca6ea1SDimitry Andric         __sys_info_end == __continuation_end(__save)};
558*0fca6ea1SDimitry Andric   }
559*0fca6ea1SDimitry Andric 
560*0fca6ea1SDimitry Andric   // See above for America/Asuncion
561*0fca6ea1SDimitry Andric   if (__rule->__save.__time == 0s && __time < __next.first) {
562*0fca6ea1SDimitry Andric     return __sys_info{
563*0fca6ea1SDimitry Andric         sys_info{__continuation_begin,
564*0fca6ea1SDimitry Andric                  __next.first,
565*0fca6ea1SDimitry Andric                  __continuation.__stdoff,
566*0fca6ea1SDimitry Andric                  0min,
567*0fca6ea1SDimitry Andric                  chrono::__format(__continuation, __rule->__letters, 0s)},
568*0fca6ea1SDimitry Andric         false};
569*0fca6ea1SDimitry Andric   }
570*0fca6ea1SDimitry Andric 
571*0fca6ea1SDimitry Andric   if (__rule->__save.__time != 0s) {
572*0fca6ea1SDimitry Andric     // another fix for America/Punta_Arenas when not at the start of the
573*0fca6ea1SDimitry Andric     // sys_info object.
574*0fca6ea1SDimitry Andric     seconds __save = __rule->__save.__time;
575*0fca6ea1SDimitry Andric     if (__continuation_begin >= __rule_begin - __save && __time < __next.first) {
576*0fca6ea1SDimitry Andric       return __sys_info{
577*0fca6ea1SDimitry Andric           sys_info{__continuation_begin,
578*0fca6ea1SDimitry Andric                    __next.first,
579*0fca6ea1SDimitry Andric                    __continuation.__stdoff + __save,
580*0fca6ea1SDimitry Andric                    chrono::duration_cast<minutes>(__save),
581*0fca6ea1SDimitry Andric                    chrono::__format(__continuation, __rule->__letters, __save)},
582*0fca6ea1SDimitry Andric           false};
583*0fca6ea1SDimitry Andric     }
584*0fca6ea1SDimitry Andric   }
585*0fca6ea1SDimitry Andric 
586*0fca6ea1SDimitry Andric   __named_rule_until __continuation_end{__continuation};
587*0fca6ea1SDimitry Andric   while (__next.second != __rules.end()) {
588*0fca6ea1SDimitry Andric #ifdef PRINT
589*0fca6ea1SDimitry Andric     std::print(
590*0fca6ea1SDimitry Andric         stderr,
591*0fca6ea1SDimitry Andric         "Rule for {}: [{}, {}) off={} save={} duration={}\n",
592*0fca6ea1SDimitry Andric         __time,
593*0fca6ea1SDimitry Andric         __rule_begin,
594*0fca6ea1SDimitry Andric         __next.first,
595*0fca6ea1SDimitry Andric         __continuation.__stdoff,
596*0fca6ea1SDimitry Andric         __rule->__save.__time,
597*0fca6ea1SDimitry Andric         __next.first - __rule_begin);
598*0fca6ea1SDimitry Andric #endif
599*0fca6ea1SDimitry Andric 
600*0fca6ea1SDimitry Andric     sys_seconds __end = __continuation_end(__rule->__save.__time);
601*0fca6ea1SDimitry Andric 
602*0fca6ea1SDimitry Andric     sys_seconds __sys_info_begin = std::max(__continuation_begin, __rule_begin);
603*0fca6ea1SDimitry Andric     sys_seconds __sys_info_end   = std::min(__end, __next.first);
604*0fca6ea1SDimitry Andric     seconds __diff               = chrono::abs(__sys_info_end - __sys_info_begin);
605*0fca6ea1SDimitry Andric 
606*0fca6ea1SDimitry Andric     if (__diff < 12h) {
607*0fca6ea1SDimitry Andric       // Z America/Argentina/Buenos_Aires -3:53:48 - LMT 1894 O 31
608*0fca6ea1SDimitry Andric       // -4:16:48 - CMT 1920 May
609*0fca6ea1SDimitry Andric       // -4 - -04 1930 D
610*0fca6ea1SDimitry Andric       // -4 A -04/-03 1969 O 5
611*0fca6ea1SDimitry Andric       // -3 A -03/-02 1999 O 3
612*0fca6ea1SDimitry Andric       // -4 A -04/-03 2000 Mar 3
613*0fca6ea1SDimitry Andric       // ...
614*0fca6ea1SDimitry Andric       //
615*0fca6ea1SDimitry Andric       // ...
616*0fca6ea1SDimitry Andric       // R A 1989 1992 - O Su>=15 0 1 -
617*0fca6ea1SDimitry Andric       // R A 1999 o - O Su>=1 0 1 -
618*0fca6ea1SDimitry Andric       // R A 2000 o - Mar 3 0 0 -
619*0fca6ea1SDimitry Andric       // R A 2007 o - D 30 0 1 -
620*0fca6ea1SDimitry Andric       // ...
621*0fca6ea1SDimitry Andric 
622*0fca6ea1SDimitry Andric       // The 1999 switch uses the same rule, but with a different stdoff.
623*0fca6ea1SDimitry Andric       //   R A 1999 o - O Su>=1 0 1 -
624*0fca6ea1SDimitry Andric       //     stdoff -3 -> 1999-10-03 03:00:00
625*0fca6ea1SDimitry Andric       //     stdoff -4 -> 1999-10-03 04:00:00
626*0fca6ea1SDimitry Andric       // This generates an invalid entry and this is evaluated as a transition.
627*0fca6ea1SDimitry Andric       // Looking at the zdump like output in libc++ this generates jumps in
628*0fca6ea1SDimitry Andric       // the UTC time.
629*0fca6ea1SDimitry Andric 
630*0fca6ea1SDimitry Andric       __rule         = __next.second;
631*0fca6ea1SDimitry Andric       __next         = __next_rule(__next.first, __continuation.__stdoff, __rule->__save.__time, __rules, __rule);
632*0fca6ea1SDimitry Andric       __end          = __continuation_end(__rule->__save.__time);
633*0fca6ea1SDimitry Andric       __sys_info_end = std::min(__end, __next.first);
634*0fca6ea1SDimitry Andric     }
635*0fca6ea1SDimitry Andric 
636*0fca6ea1SDimitry Andric     if ((__time >= __rule_begin && __time < __next.first) || __next.first >= __end) {
637*0fca6ea1SDimitry Andric       __sys_info_begin = std::max(__continuation_begin, __rule_begin);
638*0fca6ea1SDimitry Andric       __sys_info_end   = std::min(__end, __next.first);
639*0fca6ea1SDimitry Andric 
640*0fca6ea1SDimitry Andric       return __sys_info{
641*0fca6ea1SDimitry Andric           sys_info{__sys_info_begin,
642*0fca6ea1SDimitry Andric                    __sys_info_end,
643*0fca6ea1SDimitry Andric                    __continuation.__stdoff + __rule->__save.__time,
644*0fca6ea1SDimitry Andric                    chrono::duration_cast<minutes>(__rule->__save.__time),
645*0fca6ea1SDimitry Andric                    chrono::__format(__continuation, __rule->__letters, __rule->__save.__time)},
646*0fca6ea1SDimitry Andric           __sys_info_end == __end};
647*0fca6ea1SDimitry Andric     }
648*0fca6ea1SDimitry Andric 
649*0fca6ea1SDimitry Andric     __rule_begin = __next.first;
650*0fca6ea1SDimitry Andric     __rule       = __next.second;
651*0fca6ea1SDimitry Andric     __next       = __next_rule(__rule_begin, __continuation.__stdoff, __rule->__save.__time, __rules, __rule);
652*0fca6ea1SDimitry Andric   }
653*0fca6ea1SDimitry Andric 
654*0fca6ea1SDimitry Andric   return __sys_info{
655*0fca6ea1SDimitry Andric       sys_info{std::max(__continuation_begin, __rule_begin),
656*0fca6ea1SDimitry Andric                __continuation_end(__rule->__save.__time),
657*0fca6ea1SDimitry Andric                __continuation.__stdoff + __rule->__save.__time,
658*0fca6ea1SDimitry Andric                chrono::duration_cast<minutes>(__rule->__save.__time),
659*0fca6ea1SDimitry Andric                chrono::__format(__continuation, __rule->__letters, __rule->__save.__time)},
660*0fca6ea1SDimitry Andric       true};
661*0fca6ea1SDimitry Andric }
662*0fca6ea1SDimitry Andric 
663*0fca6ea1SDimitry Andric [[nodiscard]] static __sys_info_result __get_sys_info_basic(
664*0fca6ea1SDimitry Andric     sys_seconds __time, sys_seconds __continuation_begin, const __tz::__continuation& __continuation, seconds __save) {
665*0fca6ea1SDimitry Andric   sys_seconds __continuation_end = chrono::__until_to_sys_seconds(__continuation);
666*0fca6ea1SDimitry Andric   return __sys_info{
667*0fca6ea1SDimitry Andric       sys_info{__continuation_begin,
668*0fca6ea1SDimitry Andric                __continuation_end,
669*0fca6ea1SDimitry Andric                __continuation.__stdoff + __save,
670*0fca6ea1SDimitry Andric                chrono::duration_cast<minutes>(__save),
671*0fca6ea1SDimitry Andric                __continuation.__format},
672*0fca6ea1SDimitry Andric       true};
673*0fca6ea1SDimitry Andric }
674*0fca6ea1SDimitry Andric 
675*0fca6ea1SDimitry Andric [[nodiscard]] static __sys_info_result
676*0fca6ea1SDimitry Andric __get_sys_info(sys_seconds __time,
677*0fca6ea1SDimitry Andric                sys_seconds __continuation_begin,
678*0fca6ea1SDimitry Andric                const __tz::__continuation& __continuation,
679*0fca6ea1SDimitry Andric                const __tz::__rules_storage_type& __rules_db) {
680*0fca6ea1SDimitry Andric   return std::visit(
681*0fca6ea1SDimitry Andric       [&](const auto& __value) {
682*0fca6ea1SDimitry Andric         using _Tp = decay_t<decltype(__value)>;
683*0fca6ea1SDimitry Andric         if constexpr (same_as<_Tp, std::string>)
684*0fca6ea1SDimitry Andric           return chrono::__get_sys_info_rule(
685*0fca6ea1SDimitry Andric               __time, __continuation_begin, __continuation, __get_rules(__rules_db, __value));
686*0fca6ea1SDimitry Andric         else if constexpr (same_as<_Tp, monostate>)
687*0fca6ea1SDimitry Andric           return chrono::__get_sys_info_basic(__time, __continuation_begin, __continuation, chrono::seconds(0));
688*0fca6ea1SDimitry Andric         else if constexpr (same_as<_Tp, __tz::__save>)
689*0fca6ea1SDimitry Andric           return chrono::__get_sys_info_basic(__time, __continuation_begin, __continuation, __value.__time);
690*0fca6ea1SDimitry Andric         else
691*0fca6ea1SDimitry Andric           static_assert(sizeof(_Tp) == 0); // TODO TZDB static_assert(false); after droping clang-16 support
692*0fca6ea1SDimitry Andric 
693*0fca6ea1SDimitry Andric         std::__libcpp_unreachable();
694*0fca6ea1SDimitry Andric       },
695*0fca6ea1SDimitry Andric       __continuation.__rules);
696*0fca6ea1SDimitry Andric }
697*0fca6ea1SDimitry Andric 
698*0fca6ea1SDimitry Andric // The transition from one continuation to the next continuation may result in
699*0fca6ea1SDimitry Andric // two constitutive continuations with the same "offset" information.
700*0fca6ea1SDimitry Andric // [time.zone.info.sys]/3
701*0fca6ea1SDimitry Andric //   The begin and end data members indicate that, for the associated time_zone
702*0fca6ea1SDimitry Andric //   and time_point, the offset and abbrev are in effect in the range
703*0fca6ea1SDimitry Andric //   [begin, end). This information can be used to efficiently iterate the
704*0fca6ea1SDimitry Andric //   transitions of a time_zone.
705*0fca6ea1SDimitry Andric //
706*0fca6ea1SDimitry Andric // Note that this does considers a change in the SAVE field not to be a
707*0fca6ea1SDimitry Andric // different sys_info, zdump does consider this different.
708*0fca6ea1SDimitry Andric //   LWG XXXX The sys_info range should be affected by save
709*0fca6ea1SDimitry Andric // matches the behaviour of the Standard and zdump.
710*0fca6ea1SDimitry Andric //
711*0fca6ea1SDimitry Andric // Iff the "offsets" are the same '__current.__end' is replaced with
712*0fca6ea1SDimitry Andric // '__next.__end', which effectively merges the two objects in one object. The
713*0fca6ea1SDimitry Andric // function returns true if a merge occurred.
714*0fca6ea1SDimitry Andric [[nodiscard]] bool __merge_continuation(sys_info& __current, const sys_info& __next) {
715*0fca6ea1SDimitry Andric   if (__current.end != __next.begin)
716*0fca6ea1SDimitry Andric     return false;
717*0fca6ea1SDimitry Andric 
718*0fca6ea1SDimitry Andric   if (__current.offset != __next.offset || __current.abbrev != __next.abbrev || __current.save != __next.save)
719*0fca6ea1SDimitry Andric     return false;
720*0fca6ea1SDimitry Andric 
721*0fca6ea1SDimitry Andric   __current.end = __next.end;
722*0fca6ea1SDimitry Andric   return true;
723*0fca6ea1SDimitry Andric }
724*0fca6ea1SDimitry Andric 
725*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
726*0fca6ea1SDimitry Andric //                           Public API
727*0fca6ea1SDimitry Andric //===----------------------------------------------------------------------===//
728*0fca6ea1SDimitry Andric 
729*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI time_zone time_zone::__create(unique_ptr<time_zone::__impl>&& __p) {
730*0fca6ea1SDimitry Andric   _LIBCPP_ASSERT_NON_NULL(__p != nullptr, "initialized time_zone without a valid pimpl object");
731*0fca6ea1SDimitry Andric   time_zone result;
732*0fca6ea1SDimitry Andric   result.__impl_ = std::move(__p);
733*0fca6ea1SDimitry Andric   return result;
734*0fca6ea1SDimitry Andric }
735*0fca6ea1SDimitry Andric 
736*0fca6ea1SDimitry Andric _LIBCPP_EXPORTED_FROM_ABI time_zone::~time_zone() = default;
737*0fca6ea1SDimitry Andric 
738*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_EXPORTED_FROM_ABI string_view time_zone::__name() const noexcept { return __impl_->__name(); }
739*0fca6ea1SDimitry Andric 
740*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI sys_info
741*0fca6ea1SDimitry Andric time_zone::__get_info(sys_seconds __time) const {
742*0fca6ea1SDimitry Andric   optional<sys_info> __result;
743*0fca6ea1SDimitry Andric   bool __valid_result = false; // true iff __result.has_value() is true and
744*0fca6ea1SDimitry Andric                                // __result.begin <= __time < __result.end is true.
745*0fca6ea1SDimitry Andric   bool __can_merge                 = false;
746*0fca6ea1SDimitry Andric   sys_seconds __continuation_begin = sys_seconds::min();
747*0fca6ea1SDimitry Andric   // Iterates over the Zone entry and its continuations. Internally the Zone
748*0fca6ea1SDimitry Andric   // entry is split in a Zone information and the first continuation. The last
749*0fca6ea1SDimitry Andric   // continuation has no UNTIL field. This means the loop should always find a
750*0fca6ea1SDimitry Andric   // continuation.
751*0fca6ea1SDimitry Andric   //
752*0fca6ea1SDimitry Andric   // For more information on background of zone information please consult the
753*0fca6ea1SDimitry Andric   // following information
754*0fca6ea1SDimitry Andric   //   [zic manual](https://www.man7.org/linux/man-pages/man8/zic.8.html)
755*0fca6ea1SDimitry Andric   //   [tz source info](https://data.iana.org/time-zones/tz-how-to.html)
756*0fca6ea1SDimitry Andric   //   On POSIX systems the zdump tool can be useful:
757*0fca6ea1SDimitry Andric   //     zdump -v Asia/Hong_Kong
758*0fca6ea1SDimitry Andric   //   Gives all transitions in the Hong Kong time zone.
759*0fca6ea1SDimitry Andric   //
760*0fca6ea1SDimitry Andric   // During iteration the result for the current continuation is returned. If
761*0fca6ea1SDimitry Andric   // no continuation is applicable it will return the end time as "error". When
762*0fca6ea1SDimitry Andric   // two continuations are contiguous and contain the "same" information these
763*0fca6ea1SDimitry Andric   // ranges are merged as one range.
764*0fca6ea1SDimitry Andric   // The merging requires keeping any result that occurs before __time,
765*0fca6ea1SDimitry Andric   // likewise when a valid result is found the algorithm needs to test the next
766*0fca6ea1SDimitry Andric   // continuation to see whether it can be merged. For example, Africa/Ceuta
767*0fca6ea1SDimitry Andric   // Continuations
768*0fca6ea1SDimitry Andric   //  0 s WE%sT 1929                   (C1)
769*0fca6ea1SDimitry Andric   //  0 - WET 1967                     (C2)
770*0fca6ea1SDimitry Andric   //  0 Sp WE%sT 1984 Mar 16           (C3)
771*0fca6ea1SDimitry Andric   //
772*0fca6ea1SDimitry Andric   // Rules
773*0fca6ea1SDimitry Andric   //  R s 1926 1929 - O Sa>=1 24s 0 -  (R1)
774*0fca6ea1SDimitry Andric   //
775*0fca6ea1SDimitry Andric   //  R Sp 1967 o - Jun 3 12 1 S       (R2)
776*0fca6ea1SDimitry Andric   //
777*0fca6ea1SDimitry Andric   // The rule R1 is the last rule used in C1. The rule R2 is the first rule in
778*0fca6ea1SDimitry Andric   // C3. Since R2 is the first rule this means when a continuation uses this
779*0fca6ea1SDimitry Andric   // rule its value prior to R2 will be SAVE 0 LETTERS of the first entry with a
780*0fca6ea1SDimitry Andric   // SAVE of 0, in this case WET.
781*0fca6ea1SDimitry Andric   // This gives the following changes in the information.
782*0fca6ea1SDimitry Andric   //   1928-10-07 00:00:00 C1 R1 becomes active: offset 0 save 0 abbrev WET
783*0fca6ea1SDimitry Andric   //   1929-01-01 00:00:00 C2    becomes active: offset 0 save 0 abbrev WET
784*0fca6ea1SDimitry Andric   //   1967-01-01 00:00:00 C3    becomes active: offset 0 save 0 abbrev WET
785*0fca6ea1SDimitry Andric   //   1967-06-03 12:00:00 C3 R2 becomes active: offset 0 save 1 abbrev WEST
786*0fca6ea1SDimitry Andric   //
787*0fca6ea1SDimitry Andric   // The first 3 entries are contiguous and contain the same information, this
788*0fca6ea1SDimitry Andric   // means the period [1928-10-07 00:00:00, 1967-06-03 12:00:00) should be
789*0fca6ea1SDimitry Andric   // returned in one sys_info object.
790*0fca6ea1SDimitry Andric 
791*0fca6ea1SDimitry Andric   const auto& __continuations                  = __impl_->__continuations();
792*0fca6ea1SDimitry Andric   const __tz::__rules_storage_type& __rules_db = __impl_->__rules_db();
793*0fca6ea1SDimitry Andric   for (auto __it = __continuations.begin(); __it != __continuations.end(); ++__it) {
794*0fca6ea1SDimitry Andric     const auto& __continuation   = *__it;
795*0fca6ea1SDimitry Andric     __sys_info_result __sys_info = chrono::__get_sys_info(__time, __continuation_begin, __continuation, __rules_db);
796*0fca6ea1SDimitry Andric 
797*0fca6ea1SDimitry Andric     if (__sys_info) {
798*0fca6ea1SDimitry Andric       _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
799*0fca6ea1SDimitry Andric           __sys_info->__info.begin < __sys_info->__info.end, "invalid sys_info range");
800*0fca6ea1SDimitry Andric 
801*0fca6ea1SDimitry Andric       // Filters out dummy entries
802*0fca6ea1SDimitry Andric       // Z America/Argentina/Buenos_Aires -3:53:48 - LMT 1894 O 31
803*0fca6ea1SDimitry Andric       // ...
804*0fca6ea1SDimitry Andric       // -4 A -04/-03 2000 Mar 3 (C1)
805*0fca6ea1SDimitry Andric       // -3 A -03/-02            (C2)
806*0fca6ea1SDimitry Andric       //
807*0fca6ea1SDimitry Andric       // ...
808*0fca6ea1SDimitry Andric       // R A 2000 o - Mar 3 0 0 -
809*0fca6ea1SDimitry Andric       // R A 2007 o - D 30 0 1 -
810*0fca6ea1SDimitry Andric       // ...
811*0fca6ea1SDimitry Andric       //
812*0fca6ea1SDimitry Andric       // This results in an entry
813*0fca6ea1SDimitry Andric       //   [2000-03-03 03:00:00, 2000-03-03 04:00:00) -10800s 60min -03
814*0fca6ea1SDimitry Andric       // for [C1 & R1, C1, R2) which due to the end of the continuation is an
815*0fca6ea1SDimitry Andric       // one hour "sys_info". Instead the entry should be ignored and replaced
816*0fca6ea1SDimitry Andric       // by [C2 & R1, C2 & R2) which is the proper range
817*0fca6ea1SDimitry Andric       //   "[2000-03-03 03:00:00, 2007-12-30 03:00:00) -02:00:00 60min -02
818*0fca6ea1SDimitry Andric 
819*0fca6ea1SDimitry Andric       if (std::holds_alternative<string>(__continuation.__rules) && __sys_info->__can_merge &&
820*0fca6ea1SDimitry Andric           __sys_info->__info.begin + 12h > __sys_info->__info.end) {
821*0fca6ea1SDimitry Andric         __continuation_begin = __sys_info->__info.begin;
822*0fca6ea1SDimitry Andric         continue;
823*0fca6ea1SDimitry Andric       }
824*0fca6ea1SDimitry Andric 
825*0fca6ea1SDimitry Andric       if (!__result) {
826*0fca6ea1SDimitry Andric         // First entry found, always keep it.
827*0fca6ea1SDimitry Andric         __result = __sys_info->__info;
828*0fca6ea1SDimitry Andric 
829*0fca6ea1SDimitry Andric         __valid_result = __time >= __result->begin && __time < __result->end;
830*0fca6ea1SDimitry Andric         __can_merge    = __sys_info->__can_merge;
831*0fca6ea1SDimitry Andric       } else if (__can_merge && chrono::__merge_continuation(*__result, __sys_info->__info)) {
832*0fca6ea1SDimitry Andric         // The results are merged, update the result state. This may
833*0fca6ea1SDimitry Andric         // "overwrite" a valid sys_info object with another valid sys_info
834*0fca6ea1SDimitry Andric         // object.
835*0fca6ea1SDimitry Andric         __valid_result = __time >= __result->begin && __time < __result->end;
836*0fca6ea1SDimitry Andric         __can_merge    = __sys_info->__can_merge;
837*0fca6ea1SDimitry Andric       } else {
838*0fca6ea1SDimitry Andric         // Here things get interesting:
839*0fca6ea1SDimitry Andric         // For example, America/Argentina/San_Luis
840*0fca6ea1SDimitry Andric         //
841*0fca6ea1SDimitry Andric         //   -3 A -03/-02 2008 Ja 21           (C1)
842*0fca6ea1SDimitry Andric         //   -4 Sa -04/-03 2009 O 11           (C2)
843*0fca6ea1SDimitry Andric         //
844*0fca6ea1SDimitry Andric         //   R A 2007 o - D 30 0 1 -           (R1)
845*0fca6ea1SDimitry Andric         //
846*0fca6ea1SDimitry Andric         //   R Sa 2007 2008 - O Su>=8 0 1 -    (R2)
847*0fca6ea1SDimitry Andric         //
848*0fca6ea1SDimitry Andric         // Based on C1 & R1 the end time of C1 is 2008-01-21 03:00:00
849*0fca6ea1SDimitry Andric         // Based on C2 & R2 the end time of C1 is 2008-01-21 02:00:00
850*0fca6ea1SDimitry Andric         // In this case the earlier time is the real time of the transition.
851*0fca6ea1SDimitry Andric         // However the algorithm used gives 2008-01-21 03:00:00.
852*0fca6ea1SDimitry Andric         //
853*0fca6ea1SDimitry Andric         // So we need to calculate the previous UNTIL in the current context and
854*0fca6ea1SDimitry Andric         // see whether it's earlier.
855*0fca6ea1SDimitry Andric 
856*0fca6ea1SDimitry Andric         // The results could not be merged.
857*0fca6ea1SDimitry Andric         // - When we have a valid result that result is the final result.
858*0fca6ea1SDimitry Andric         // - Otherwise the result we had is before __time and the result we got
859*0fca6ea1SDimitry Andric         //   is at a later time (possibly valid). This result is always better
860*0fca6ea1SDimitry Andric         //   than the previous result.
861*0fca6ea1SDimitry Andric         if (__valid_result) {
862*0fca6ea1SDimitry Andric           return *__result;
863*0fca6ea1SDimitry Andric         } else {
864*0fca6ea1SDimitry Andric           _LIBCPP_ASSERT_ARGUMENT_WITHIN_DOMAIN(
865*0fca6ea1SDimitry Andric               __it != __continuations.begin(), "the first rule should always seed the result");
866*0fca6ea1SDimitry Andric           const auto& __last = *(__it - 1);
867*0fca6ea1SDimitry Andric           if (std::holds_alternative<string>(__last.__rules)) {
868*0fca6ea1SDimitry Andric             // Europe/Berlin
869*0fca6ea1SDimitry Andric             // 1 c CE%sT 1945 May 24 2          (C1)
870*0fca6ea1SDimitry Andric             // 1 So CE%sT 1946                  (C2)
871*0fca6ea1SDimitry Andric             //
872*0fca6ea1SDimitry Andric             // R c 1944 1945 - Ap M>=1 2s 1 S   (R1)
873*0fca6ea1SDimitry Andric             //
874*0fca6ea1SDimitry Andric             // R So 1945 o - May 24 2 2 M       (R2)
875*0fca6ea1SDimitry Andric             //
876*0fca6ea1SDimitry Andric             // When C2 becomes active the time would be before the first rule R2,
877*0fca6ea1SDimitry Andric             // giving a 1 hour sys_info. This is not valid and the results need
878*0fca6ea1SDimitry Andric             // merging.
879*0fca6ea1SDimitry Andric 
880*0fca6ea1SDimitry Andric             if (__result->end != __sys_info->__info.begin) {
881*0fca6ea1SDimitry Andric               // When the UTC gap between the rules is due to the change of
882*0fca6ea1SDimitry Andric               // offsets adjust the new time to remove the gap.
883*0fca6ea1SDimitry Andric               sys_seconds __end   = __result->end - __result->offset;
884*0fca6ea1SDimitry Andric               sys_seconds __begin = __sys_info->__info.begin - __sys_info->__info.offset;
885*0fca6ea1SDimitry Andric               if (__end == __begin) {
886*0fca6ea1SDimitry Andric                 __sys_info->__info.begin = __result->end;
887*0fca6ea1SDimitry Andric               }
888*0fca6ea1SDimitry Andric             }
889*0fca6ea1SDimitry Andric           }
890*0fca6ea1SDimitry Andric 
891*0fca6ea1SDimitry Andric           __result       = __sys_info->__info;
892*0fca6ea1SDimitry Andric           __valid_result = __time >= __result->begin && __time < __result->end;
893*0fca6ea1SDimitry Andric           __can_merge    = __sys_info->__can_merge;
894*0fca6ea1SDimitry Andric         }
895*0fca6ea1SDimitry Andric       }
896*0fca6ea1SDimitry Andric       __continuation_begin = __result->end;
897*0fca6ea1SDimitry Andric     } else {
898*0fca6ea1SDimitry Andric       __continuation_begin = __sys_info.error();
899*0fca6ea1SDimitry Andric     }
900*0fca6ea1SDimitry Andric   }
901*0fca6ea1SDimitry Andric   if (__valid_result)
902*0fca6ea1SDimitry Andric     return *__result;
903*0fca6ea1SDimitry Andric 
904*0fca6ea1SDimitry Andric   std::__throw_runtime_error("tzdb: corrupt db");
905*0fca6ea1SDimitry Andric }
906*0fca6ea1SDimitry Andric 
907*0fca6ea1SDimitry Andric // Is the "__local_time" present in "__first" and "__second". If so the
908*0fca6ea1SDimitry Andric // local_info has an ambiguous result.
909*0fca6ea1SDimitry Andric [[nodiscard]] static bool
910*0fca6ea1SDimitry Andric __is_ambiguous(local_seconds __local_time, const sys_info& __first, const sys_info& __second) {
911*0fca6ea1SDimitry Andric   std::chrono::local_seconds __end_first{__first.end.time_since_epoch() + __first.offset};
912*0fca6ea1SDimitry Andric   std::chrono::local_seconds __begin_second{__second.begin.time_since_epoch() + __second.offset};
913*0fca6ea1SDimitry Andric 
914*0fca6ea1SDimitry Andric   return __local_time < __end_first && __local_time >= __begin_second;
915*0fca6ea1SDimitry Andric }
916*0fca6ea1SDimitry Andric 
917*0fca6ea1SDimitry Andric // Determines the result of the "__local_time". This expects the object
918*0fca6ea1SDimitry Andric // "__first" to be earlier in time than "__second".
919*0fca6ea1SDimitry Andric [[nodiscard]] static local_info
920*0fca6ea1SDimitry Andric __get_info(local_seconds __local_time, const sys_info& __first, const sys_info& __second) {
921*0fca6ea1SDimitry Andric   std::chrono::local_seconds __end_first{__first.end.time_since_epoch() + __first.offset};
922*0fca6ea1SDimitry Andric   std::chrono::local_seconds __begin_second{__second.begin.time_since_epoch() + __second.offset};
923*0fca6ea1SDimitry Andric 
924*0fca6ea1SDimitry Andric   if (__local_time < __end_first) {
925*0fca6ea1SDimitry Andric     if (__local_time >= __begin_second)
926*0fca6ea1SDimitry Andric       // |--------|
927*0fca6ea1SDimitry Andric       //        |------|
928*0fca6ea1SDimitry Andric       //         ^
929*0fca6ea1SDimitry Andric       return {local_info::ambiguous, __first, __second};
930*0fca6ea1SDimitry Andric 
931*0fca6ea1SDimitry Andric     // |--------|
932*0fca6ea1SDimitry Andric     //          |------|
933*0fca6ea1SDimitry Andric     //         ^
934*0fca6ea1SDimitry Andric     return {local_info::unique, __first, sys_info{}};
935*0fca6ea1SDimitry Andric   }
936*0fca6ea1SDimitry Andric 
937*0fca6ea1SDimitry Andric   if (__local_time < __begin_second)
938*0fca6ea1SDimitry Andric     // |--------|
939*0fca6ea1SDimitry Andric     //             |------|
940*0fca6ea1SDimitry Andric     //           ^
941*0fca6ea1SDimitry Andric     return {local_info::nonexistent, __first, __second};
942*0fca6ea1SDimitry Andric 
943*0fca6ea1SDimitry Andric   // |--------|
944*0fca6ea1SDimitry Andric   //          |------|
945*0fca6ea1SDimitry Andric   //           ^
946*0fca6ea1SDimitry Andric   return {local_info::unique, __second, sys_info{}};
947*0fca6ea1SDimitry Andric }
948*0fca6ea1SDimitry Andric 
949*0fca6ea1SDimitry Andric [[nodiscard]] _LIBCPP_AVAILABILITY_TZDB _LIBCPP_EXPORTED_FROM_ABI local_info
950*0fca6ea1SDimitry Andric time_zone::__get_info(local_seconds __local_time) const {
951*0fca6ea1SDimitry Andric   seconds __local_seconds = __local_time.time_since_epoch();
952*0fca6ea1SDimitry Andric 
953*0fca6ea1SDimitry Andric   /* An example of a typical year with a DST switch displayed in local time.
954*0fca6ea1SDimitry Andric    *
955*0fca6ea1SDimitry Andric    * At the first of April the time goes forward one hour. This means the
956*0fca6ea1SDimitry Andric    * time marked with ~~ is not a valid local time. This is represented by the
957*0fca6ea1SDimitry Andric    * nonexistent value in local_info.result.
958*0fca6ea1SDimitry Andric    *
959*0fca6ea1SDimitry Andric    * At the first of November the time goes backward one hour. This means the
960*0fca6ea1SDimitry Andric    * time marked with ^^ happens twice. This is represented by the ambiguous
961*0fca6ea1SDimitry Andric    * value in local_info.result.
962*0fca6ea1SDimitry Andric    *
963*0fca6ea1SDimitry Andric    * 2020.11.01                  2021.04.01              2021.11.01
964*0fca6ea1SDimitry Andric    * offset +05                  offset +05              offset +05
965*0fca6ea1SDimitry Andric    * save    0s                  save    1h              save    0s
966*0fca6ea1SDimitry Andric    * |------------//----------|
967*0fca6ea1SDimitry Andric    *                             |---------//--------------|
968*0fca6ea1SDimitry Andric    *                                                    |-------------
969*0fca6ea1SDimitry Andric    *                           ~~                        ^^
970*0fca6ea1SDimitry Andric    *
971*0fca6ea1SDimitry Andric    * These shifts can happen due to changes in the current time zone for a
972*0fca6ea1SDimitry Andric    * location. For example, Indian/Kerguelen switched only once. In 1950 from an
973*0fca6ea1SDimitry Andric    * offset of 0 hours to an offset of +05 hours.
974*0fca6ea1SDimitry Andric    *
975*0fca6ea1SDimitry Andric    * During all these shifts the UTC time will not have gaps.
976*0fca6ea1SDimitry Andric    */
977*0fca6ea1SDimitry Andric 
978*0fca6ea1SDimitry Andric   // The code needs to determine the system time for the local time. There is no
979*0fca6ea1SDimitry Andric   // information available. Assume the offset between system time and local time
980*0fca6ea1SDimitry Andric   // is 0s. This gives an initial estimate.
981*0fca6ea1SDimitry Andric   sys_seconds __guess{__local_seconds};
982*0fca6ea1SDimitry Andric   sys_info __info = __get_info(__guess);
983*0fca6ea1SDimitry Andric 
984*0fca6ea1SDimitry Andric   // At this point the offset can be used to determine an estimate for the local
985*0fca6ea1SDimitry Andric   // time. Before doing that, determine the offset and validate whether the
986*0fca6ea1SDimitry Andric   // local time is the range [chrono::local_seconds::min(),
987*0fca6ea1SDimitry Andric   // chrono::local_seconds::max()).
988*0fca6ea1SDimitry Andric   if (__local_seconds < 0s && __info.offset > 0s)
989*0fca6ea1SDimitry Andric     if (__local_seconds - chrono::local_seconds::min().time_since_epoch() < __info.offset)
990*0fca6ea1SDimitry Andric       return {-1, __info, {}};
991*0fca6ea1SDimitry Andric 
992*0fca6ea1SDimitry Andric   if (__local_seconds > 0s && __info.offset < 0s)
993*0fca6ea1SDimitry Andric     if (chrono::local_seconds::max().time_since_epoch() - __local_seconds < -__info.offset)
994*0fca6ea1SDimitry Andric       return {-2, __info, {}};
995*0fca6ea1SDimitry Andric 
996*0fca6ea1SDimitry Andric   // Based on the information found in the sys_info, the local time can be
997*0fca6ea1SDimitry Andric   // converted to a system time. This resulting time can be in the following
998*0fca6ea1SDimitry Andric   // locations of the sys_info:
999*0fca6ea1SDimitry Andric   //
1000*0fca6ea1SDimitry Andric   //                             |---------//--------------|
1001*0fca6ea1SDimitry Andric   //                           1   2.1      2.2         2.3  3
1002*0fca6ea1SDimitry Andric   //
1003*0fca6ea1SDimitry Andric   // 1. The estimate is before the returned sys_info object.
1004*0fca6ea1SDimitry Andric   //    The result is either non-existent or unique in the previous sys_info.
1005*0fca6ea1SDimitry Andric   // 2. The estimate is in the sys_info object
1006*0fca6ea1SDimitry Andric   //    - If the sys_info begin is not sys_seconds::min(), then it might be at
1007*0fca6ea1SDimitry Andric   //      2.1 and could be ambiguous with the previous or unique.
1008*0fca6ea1SDimitry Andric   //    - If sys_info end is not sys_seconds::max(), then it might be at 2.3
1009*0fca6ea1SDimitry Andric   //      and could be ambiguous with the next or unique.
1010*0fca6ea1SDimitry Andric   //    - Else it is at 2.2 and always unique. This case happens when a
1011*0fca6ea1SDimitry Andric   //      time zone has no transitions. For example, UTC or GMT+1.
1012*0fca6ea1SDimitry Andric   // 3. The estimate is after the returned sys_info object.
1013*0fca6ea1SDimitry Andric   //    The result is either non-existent or unique in the next sys_info.
1014*0fca6ea1SDimitry Andric   //
1015*0fca6ea1SDimitry Andric   // There is no specification where the "middle" starts. Similar issues can
1016*0fca6ea1SDimitry Andric   // happen when sys_info objects are "short", then "unique in the next" could
1017*0fca6ea1SDimitry Andric   // become "ambiguous in the next and the one following". Theoretically there
1018*0fca6ea1SDimitry Andric   // is the option of the following time-line
1019*0fca6ea1SDimitry Andric   //
1020*0fca6ea1SDimitry Andric   // |------------|
1021*0fca6ea1SDimitry Andric   //           |----|
1022*0fca6ea1SDimitry Andric   //       |-----------------|
1023*0fca6ea1SDimitry Andric   //
1024*0fca6ea1SDimitry Andric   // However the local_info object only has 2 sys_info objects, so this option
1025*0fca6ea1SDimitry Andric   // is not tested.
1026*0fca6ea1SDimitry Andric 
1027*0fca6ea1SDimitry Andric   sys_seconds __sys_time{__local_seconds - __info.offset};
1028*0fca6ea1SDimitry Andric   if (__sys_time < __info.begin)
1029*0fca6ea1SDimitry Andric     // Case 1 before __info
1030*0fca6ea1SDimitry Andric     return chrono::__get_info(__local_time, __get_info(__info.begin - 1s), __info);
1031*0fca6ea1SDimitry Andric 
1032*0fca6ea1SDimitry Andric   if (__sys_time >= __info.end)
1033*0fca6ea1SDimitry Andric     // Case 3 after __info
1034*0fca6ea1SDimitry Andric     return chrono::__get_info(__local_time, __info, __get_info(__info.end));
1035*0fca6ea1SDimitry Andric 
1036*0fca6ea1SDimitry Andric   // Case 2 in __info
1037*0fca6ea1SDimitry Andric   if (__info.begin != sys_seconds::min()) {
1038*0fca6ea1SDimitry Andric     // Case 2.1 Not at the beginning, when not ambiguous the result should test
1039*0fca6ea1SDimitry Andric     // case 2.3.
1040*0fca6ea1SDimitry Andric     sys_info __prev = __get_info(__info.begin - 1s);
1041*0fca6ea1SDimitry Andric     if (__is_ambiguous(__local_time, __prev, __info))
1042*0fca6ea1SDimitry Andric       return {local_info::ambiguous, __prev, __info};
1043*0fca6ea1SDimitry Andric   }
1044*0fca6ea1SDimitry Andric 
1045*0fca6ea1SDimitry Andric   if (__info.end == sys_seconds::max())
1046*0fca6ea1SDimitry Andric     // At the end so it's case 2.2
1047*0fca6ea1SDimitry Andric     return {local_info::unique, __info, sys_info{}};
1048*0fca6ea1SDimitry Andric 
1049*0fca6ea1SDimitry Andric   // This tests case 2.2 or case 2.3.
1050*0fca6ea1SDimitry Andric   return chrono::__get_info(__local_time, __info, __get_info(__info.end));
1051*0fca6ea1SDimitry Andric }
1052*0fca6ea1SDimitry Andric 
1053*0fca6ea1SDimitry Andric } // namespace chrono
1054*0fca6ea1SDimitry Andric 
1055*0fca6ea1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
1056