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 
16 
17 #include "gtest/gtest.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 std::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
time_zone fixed_time_zone(const seconds &offset)
TimeZone FixedTimeZone(int seconds)
Definition: time.h:1018
std::string name() const
Definition: time.h:850
TimeZone UTCTimeZone()
Definition: time.h:1026
std::chrono::duration< std::int_fast64_t > seconds
Definition: time_zone.h:37
char name[1]
Definition: mutex.cc:296
TEST(Symbolize, Unimplemented)
TimeZone LoadTimeZone(const std::string &name)
Definition: test_util.cc:29
uint64_t b
Definition: layout_test.cc:50
TimeZone LocalTimeZone()
Definition: time.h:1036


abseil_cpp
Author(s):
autogenerated on Mon Feb 28 2022 21:31:21