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