abseil-cpp/absl/time/internal/test_util.cc
Go to the documentation of this file.
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 
27 
28 namespace absl {
30 namespace time_internal {
31 
33  TimeZone tz;
34  ABSL_RAW_CHECK(LoadTimeZone(name, &tz), name.c_str());
35  return tz;
36 }
37 
38 } // namespace time_internal
40 } // namespace absl
41 
42 namespace absl {
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:
83  TestZoneInfoSource(const char* data, std::size_t size)
84  : data_(data), end_(data + size) {}
85 
86  std::size_t Read(void* ptr, std::size_t size) override {
87  const std::size_t len = std::min<std::size_t>(size, end_ - data_);
88  memcpy(ptr, data_, len);
89  data_ += len;
90  return len;
91  }
92 
93  int Skip(std::size_t offset) override {
94  data_ += std::min<std::size_t>(offset, end_ - data_);
95  return 0;
96  }
97 
98  private:
99  const char* data_;
100  const char* const end_;
101 };
102 
103 std::unique_ptr<cctz::ZoneInfoSource> TestFactory(
104  const std::string& name,
105  const std::function<std::unique_ptr<cctz::ZoneInfoSource>(
106  const std::string& name)>& /*fallback_factory*/) {
107  for (const ZoneInfo& zoneinfo : kZoneInfo) {
108  if (name == zoneinfo.name) {
109  if (zoneinfo.data == nullptr) return nullptr;
110  return std::unique_ptr<cctz::ZoneInfoSource>(
111  new TestZoneInfoSource(zoneinfo.data, zoneinfo.length));
112  }
113  }
114 
115  // The embedded zoneinfo data does not include the zone, so fallback to
116  // built-in UTC. The tests have been crafted so that this should only
117  // happen when testing absl::LocalTimeZone() with an unconstrained ${TZ}.
118  return nullptr;
119 }
120 
121 } // namespace
122 
123 #if !defined(__MINGW32__)
124 // MinGW does not support the weak symbol extension mechanism.
126 #endif
127 
128 } // namespace cctz_extension
129 } // namespace time_internal
131 } // namespace absl
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
ABSL_RAW_CHECK
#define ABSL_RAW_CHECK(condition, message)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:59
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::time_internal::cctz_extension::ZoneInfoSourceFactory
std::unique_ptr< absl::time_internal::cctz::ZoneInfoSource >(*)(const std::string &, const std::function< std::unique_ptr< absl::time_internal::cctz::ZoneInfoSource >(const std::string &)> &) ZoneInfoSourceFactory
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/zone_info_source.h:61
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
testing::internal::posix::Read
int Read(int fd, void *buf, unsigned int count)
Definition: bloaty/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:2044
absl::time_internal::cctz::ZoneInfoSource
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/zone_info_source.h:31
absl::Skip
static PerThreadSynch * Skip(PerThreadSynch *x)
Definition: abseil-cpp/absl/synchronization/mutex.cc:837
data_
const char * data_
Definition: abseil-cpp/absl/time/internal/test_util.cc:99
end_
const char *const end_
Definition: abseil-cpp/absl/time/internal/test_util.cc:100
absl::time_internal::cctz_extension::zone_info_source_factory
ZoneInfoSourceFactory zone_info_source_factory
Definition: abseil-cpp/absl/time/internal/cctz/src/zone_info_source.cc:109
absl::time_internal::LoadTimeZone
TimeZone LoadTimeZone(const std::string &name)
Definition: abseil-cpp/absl/time/internal/test_util.cc:32
data
const char * data
Definition: abseil-cpp/absl/time/internal/test_util.cc:56
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
absl::time_internal::cctz
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time.h:24
function
std::function< bool(GrpcTool *, int, const char **, const CliCredentials &, GrpcToolOutputCallback)> function
Definition: grpc_tool.cc:250
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
length
std::size_t length
Definition: abseil-cpp/absl/time/internal/test_util.cc:57
absl::TimeZone
Definition: third_party/abseil-cpp/absl/time/time.h:912
offset
voidpf uLong offset
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:142
name
const char * name
Definition: abseil-cpp/absl/time/internal/test_util.cc:55


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:31