abseil-cpp/absl/time/time_zone_test.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/cctz/include/cctz/time_zone.h"
16 
17 #include "gtest/gtest.h"
18 #include "absl/time/internal/test_util.h"
19 #include "absl/time/time.h"
20 
22 
23 namespace {
24 
25 TEST(TimeZone, ValueSemantics) {
26  absl::TimeZone tz;
27  absl::TimeZone tz2 = tz; // Copy-construct
28  EXPECT_EQ(tz, tz2);
29  tz2 = tz; // Copy-assign
30  EXPECT_EQ(tz, tz2);
31 }
32 
33 TEST(TimeZone, Equality) {
35  EXPECT_EQ(a, b);
36  EXPECT_EQ(a.name(), b.name());
37 
38  absl::TimeZone implicit_utc;
39  absl::TimeZone explicit_utc = absl::UTCTimeZone();
40  EXPECT_EQ(implicit_utc, explicit_utc);
41  EXPECT_EQ(implicit_utc.name(), explicit_utc.name());
42 
43  absl::TimeZone la = absl::time_internal::LoadTimeZone("America/Los_Angeles");
44  absl::TimeZone nyc = absl::time_internal::LoadTimeZone("America/New_York");
45  EXPECT_NE(la, nyc);
46 }
47 
48 TEST(TimeZone, CCTZConversion) {
50  const absl::TimeZone tz(cz);
51  EXPECT_EQ(cz, cctz::time_zone(tz));
52 }
53 
54 TEST(TimeZone, DefaultTimeZones) {
55  absl::TimeZone tz;
56  EXPECT_EQ("UTC", absl::TimeZone().name());
57  EXPECT_EQ("UTC", absl::UTCTimeZone().name());
58 }
59 
60 TEST(TimeZone, FixedTimeZone) {
61  const absl::TimeZone tz = absl::FixedTimeZone(123);
63  EXPECT_EQ(tz, absl::TimeZone(cz));
64 }
65 
66 TEST(TimeZone, LocalTimeZone) {
67  const absl::TimeZone local_tz = absl::LocalTimeZone();
69  EXPECT_EQ(tz, local_tz);
70 }
71 
72 TEST(TimeZone, NamedTimeZones) {
73  absl::TimeZone nyc = absl::time_internal::LoadTimeZone("America/New_York");
74  EXPECT_EQ("America/New_York", nyc.name());
75  absl::TimeZone syd = absl::time_internal::LoadTimeZone("Australia/Sydney");
76  EXPECT_EQ("Australia/Sydney", syd.name());
77  absl::TimeZone fixed = absl::FixedTimeZone((((3 * 60) + 25) * 60) + 45);
78  EXPECT_EQ("Fixed/UTC+03:25:45", fixed.name());
79 }
80 
81 TEST(TimeZone, Failures) {
82  absl::TimeZone tz = absl::time_internal::LoadTimeZone("America/Los_Angeles");
83  EXPECT_FALSE(LoadTimeZone("Invalid/TimeZone", &tz));
84  EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC
85 
86  // Ensures that the load still fails on a subsequent attempt.
87  tz = absl::time_internal::LoadTimeZone("America/Los_Angeles");
88  EXPECT_FALSE(LoadTimeZone("Invalid/TimeZone", &tz));
89  EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC
90 
91  // Loading an empty string timezone should fail.
92  tz = absl::time_internal::LoadTimeZone("America/Los_Angeles");
93  EXPECT_FALSE(LoadTimeZone("", &tz));
94  EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC
95 }
96 
97 } // namespace
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
fixed
int fixed(struct state *s)
Definition: bloaty/third_party/zlib/contrib/puff/puff.c:536
absl::time_internal::cctz::seconds
std::chrono::duration< std::int_fast64_t > seconds
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:40
absl::LocalTimeZone
TimeZone LocalTimeZone()
Definition: third_party/abseil-cpp/absl/time/time.h:1109
setup.name
name
Definition: setup.py:542
a
int a
Definition: abseil-cpp/absl/container/internal/hash_policy_traits_test.cc:88
absl::UTCTimeZone
TimeZone UTCTimeZone()
Definition: third_party/abseil-cpp/absl/time/time.h:1099
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
TEST
#define TEST(name, init_size,...)
Definition: arena_test.cc:75
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
absl::FixedTimeZone
TimeZone FixedTimeZone(int seconds)
Definition: third_party/abseil-cpp/absl/time/time.h:1091
absl::time_internal::cctz::utc_time_zone
time_zone utc_time_zone()
Definition: abseil-cpp/absl/time/internal/cctz/src/time_zone_lookup.cc:119
absl::time_internal::cctz::fixed_time_zone
time_zone fixed_time_zone(const seconds &offset)
Definition: abseil-cpp/absl/time/internal/cctz/src/time_zone_lookup.cc:123
b
uint64_t b
Definition: abseil-cpp/absl/container/internal/layout_test.cc:53
absl::time_internal::cctz::time_zone
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/time_zone.h:68
absl::time_internal::LoadTimeZone
TimeZone LoadTimeZone(const std::string &name)
Definition: abseil-cpp/absl/time/internal/test_util.cc:32
absl::time_internal::cctz
Definition: abseil-cpp/absl/time/internal/cctz/include/cctz/civil_time.h:24
absl::TimeZone
Definition: third_party/abseil-cpp/absl/time/time.h:912
absl::TimeZone::name
std::string name() const
Definition: third_party/abseil-cpp/absl/time/time.h:923


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