• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2017 The Abseil Authors.
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 #include "absl/time/internal/test_util.h"
16 
17 #include <algorithm>
18 #include <cstddef>
19 #include <cstring>
20 #include <memory>
21 
22 #include "absl/base/config.h"
23 #include "absl/base/internal/raw_logging.h"
24 #include "absl/time/internal/cctz/include/cctz/zone_info_source.h"
25 
26 namespace cctz = absl::time_internal::cctz;
27 
28 namespace absl {
29 ABSL_NAMESPACE_BEGIN
30 namespace time_internal {
31 
LoadTimeZone(const std::string & name)32 TimeZone LoadTimeZone(const std::string& name) {
33   TimeZone tz;
34   ABSL_RAW_CHECK(LoadTimeZone(name, &tz), name.c_str());
35   return tz;
36 }
37 
38 }  // namespace time_internal
39 ABSL_NAMESPACE_END
40 }  // namespace absl
41 
42 namespace absl {
43 ABSL_NAMESPACE_BEGIN
44 namespace time_internal {
45 namespace cctz_extension {
46 namespace {
47 
48 // Embed the zoneinfo data for time zones used during tests and benchmarks.
49 // The data was generated using "xxd -i zoneinfo-file".  There is no need
50 // to update the data as long as the tests do not depend on recent changes
51 // (and the past rules remain the same).
52 #include "absl/time/internal/zoneinfo.inc"
53 
54 const struct ZoneInfo {
55   const char* name;
56   const char* data;
57   std::size_t length;
58 } kZoneInfo[] = {
59     // The three real time zones used by :time_test and :time_benchmark.
60     {"America/Los_Angeles",  //
61      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
62     {"America/New_York",  //
63      reinterpret_cast<char*>(America_New_York), America_New_York_len},
64     {"Australia/Sydney",  //
65      reinterpret_cast<char*>(Australia_Sydney), Australia_Sydney_len},
66 
67     // Other zones named in tests but which should fail to load.
68     {"Invalid/TimeZone", nullptr, 0},
69     {"", nullptr, 0},
70 
71     // Allows use of the local time zone from a system-specific location.
72 #ifdef _MSC_VER
73     {"localtime",  //
74      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
75 #else
76     {"/etc/localtime",  //
77      reinterpret_cast<char*>(America_Los_Angeles), America_Los_Angeles_len},
78 #endif
79 };
80 
81 class TestZoneInfoSource : public cctz::ZoneInfoSource {
82  public:
TestZoneInfoSource(const char * data,std::size_t size)83   TestZoneInfoSource(const char* data, std::size_t size)
84       : data_(data), end_(data + size) {}
85 
Read(void * ptr,std::size_t size)86   std::size_t Read(void* ptr, std::size_t size) override {
87     const std::size_t len =
88         std::min(size, static_cast<std::size_t>(end_ - data_));
89     memcpy(ptr, data_, len);
90     data_ += len;
91     return len;
92   }
93 
Skip(std::size_t offset)94   int Skip(std::size_t offset) override {
95     data_ += std::min(offset, static_cast<std::size_t>(end_ - data_));
96     return 0;
97   }
98 
99  private:
100   const char* data_;
101   const char* const end_;
102 };
103 
TestFactory(const std::string & name,const std::function<std::unique_ptr<cctz::ZoneInfoSource> (const std::string & name)> &)104 std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
105     const std::string& name,
106     const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
107         const std::string& name)>& /*fallback_factory*/) {
108   for (const ZoneInfo& zoneinfo : kZoneInfo) {
109     if (name == zoneinfo.name) {
110       if (zoneinfo.data == nullptr) return nullptr;
111       return std::unique_ptr<cctz::ZoneInfoSource>(
112           new TestZoneInfoSource(zoneinfo.data, zoneinfo.length));
113     }
114   }
115 
116   // The embedded zoneinfo data does not include the zone, so fallback to
117   // built-in UTC. The tests have been crafted so that this should only
118   // happen when testing absl::LocalTimeZone() with an unconstrained ${TZ}.
119   return nullptr;
120 }
121 
122 }  // namespace
123 
124 #if !defined(__MINGW32__)
125 // MinGW does not support the weak symbol extension mechanism.
126 ZoneInfoSourceFactory zone_info_source_factory = TestFactory;
127 #endif
128 
129 }  // namespace cctz_extension
130 }  // namespace time_internal
131 ABSL_NAMESPACE_END
132 }  // namespace absl
133