Austin Schuh | 36244a1 | 2019-09-21 17:52:38 -0700 | [diff] [blame^] | 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 | |
| 21 | namespace cctz = absl::time_internal::cctz; |
| 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) { |
| 34 | absl::TimeZone a, b; |
| 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) { |
| 49 | const cctz::time_zone cz = cctz::utc_time_zone(); |
| 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); |
| 62 | const cctz::time_zone cz = cctz::fixed_time_zone(cctz::seconds(123)); |
| 63 | EXPECT_EQ(tz, absl::TimeZone(cz)); |
| 64 | } |
| 65 | |
| 66 | TEST(TimeZone, LocalTimeZone) { |
| 67 | const absl::TimeZone local_tz = absl::LocalTimeZone(); |
| 68 | absl::TimeZone tz = absl::time_internal::LoadTimeZone("localtime"); |
| 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 |