blob: 9a485a0750e486569a1ae7f167a61e8f73fdab8b [file] [log] [blame]
Austin Schuh36244a12019-09-21 17:52:38 -07001// 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
Austin Schuhb4691e92020-12-31 12:37:18 -080021#include "absl/base/config.h"
Austin Schuh36244a12019-09-21 17:52:38 -070022#include "absl/base/internal/raw_logging.h"
23#include "absl/time/internal/cctz/include/cctz/zone_info_source.h"
24
25namespace cctz = absl::time_internal::cctz;
26
27namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080028ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070029namespace time_internal {
30
31TimeZone 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
Austin Schuhb4691e92020-12-31 12:37:18 -080038ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -070039} // namespace absl
40
41namespace absl {
Austin Schuhb4691e92020-12-31 12:37:18 -080042ABSL_NAMESPACE_BEGIN
Austin Schuh36244a12019-09-21 17:52:38 -070043namespace time_internal {
44namespace cctz_extension {
45namespace {
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
53const 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
84class TestZoneInfoSource : public cctz::ZoneInfoSource {
85 public:
86 TestZoneInfoSource(const char* data, std::size_t size)
87 : data_(data), end_(data + size) {}
88
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
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
106std::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
Austin Schuhb4691e92020-12-31 12:37:18 -0800123#if !defined(__MINGW32__)
124// MinGW does not support the weak symbol extension mechanism.
Austin Schuh36244a12019-09-21 17:52:38 -0700125ZoneInfoSourceFactory zone_info_source_factory = TestFactory;
Austin Schuhb4691e92020-12-31 12:37:18 -0800126#endif
Austin Schuh36244a12019-09-21 17:52:38 -0700127
128} // namespace cctz_extension
129} // namespace time_internal
Austin Schuhb4691e92020-12-31 12:37:18 -0800130ABSL_NAMESPACE_END
Austin Schuh36244a12019-09-21 17:52:38 -0700131} // namespace absl