time_zone_info.h
Go to the documentation of this file.
00001 // Copyright 2016 Google Inc. All Rights Reserved.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //   https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 //   Unless required by applicable law or agreed to in writing, software
00010 //   distributed under the License is distributed on an "AS IS" BASIS,
00011 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 //   See the License for the specific language governing permissions and
00013 //   limitations under the License.
00014 
00015 #ifndef ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_INFO_H_
00016 #define ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_INFO_H_
00017 
00018 #include <atomic>
00019 #include <cstddef>
00020 #include <cstdint>
00021 #include <string>
00022 #include <vector>
00023 
00024 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
00025 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
00026 #include "absl/time/internal/cctz/include/cctz/zone_info_source.h"
00027 #include "time_zone_if.h"
00028 #include "tzfile.h"
00029 
00030 namespace absl {
00031 namespace time_internal {
00032 namespace cctz {
00033 
00034 // A transition to a new UTC offset.
00035 struct Transition {
00036   std::int_least64_t unix_time;   // the instant of this transition
00037   std::uint_least8_t type_index;  // index of the transition type
00038   civil_second civil_sec;         // local civil time of transition
00039   civil_second prev_civil_sec;    // local civil time one second earlier
00040 
00041   struct ByUnixTime {
00042     inline bool operator()(const Transition& lhs, const Transition& rhs) const {
00043       return lhs.unix_time < rhs.unix_time;
00044     }
00045   };
00046   struct ByCivilTime {
00047     inline bool operator()(const Transition& lhs, const Transition& rhs) const {
00048       return lhs.civil_sec < rhs.civil_sec;
00049     }
00050   };
00051 };
00052 
00053 // The characteristics of a particular transition.
00054 struct TransitionType {
00055   std::int_least32_t utc_offset;  // the new prevailing UTC offset
00056   civil_second civil_max;         // max convertible civil time for offset
00057   civil_second civil_min;         // min convertible civil time for offset
00058   bool is_dst;                    // did we move into daylight-saving time
00059   std::uint_least8_t abbr_index;  // index of the new abbreviation
00060 };
00061 
00062 // A time zone backed by the IANA Time Zone Database (zoneinfo).
00063 class TimeZoneInfo : public TimeZoneIf {
00064  public:
00065   TimeZoneInfo() = default;
00066   TimeZoneInfo(const TimeZoneInfo&) = delete;
00067   TimeZoneInfo& operator=(const TimeZoneInfo&) = delete;
00068 
00069   // Loads the zoneinfo for the given name, returning true if successful.
00070   bool Load(const std::string& name);
00071 
00072   // TimeZoneIf implementations.
00073   time_zone::absolute_lookup BreakTime(
00074       const time_point<seconds>& tp) const override;
00075   time_zone::civil_lookup MakeTime(
00076       const civil_second& cs) const override;
00077   bool NextTransition(const time_point<seconds>& tp,
00078                       time_zone::civil_transition* trans) const override;
00079   bool PrevTransition(const time_point<seconds>& tp,
00080                       time_zone::civil_transition* trans) const override;
00081   std::string Version() const override;
00082   std::string Description() const override;
00083 
00084  private:
00085   struct Header {  // counts of:
00086     std::size_t timecnt;     // transition times
00087     std::size_t typecnt;     // transition types
00088     std::size_t charcnt;     // zone abbreviation characters
00089     std::size_t leapcnt;     // leap seconds (we expect none)
00090     std::size_t ttisstdcnt;  // UTC/local indicators (unused)
00091     std::size_t ttisgmtcnt;  // standard/wall indicators (unused)
00092 
00093     bool Build(const tzhead& tzh);
00094     std::size_t DataLength(std::size_t time_len) const;
00095   };
00096 
00097   void CheckTransition(const std::string& name, const TransitionType& tt,
00098                        std::int_fast32_t offset, bool is_dst,
00099                        const std::string& abbr) const;
00100   bool EquivTransitions(std::uint_fast8_t tt1_index,
00101                         std::uint_fast8_t tt2_index) const;
00102   void ExtendTransitions(const std::string& name, const Header& hdr);
00103 
00104   bool ResetToBuiltinUTC(const seconds& offset);
00105   bool Load(const std::string& name, ZoneInfoSource* zip);
00106 
00107   // Helpers for BreakTime() and MakeTime().
00108   time_zone::absolute_lookup LocalTime(std::int_fast64_t unix_time,
00109                                        const TransitionType& tt) const;
00110   time_zone::absolute_lookup LocalTime(std::int_fast64_t unix_time,
00111                                        const Transition& tr) const;
00112   time_zone::civil_lookup TimeLocal(const civil_second& cs,
00113                                     year_t c4_shift) const;
00114 
00115   std::vector<Transition> transitions_;  // ordered by unix_time and civil_sec
00116   std::vector<TransitionType> transition_types_;  // distinct transition types
00117   std::uint_fast8_t default_transition_type_;  // for before first transition
00118   std::string abbreviations_;  // all the NUL-terminated abbreviations
00119 
00120   std::string version_;      // the tzdata version if available
00121   std::string future_spec_;  // for after the last zic transition
00122   bool extended_;            // future_spec_ was used to generate transitions
00123   year_t last_year_;         // the final year of the generated transitions
00124 
00125   // We remember the transitions found during the last BreakTime() and
00126   // MakeTime() calls. If the next request is for the same transition we
00127   // will avoid re-searching.
00128   mutable std::atomic<std::size_t> local_time_hint_ = {};  // BreakTime() hint
00129   mutable std::atomic<std::size_t> time_local_hint_ = {};  // MakeTime() hint
00130 };
00131 
00132 }  // namespace cctz
00133 }  // namespace time_internal
00134 }  // namespace absl
00135 
00136 #endif  // ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_INFO_H_


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15