| 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_IF_H_ | 
|---|
| 16 | #define CCTZ_TIME_ZONE_IF_H_ | 
|---|
| 17 |  | 
|---|
| 18 | #include <chrono> | 
|---|
| 19 | #include <cstdint> | 
|---|
| 20 | #include <memory> | 
|---|
| 21 | #include <string> | 
|---|
| 22 |  | 
|---|
| 23 | #include "cctz/civil_time.h" | 
|---|
| 24 | #include "cctz/time_zone.h" | 
|---|
| 25 |  | 
|---|
| 26 | namespace cctz { | 
|---|
| 27 |  | 
|---|
| 28 | // A simple interface used to hide time-zone complexities from time_zone::Impl. | 
|---|
| 29 | // Subclasses implement the functions for civil-time conversions in the zone. | 
|---|
| 30 | class TimeZoneIf { | 
|---|
| 31 | public: | 
|---|
| 32 | // A factory function for TimeZoneIf implementations. | 
|---|
| 33 | static std::unique_ptr<TimeZoneIf> Load(const std::string& name); | 
|---|
| 34 |  | 
|---|
| 35 | virtual ~TimeZoneIf(); | 
|---|
| 36 |  | 
|---|
| 37 | virtual time_zone::absolute_lookup BreakTime( | 
|---|
| 38 | const time_point<sys_seconds>& tp) const = 0; | 
|---|
| 39 | virtual time_zone::civil_lookup MakeTime( | 
|---|
| 40 | const civil_second& cs) const = 0; | 
|---|
| 41 |  | 
|---|
| 42 | virtual std::string Description() const = 0; | 
|---|
| 43 | virtual bool NextTransition(time_point<sys_seconds>* tp) const = 0; | 
|---|
| 44 | virtual bool PrevTransition(time_point<sys_seconds>* tp) const = 0; | 
|---|
| 45 |  | 
|---|
| 46 | protected: | 
|---|
| 47 | TimeZoneIf() {} | 
|---|
| 48 | }; | 
|---|
| 49 |  | 
|---|
| 50 | // Convert between time_point<sys_seconds> and a count of seconds since | 
|---|
| 51 | // the Unix epoch.  We assume that the std::chrono::system_clock and the | 
|---|
| 52 | // Unix clock are second aligned, but not that they share an epoch. | 
|---|
| 53 | inline std::int_fast64_t ToUnixSeconds(const time_point<sys_seconds>& tp) { | 
|---|
| 54 | return (tp - std::chrono::time_point_cast<sys_seconds>( | 
|---|
| 55 | std::chrono::system_clock::from_time_t(0))) | 
|---|
| 56 | .count(); | 
|---|
| 57 | } | 
|---|
| 58 | inline time_point<sys_seconds> FromUnixSeconds(std::int_fast64_t t) { | 
|---|
| 59 | return std::chrono::time_point_cast<sys_seconds>( | 
|---|
| 60 | std::chrono::system_clock::from_time_t(0)) + | 
|---|
| 61 | sys_seconds(t); | 
|---|
| 62 | } | 
|---|
| 63 |  | 
|---|
| 64 | }  // namespace cctz | 
|---|
| 65 |  | 
|---|
| 66 | #endif  // CCTZ_TIME_ZONE_IF_H_ | 
|---|
| 67 |  | 
|---|