time_zone_test.cc 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "absl/time/internal/cctz/include/cctz/time_zone.h"
  15. #include "gtest/gtest.h"
  16. #include "absl/time/internal/test_util.h"
  17. #include "absl/time/time.h"
  18. namespace cctz = absl::time_internal::cctz;
  19. namespace {
  20. TEST(TimeZone, ValueSemantics) {
  21. absl::TimeZone tz;
  22. absl::TimeZone tz2 = tz; // Copy-construct
  23. EXPECT_EQ(tz, tz2);
  24. tz2 = tz; // Copy-assign
  25. EXPECT_EQ(tz, tz2);
  26. }
  27. TEST(TimeZone, Equality) {
  28. absl::TimeZone a, b;
  29. EXPECT_EQ(a, b);
  30. EXPECT_EQ(a.name(), b.name());
  31. absl::TimeZone implicit_utc;
  32. absl::TimeZone explicit_utc = absl::UTCTimeZone();
  33. EXPECT_EQ(implicit_utc, explicit_utc);
  34. EXPECT_EQ(implicit_utc.name(), explicit_utc.name());
  35. absl::TimeZone la = absl::time_internal::LoadTimeZone("America/Los_Angeles");
  36. absl::TimeZone nyc = absl::time_internal::LoadTimeZone("America/New_York");
  37. EXPECT_NE(la, nyc);
  38. }
  39. TEST(TimeZone, CCTZConversion) {
  40. const cctz::time_zone cz = cctz::utc_time_zone();
  41. const absl::TimeZone tz(cz);
  42. EXPECT_EQ(cz, cctz::time_zone(tz));
  43. }
  44. TEST(TimeZone, DefaultTimeZones) {
  45. absl::TimeZone tz;
  46. EXPECT_EQ("UTC", absl::TimeZone().name());
  47. EXPECT_EQ("UTC", absl::UTCTimeZone().name());
  48. }
  49. TEST(TimeZone, FixedTimeZone) {
  50. const absl::TimeZone tz = absl::FixedTimeZone(123);
  51. const cctz::time_zone cz = cctz::fixed_time_zone(cctz::seconds(123));
  52. EXPECT_EQ(tz, absl::TimeZone(cz));
  53. }
  54. TEST(TimeZone, LocalTimeZone) {
  55. const absl::TimeZone local_tz = absl::LocalTimeZone();
  56. absl::TimeZone tz = absl::time_internal::LoadTimeZone("localtime");
  57. EXPECT_EQ(tz, local_tz);
  58. }
  59. TEST(TimeZone, NamedTimeZones) {
  60. absl::TimeZone nyc = absl::time_internal::LoadTimeZone("America/New_York");
  61. EXPECT_EQ("America/New_York", nyc.name());
  62. absl::TimeZone syd = absl::time_internal::LoadTimeZone("Australia/Sydney");
  63. EXPECT_EQ("Australia/Sydney", syd.name());
  64. absl::TimeZone fixed = absl::FixedTimeZone((((3 * 60) + 25) * 60) + 45);
  65. EXPECT_EQ("Fixed/UTC+03:25:45", fixed.name());
  66. }
  67. TEST(TimeZone, Failures) {
  68. absl::TimeZone tz = absl::time_internal::LoadTimeZone("America/Los_Angeles");
  69. EXPECT_FALSE(LoadTimeZone("Invalid/TimeZone", &tz));
  70. EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC
  71. // Ensures that the load still fails on a subsequent attempt.
  72. tz = absl::time_internal::LoadTimeZone("America/Los_Angeles");
  73. EXPECT_FALSE(LoadTimeZone("Invalid/TimeZone", &tz));
  74. EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC
  75. // Loading an empty string timezone should fail.
  76. tz = absl::time_internal::LoadTimeZone("America/Los_Angeles");
  77. EXPECT_FALSE(LoadTimeZone("", &tz));
  78. EXPECT_EQ(absl::UTCTimeZone(), tz); // guaranteed fallback to UTC
  79. }
  80. } // namespace