1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //   https://www.apache.org/licenses/LICENSE-2.0
8 //
9 //   Unless required by applicable law or agreed to in writing, software
10 //   distributed under the License is distributed on an "AS IS" BASIS,
11 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //   See the License for the specific language governing permissions and
13 //   limitations under the License.
14 
15 #ifndef ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_IMPL_H_
16 #define ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_IMPL_H_
17 
18 #include <memory>
19 #include <string>
20 
21 #include "absl/base/config.h"
22 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
23 #include "absl/time/internal/cctz/include/cctz/time_zone.h"
24 #include "time_zone_if.h"
25 #include "time_zone_info.h"
26 
27 namespace absl {
28 ABSL_NAMESPACE_BEGIN
29 namespace time_internal {
30 namespace cctz {
31 
32 // time_zone::Impl is the internal object referenced by a cctz::time_zone.
33 class time_zone::Impl {
34  public:
35   // The UTC time zone. Also used for other time zones that fail to load.
36   static time_zone UTC();
37 
38   // Load a named time zone. Returns false if the name is invalid, or if
39   // some other kind of error occurs. Note that loading "UTC" never fails.
40   static bool LoadTimeZone(const std::string& name, time_zone* tz);
41 
42   // Clears the map of cached time zones.  Primarily for use in benchmarks
43   // that gauge the performance of loading/parsing the time-zone data.
44   static void ClearTimeZoneMapTestOnly();
45 
46   // The primary key is the time-zone ID (e.g., "America/New_York").
Name()47   const std::string& Name() const {
48     // TODO: It would nice if the zoneinfo data included the zone name.
49     return name_;
50   }
51 
52   // Breaks a time_point down to civil-time components in this time zone.
BreakTime(const time_point<seconds> & tp)53   time_zone::absolute_lookup BreakTime(const time_point<seconds>& tp) const {
54     return zone_->BreakTime(tp);
55   }
56 
57   // Converts the civil-time components in this time zone into a time_point.
58   // That is, the opposite of BreakTime(). The requested civil time may be
59   // ambiguous or illegal due to a change of UTC offset.
MakeTime(const civil_second & cs)60   time_zone::civil_lookup MakeTime(const civil_second& cs) const {
61     return zone_->MakeTime(cs);
62   }
63 
64   // Finds the time of the next/previous offset change in this time zone.
NextTransition(const time_point<seconds> & tp,time_zone::civil_transition * trans)65   bool NextTransition(const time_point<seconds>& tp,
66                       time_zone::civil_transition* trans) const {
67     return zone_->NextTransition(tp, trans);
68   }
PrevTransition(const time_point<seconds> & tp,time_zone::civil_transition * trans)69   bool PrevTransition(const time_point<seconds>& tp,
70                       time_zone::civil_transition* trans) const {
71     return zone_->PrevTransition(tp, trans);
72   }
73 
74   // Returns an implementation-defined version string for this time zone.
Version()75   std::string Version() const { return zone_->Version(); }
76 
77   // Returns an implementation-defined description of this time zone.
Description()78   std::string Description() const { return zone_->Description(); }
79 
80  private:
81   explicit Impl(const std::string& name);
82   static const Impl* UTCImpl();
83 
84   const std::string name_;
85   std::unique_ptr<TimeZoneIf> zone_;
86 };
87 
88 }  // namespace cctz
89 }  // namespace time_internal
90 ABSL_NAMESPACE_END
91 }  // namespace absl
92 
93 #endif  // ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_IMPL_H_
94