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