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// http://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 CCTZ_TIME_ZONE_IMPL_H_
16#define CCTZ_TIME_ZONE_IMPL_H_
17
18#include <memory>
19#include <string>
20
21#include "cctz/civil_time.h"
22#include "cctz/time_zone.h"
23#include "time_zone_if.h"
24#include "time_zone_info.h"
25
26namespace cctz {
27
28// time_zone::Impl is the internal object referenced by a cctz::time_zone.
29class time_zone::Impl {
30 public:
31 // The UTC time zone. Also used for other time zones that fail to load.
32 static time_zone UTC();
33
34 // Load a named time zone. Returns false if the name is invalid, or if
35 // some other kind of error occurs. Note that loading "UTC" never fails.
36 static bool LoadTimeZone(const std::string& name, time_zone* tz);
37
38 // Dereferences the time_zone to obtain its Impl.
39 static const time_zone::Impl& get(const time_zone& tz);
40
41 // Clears the map of cached time zones. Primarily for use in benchmarks
42 // that gauge the performance of loading/parsing the time-zone data.
43 static void ClearTimeZoneMapTestOnly();
44
45 // The primary key is the time-zone ID (e.g., "America/New_York").
46 const std::string& name() const { return name_; }
47
48 // Breaks a time_point down to civil-time components in this time zone.
49 time_zone::absolute_lookup BreakTime(
50 const time_point<sys_seconds>& tp) const {
51 return zone_->BreakTime(tp);
52 }
53
54 // Converts the civil-time components in this time zone into a time_point.
55 // That is, the opposite of BreakTime(). The requested civil time may be
56 // ambiguous or illegal due to a change of UTC offset.
57 time_zone::civil_lookup MakeTime(const civil_second& cs) const {
58 return zone_->MakeTime(cs);
59 }
60
61 // Returns an implementation-specific description of this time zone.
62 std::string Description() const { return zone_->Description(); }
63
64 // Finds the time of the next/previous offset change in this time zone.
65 //
66 // By definition, NextTransition(&tp) returns false when tp has its
67 // maximum value, and PrevTransition(&tp) returns false when tp has its
68 // mimimum value. If the zone has no transitions, the result will also
69 // be false no matter what the argument.
70 //
71 // Otherwise, when tp has its mimimum value, NextTransition(&tp) returns
72 // true and sets tp to the first recorded transition. Chains of calls
73 // to NextTransition()/PrevTransition() will eventually return false,
74 // but it is unspecified exactly when NextTransition(&tp) jumps to false,
75 // or what time is set by PrevTransition(&tp) for a very distant tp.
76 bool NextTransition(time_point<sys_seconds>* tp) const {
77 return zone_->NextTransition(tp);
78 }
79 bool PrevTransition(time_point<sys_seconds>* tp) const {
80 return zone_->PrevTransition(tp);
81 }
82
83 private:
84 explicit Impl(const std::string& name);
85 static const Impl* UTCImpl();
86
87 const std::string name_;
88 std::unique_ptr<TimeZoneIf> zone_;
89};
90
91} // namespace cctz
92
93#endif // CCTZ_TIME_ZONE_IMPL_H_
94