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_IF_H_ 16 #define ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_IF_H_ 17 18 #include <chrono> 19 #include <cstdint> 20 #include <memory> 21 #include <string> 22 23 #include "absl/base/config.h" 24 #include "absl/time/internal/cctz/include/cctz/civil_time.h" 25 #include "absl/time/internal/cctz/include/cctz/time_zone.h" 26 27 namespace absl { 28 ABSL_NAMESPACE_BEGIN 29 namespace time_internal { 30 namespace cctz { 31 32 // A simple interface used to hide time-zone complexities from time_zone::Impl. 33 // Subclasses implement the functions for civil-time conversions in the zone. 34 class TimeZoneIf { 35 public: 36 // Factory functions for TimeZoneIf implementations. 37 static std::unique_ptr<TimeZoneIf> UTC(); // never fails 38 static std::unique_ptr<TimeZoneIf> Make(const std::string& name); 39 40 virtual ~TimeZoneIf(); 41 42 virtual time_zone::absolute_lookup BreakTime( 43 const time_point<seconds>& tp) const = 0; 44 virtual time_zone::civil_lookup MakeTime(const civil_second& cs) const = 0; 45 46 virtual bool NextTransition(const time_point<seconds>& tp, 47 time_zone::civil_transition* trans) const = 0; 48 virtual bool PrevTransition(const time_point<seconds>& tp, 49 time_zone::civil_transition* trans) const = 0; 50 51 virtual std::string Version() const = 0; 52 virtual std::string Description() const = 0; 53 54 protected: 55 TimeZoneIf() = default; 56 TimeZoneIf(const TimeZoneIf&) = delete; 57 TimeZoneIf& operator=(const TimeZoneIf&) = delete; 58 }; 59 60 // Convert between time_point<seconds> and a count of seconds since the 61 // Unix epoch. We assume that the std::chrono::system_clock and the 62 // Unix clock are second aligned, and that the results are representable. 63 // (That is, that they share an epoch, which is required since C++20.) ToUnixSeconds(const time_point<seconds> & tp)64inline std::int_fast64_t ToUnixSeconds(const time_point<seconds>& tp) { 65 return (tp - std::chrono::time_point_cast<seconds>( 66 std::chrono::system_clock::from_time_t(0))) 67 .count(); 68 } FromUnixSeconds(std::int_fast64_t t)69inline time_point<seconds> FromUnixSeconds(std::int_fast64_t t) { 70 return std::chrono::time_point_cast<seconds>( 71 std::chrono::system_clock::from_time_t(0)) + 72 seconds(t); 73 } 74 75 } // namespace cctz 76 } // namespace time_internal 77 ABSL_NAMESPACE_END 78 } // namespace absl 79 80 #endif // ABSL_TIME_INTERNAL_CCTZ_TIME_ZONE_IF_H_ 81