Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:27:29

0001 // Copyright 2016 Google Inc. All Rights Reserved.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //   https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 //   Unless required by applicable law or agreed to in writing, software
0010 //   distributed under the License is distributed on an "AS IS" BASIS,
0011 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 //   See the License for the specific language governing permissions and
0013 //   limitations under the License.
0014 
0015 #ifndef ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_INFO_H_
0016 #define ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_INFO_H_
0017 
0018 #include <atomic>
0019 #include <cstddef>
0020 #include <cstdint>
0021 #include <memory>
0022 #include <string>
0023 #include <vector>
0024 
0025 #include "absl/base/config.h"
0026 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
0027 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
0028 #include "absl/time/internal/cctz/include/cctz/zone_info_source.h"
0029 #include "time_zone_if.h"
0030 #include "tzfile.h"
0031 
0032 namespace absl {
0033 ABSL_NAMESPACE_BEGIN
0034 namespace time_internal {
0035 namespace cctz {
0036 
0037 // A transition to a new UTC offset.
0038 struct Transition {
0039   std::int_least64_t unix_time;   // the instant of this transition
0040   std::uint_least8_t type_index;  // index of the transition type
0041   civil_second civil_sec;         // local civil time of transition
0042   civil_second prev_civil_sec;    // local civil time one second earlier
0043 
0044   struct ByUnixTime {
0045     inline bool operator()(const Transition& lhs, const Transition& rhs) const {
0046       return lhs.unix_time < rhs.unix_time;
0047     }
0048   };
0049   struct ByCivilTime {
0050     inline bool operator()(const Transition& lhs, const Transition& rhs) const {
0051       return lhs.civil_sec < rhs.civil_sec;
0052     }
0053   };
0054 };
0055 
0056 // The characteristics of a particular transition.
0057 struct TransitionType {
0058   std::int_least32_t utc_offset;  // the new prevailing UTC offset
0059   civil_second civil_max;         // max convertible civil time for offset
0060   civil_second civil_min;         // min convertible civil time for offset
0061   bool is_dst;                    // did we move into daylight-saving time
0062   std::uint_least8_t abbr_index;  // index of the new abbreviation
0063 };
0064 
0065 // A time zone backed by the IANA Time Zone Database (zoneinfo).
0066 class TimeZoneInfo : public TimeZoneIf {
0067  public:
0068   // Factories.
0069   static std::unique_ptr<TimeZoneInfo> UTC();  // never fails
0070   static std::unique_ptr<TimeZoneInfo> Make(const std::string& name);
0071 
0072   // TimeZoneIf implementations.
0073   time_zone::absolute_lookup BreakTime(
0074       const time_point<seconds>& tp) const override;
0075   time_zone::civil_lookup MakeTime(const civil_second& cs) const override;
0076   bool NextTransition(const time_point<seconds>& tp,
0077                       time_zone::civil_transition* trans) const override;
0078   bool PrevTransition(const time_point<seconds>& tp,
0079                       time_zone::civil_transition* trans) const override;
0080   std::string Version() const override;
0081   std::string Description() const override;
0082 
0083  private:
0084   TimeZoneInfo() = default;
0085   TimeZoneInfo(const TimeZoneInfo&) = delete;
0086   TimeZoneInfo& operator=(const TimeZoneInfo&) = delete;
0087 
0088   bool GetTransitionType(std::int_fast32_t utc_offset, bool is_dst,
0089                          const std::string& abbr, std::uint_least8_t* index);
0090   bool EquivTransitions(std::uint_fast8_t tt1_index,
0091                         std::uint_fast8_t tt2_index) const;
0092   bool ExtendTransitions();
0093 
0094   bool ResetToBuiltinUTC(const seconds& offset);
0095   bool Load(const std::string& name);
0096   bool Load(ZoneInfoSource* zip);
0097 
0098   // Helpers for BreakTime() and MakeTime().
0099   time_zone::absolute_lookup LocalTime(std::int_fast64_t unix_time,
0100                                        const TransitionType& tt) const;
0101   time_zone::absolute_lookup LocalTime(std::int_fast64_t unix_time,
0102                                        const Transition& tr) const;
0103   time_zone::civil_lookup TimeLocal(const civil_second& cs,
0104                                     year_t c4_shift) const;
0105 
0106   std::vector<Transition> transitions_;  // ordered by unix_time and civil_sec
0107   std::vector<TransitionType> transition_types_;  // distinct transition types
0108   std::uint_fast8_t default_transition_type_;     // for before first transition
0109   std::string abbreviations_;  // all the NUL-terminated abbreviations
0110 
0111   std::string version_;      // the tzdata version if available
0112   std::string future_spec_;  // for after the last zic transition
0113   bool extended_;            // future_spec_ was used to generate transitions
0114   year_t last_year_;         // the final year of the generated transitions
0115 
0116   // We remember the transitions found during the last BreakTime() and
0117   // MakeTime() calls. If the next request is for the same transition we
0118   // will avoid re-searching.
0119   mutable std::atomic<std::size_t> local_time_hint_ = {};  // BreakTime() hint
0120   mutable std::atomic<std::size_t> time_local_hint_ = {};  // MakeTime() hint
0121 };
0122 
0123 }  // namespace cctz
0124 }  // namespace time_internal
0125 ABSL_NAMESPACE_END
0126 }  // namespace absl
0127 
0128 #endif  // ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_INFO_H_