Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-08-28 08:27:14

0001 #ifndef TZ_PRIVATE_H
0002 #define TZ_PRIVATE_H
0003 
0004 // The MIT License (MIT)
0005 //
0006 // Copyright (c) 2015, 2016 Howard Hinnant
0007 //
0008 // Permission is hereby granted, free of charge, to any person obtaining a copy
0009 // of this software and associated documentation files (the "Software"), to deal
0010 // in the Software without restriction, including without limitation the rights
0011 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0012 // copies of the Software, and to permit persons to whom the Software is
0013 // furnished to do so, subject to the following conditions:
0014 //
0015 // The above copyright notice and this permission notice shall be included in all
0016 // copies or substantial portions of the Software.
0017 //
0018 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0019 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0020 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0021 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0022 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0023 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
0024 // SOFTWARE.
0025 //
0026 // Our apologies.  When the previous paragraph was written, lowercase had not yet
0027 // been invented (that would involve another several millennia of evolution).
0028 // We did not mean to shout.
0029 
0030 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
0031 #include "tz.h"
0032 #else
0033 #include "date.h"
0034 #include <vector>
0035 #endif
0036 
0037 namespace arrow_vendored::date
0038 {
0039 
0040 namespace detail
0041 {
0042 
0043 #if !USE_OS_TZDB
0044 
0045 enum class tz {utc, local, standard};
0046 
0047 //forward declare to avoid warnings in gcc 6.2
0048 class MonthDayTime;
0049 std::istream& operator>>(std::istream& is, MonthDayTime& x);
0050 std::ostream& operator<<(std::ostream& os, const MonthDayTime& x);
0051 
0052 
0053 class MonthDayTime
0054 {
0055 private:
0056     struct pair
0057     {
0058 #if defined(_MSC_VER) && (_MSC_VER < 1900)
0059         pair() : month_day_(date::jan / 1), weekday_(0U) {}
0060 
0061         pair(const date::month_day& month_day, const date::weekday& weekday)
0062             : month_day_(month_day), weekday_(weekday) {}
0063 #endif
0064 
0065         date::month_day month_day_;
0066         date::weekday   weekday_;
0067     };
0068 
0069     enum Type {month_day, month_last_dow, lteq, gteq};
0070 
0071     Type                         type_{month_day};
0072 
0073 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
0074     union U
0075 #else
0076     struct U
0077 #endif
0078     {
0079         date::month_day          month_day_;
0080         date::month_weekday_last month_weekday_last_;
0081         pair                     month_day_weekday_;
0082 
0083 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
0084         U() : month_day_{date::jan/1} {}
0085 #else
0086         U() :
0087             month_day_(date::jan/1),
0088             month_weekday_last_(date::month(0U), date::weekday_last(date::weekday(0U)))
0089         {}
0090 
0091 #endif // !defined(_MSC_VER) || (_MSC_VER >= 1900)
0092 
0093         U& operator=(const date::month_day& x);
0094         U& operator=(const date::month_weekday_last& x);
0095         U& operator=(const pair& x);
0096     } u;
0097 
0098     std::chrono::hours           h_{0};
0099     std::chrono::minutes         m_{0};
0100     std::chrono::seconds         s_{0};
0101     tz                           zone_{tz::local};
0102 
0103 public:
0104     MonthDayTime() = default;
0105     MonthDayTime(local_seconds tp, tz timezone);
0106     MonthDayTime(const date::month_day& md, tz timezone);
0107 
0108     date::day day() const;
0109     date::month month() const;
0110     tz zone() const {return zone_;}
0111 
0112     void canonicalize(date::year y);
0113 
0114     sys_seconds
0115        to_sys(date::year y, std::chrono::seconds offset, std::chrono::seconds save) const;
0116     sys_days to_sys_days(date::year y) const;
0117 
0118     sys_seconds to_time_point(date::year y) const;
0119     int compare(date::year y, const MonthDayTime& x, date::year yx,
0120                 std::chrono::seconds offset, std::chrono::minutes prev_save) const;
0121 
0122     friend std::istream& operator>>(std::istream& is, MonthDayTime& x);
0123     friend std::ostream& operator<<(std::ostream& os, const MonthDayTime& x);
0124 };
0125 
0126 // A Rule specifies one or more set of datetimes without using an offset.
0127 // Multiple dates are specified with multiple years.  The years in effect
0128 // go from starting_year_ to ending_year_, inclusive.  starting_year_ <=
0129 // ending_year_. save_ is in effect for times from the specified time
0130 // onward, including the specified time. When the specified time is
0131 // local, it uses the save_ from the chronologically previous Rule, or if
0132 // there is none, 0.
0133 
0134 //forward declare to avoid warnings in gcc 6.2
0135 class Rule;
0136 bool operator==(const Rule& x, const Rule& y);
0137 bool operator<(const Rule& x, const Rule& y);
0138 bool operator==(const Rule& x, const date::year& y);
0139 bool operator<(const Rule& x, const date::year& y);
0140 bool operator==(const date::year& x, const Rule& y);
0141 bool operator<(const date::year& x, const Rule& y);
0142 bool operator==(const Rule& x, const std::string& y);
0143 bool operator<(const Rule& x, const std::string& y);
0144 bool operator==(const std::string& x, const Rule& y);
0145 bool operator<(const std::string& x, const Rule& y);
0146 std::ostream& operator<<(std::ostream& os, const Rule& r);
0147 
0148 class Rule
0149 {
0150 private:
0151     std::string          name_;
0152     date::year           starting_year_{0};
0153     date::year           ending_year_{0};
0154     MonthDayTime         starting_at_;
0155     std::chrono::minutes save_{0};
0156     std::string          abbrev_;
0157 
0158 public:
0159     Rule() = default;
0160     explicit Rule(const std::string& s);
0161     Rule(const Rule& r, date::year starting_year, date::year ending_year);
0162 
0163     const std::string& name() const {return name_;}
0164     const std::string& abbrev() const {return abbrev_;}
0165 
0166     const MonthDayTime&         mdt()           const {return starting_at_;}
0167     const date::year&           starting_year() const {return starting_year_;}
0168     const date::year&           ending_year()   const {return ending_year_;}
0169     const std::chrono::minutes& save()          const {return save_;}
0170 
0171     static void split_overlaps(std::vector<Rule>& rules);
0172 
0173     friend bool operator==(const Rule& x, const Rule& y);
0174     friend bool operator<(const Rule& x, const Rule& y);
0175     friend bool operator==(const Rule& x, const date::year& y);
0176     friend bool operator<(const Rule& x, const date::year& y);
0177     friend bool operator==(const date::year& x, const Rule& y);
0178     friend bool operator<(const date::year& x, const Rule& y);
0179     friend bool operator==(const Rule& x, const std::string& y);
0180     friend bool operator<(const Rule& x, const std::string& y);
0181     friend bool operator==(const std::string& x, const Rule& y);
0182     friend bool operator<(const std::string& x, const Rule& y);
0183 
0184     friend std::ostream& operator<<(std::ostream& os, const Rule& r);
0185 
0186 private:
0187     date::day day() const;
0188     date::month month() const;
0189     static void split_overlaps(std::vector<Rule>& rules, std::size_t i, std::size_t& e);
0190     static bool overlaps(const Rule& x, const Rule& y);
0191     static void split(std::vector<Rule>& rules, std::size_t i, std::size_t k,
0192                       std::size_t& e);
0193 };
0194 
0195 inline bool operator!=(const Rule& x, const Rule& y) {return !(x == y);}
0196 inline bool operator> (const Rule& x, const Rule& y) {return   y < x;}
0197 inline bool operator<=(const Rule& x, const Rule& y) {return !(y < x);}
0198 inline bool operator>=(const Rule& x, const Rule& y) {return !(x < y);}
0199 
0200 inline bool operator!=(const Rule& x, const date::year& y) {return !(x == y);}
0201 inline bool operator> (const Rule& x, const date::year& y) {return   y < x;}
0202 inline bool operator<=(const Rule& x, const date::year& y) {return !(y < x);}
0203 inline bool operator>=(const Rule& x, const date::year& y) {return !(x < y);}
0204 
0205 inline bool operator!=(const date::year& x, const Rule& y) {return !(x == y);}
0206 inline bool operator> (const date::year& x, const Rule& y) {return   y < x;}
0207 inline bool operator<=(const date::year& x, const Rule& y) {return !(y < x);}
0208 inline bool operator>=(const date::year& x, const Rule& y) {return !(x < y);}
0209 
0210 inline bool operator!=(const Rule& x, const std::string& y) {return !(x == y);}
0211 inline bool operator> (const Rule& x, const std::string& y) {return   y < x;}
0212 inline bool operator<=(const Rule& x, const std::string& y) {return !(y < x);}
0213 inline bool operator>=(const Rule& x, const std::string& y) {return !(x < y);}
0214 
0215 inline bool operator!=(const std::string& x, const Rule& y) {return !(x == y);}
0216 inline bool operator> (const std::string& x, const Rule& y) {return   y < x;}
0217 inline bool operator<=(const std::string& x, const Rule& y) {return !(y < x);}
0218 inline bool operator>=(const std::string& x, const Rule& y) {return !(x < y);}
0219 
0220 struct zonelet
0221 {
0222     enum tag {has_rule, has_save, is_empty};
0223 
0224     std::chrono::seconds gmtoff_;
0225     tag tag_ = has_rule;
0226 
0227 #if !defined(_MSC_VER) || (_MSC_VER >= 1900)
0228     union U
0229 #else
0230     struct U
0231 #endif
0232     {
0233         std::string          rule_;
0234         std::chrono::minutes save_;
0235 
0236         ~U() {}
0237         U() {}
0238         U(const U&) {}
0239         U& operator=(const U&) = delete;
0240     } u;
0241 
0242     std::string                        format_;
0243     date::year                         until_year_{0};
0244     MonthDayTime                       until_date_;
0245     sys_seconds                        until_utc_;
0246     local_seconds                      until_std_;
0247     local_seconds                      until_loc_;
0248     std::chrono::minutes               initial_save_{0};
0249     std::string                        initial_abbrev_;
0250     std::pair<const Rule*, date::year> first_rule_{nullptr, date::year::min()};
0251     std::pair<const Rule*, date::year> last_rule_{nullptr, date::year::max()};
0252 
0253     ~zonelet();
0254     zonelet();
0255     zonelet(const zonelet& i);
0256     zonelet& operator=(const zonelet&) = delete;
0257 };
0258 
0259 #else  // USE_OS_TZDB
0260 
0261 struct ttinfo
0262 {
0263     std::int32_t  tt_gmtoff;
0264     unsigned char tt_isdst;
0265     unsigned char tt_abbrind;
0266     unsigned char pad[2];
0267 };
0268 
0269 static_assert(sizeof(ttinfo) == 8, "");
0270 
0271 struct expanded_ttinfo
0272 {
0273     std::chrono::seconds offset;
0274     std::string          abbrev;
0275     bool                 is_dst;
0276 };
0277 
0278 struct transition
0279 {
0280     sys_seconds            timepoint;
0281     const expanded_ttinfo* info;
0282 
0283     transition(sys_seconds tp, const expanded_ttinfo* i = nullptr)
0284         : timepoint(tp)
0285         , info(i)
0286         {}
0287 
0288     friend
0289     std::ostream&
0290     operator<<(std::ostream& os, const transition& t)
0291     {
0292         date::operator<<(os, t.timepoint) << "Z ";
0293         if (t.info->offset >= std::chrono::seconds{0})
0294             os << '+';
0295         os << make_time(t.info->offset);
0296         if (t.info->is_dst > 0)
0297             os << " daylight ";
0298         else
0299             os << " standard ";
0300         os << t.info->abbrev;
0301         return os;
0302     }
0303 };
0304 
0305 #endif  // USE_OS_TZDB
0306 
0307 }  // namespace detail
0308 
0309 }  // namespace arrow_vendored::date
0310 
0311 #if defined(_MSC_VER) && (_MSC_VER < 1900)
0312 #include "tz.h"
0313 #endif
0314 
0315 #endif  // TZ_PRIVATE_H