time_test.cc
Go to the documentation of this file.
00001 // Copyright 2017 The Abseil Authors.
00002 //
00003 // Licensed under the Apache License, Version 2.0 (the "License");
00004 // you may not use this file except in compliance with the License.
00005 // You may obtain a copy of the License at
00006 //
00007 //      https://www.apache.org/licenses/LICENSE-2.0
00008 //
00009 // Unless required by applicable law or agreed to in writing, software
00010 // distributed under the License is distributed on an "AS IS" BASIS,
00011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00012 // See the License for the specific language governing permissions and
00013 // limitations under the License.
00014 
00015 #include "absl/time/time.h"
00016 
00017 #include <chrono>  // NOLINT(build/c++11)
00018 #include <cstring>
00019 #include <ctime>
00020 #include <iomanip>
00021 #include <limits>
00022 #include <string>
00023 
00024 #include "gmock/gmock.h"
00025 #include "gtest/gtest.h"
00026 #include "absl/time/clock.h"
00027 #include "absl/time/internal/test_util.h"
00028 
00029 namespace {
00030 
00031 #if defined(GTEST_USES_SIMPLE_RE) && GTEST_USES_SIMPLE_RE
00032 const char kZoneAbbrRE[] = ".*";  // just punt
00033 #else
00034 const char kZoneAbbrRE[] = "[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?";
00035 #endif
00036 
00037 // This helper is a macro so that failed expectations show up with the
00038 // correct line numbers.
00039 #define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst)      \
00040   do {                                                             \
00041     EXPECT_EQ(y, ci.cs.year());                                    \
00042     EXPECT_EQ(m, ci.cs.month());                                   \
00043     EXPECT_EQ(d, ci.cs.day());                                     \
00044     EXPECT_EQ(h, ci.cs.hour());                                    \
00045     EXPECT_EQ(min, ci.cs.minute());                                \
00046     EXPECT_EQ(s, ci.cs.second());                                  \
00047     EXPECT_EQ(off, ci.offset);                                     \
00048     EXPECT_EQ(isdst, ci.is_dst);                                   \
00049     EXPECT_THAT(ci.zone_abbr, testing::MatchesRegex(kZoneAbbrRE)); \
00050   } while (0)
00051 
00052 // A gMock matcher to match timespec values. Use this matcher like:
00053 // timespec ts1, ts2;
00054 // EXPECT_THAT(ts1, TimespecMatcher(ts2));
00055 MATCHER_P(TimespecMatcher, ts, "") {
00056   if (ts.tv_sec == arg.tv_sec && ts.tv_nsec == arg.tv_nsec)
00057     return true;
00058   *result_listener << "expected: {" << ts.tv_sec << ", " << ts.tv_nsec << "} ";
00059   *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_nsec << "}";
00060   return false;
00061 }
00062 
00063 // A gMock matcher to match timeval values. Use this matcher like:
00064 // timeval tv1, tv2;
00065 // EXPECT_THAT(tv1, TimevalMatcher(tv2));
00066 MATCHER_P(TimevalMatcher, tv, "") {
00067   if (tv.tv_sec == arg.tv_sec && tv.tv_usec == arg.tv_usec)
00068     return true;
00069   *result_listener << "expected: {" << tv.tv_sec << ", " << tv.tv_usec << "} ";
00070   *result_listener << "actual: {" << arg.tv_sec << ", " << arg.tv_usec << "}";
00071   return false;
00072 }
00073 
00074 TEST(Time, ConstExpr) {
00075   constexpr absl::Time t0 = absl::UnixEpoch();
00076   static_assert(t0 == absl::Time(), "UnixEpoch");
00077   constexpr absl::Time t1 = absl::InfiniteFuture();
00078   static_assert(t1 != absl::Time(), "InfiniteFuture");
00079   constexpr absl::Time t2 = absl::InfinitePast();
00080   static_assert(t2 != absl::Time(), "InfinitePast");
00081   constexpr absl::Time t3 = absl::FromUnixNanos(0);
00082   static_assert(t3 == absl::Time(), "FromUnixNanos");
00083   constexpr absl::Time t4 = absl::FromUnixMicros(0);
00084   static_assert(t4 == absl::Time(), "FromUnixMicros");
00085   constexpr absl::Time t5 = absl::FromUnixMillis(0);
00086   static_assert(t5 == absl::Time(), "FromUnixMillis");
00087   constexpr absl::Time t6 = absl::FromUnixSeconds(0);
00088   static_assert(t6 == absl::Time(), "FromUnixSeconds");
00089   constexpr absl::Time t7 = absl::FromTimeT(0);
00090   static_assert(t7 == absl::Time(), "FromTimeT");
00091 }
00092 
00093 TEST(Time, ValueSemantics) {
00094   absl::Time a;      // Default construction
00095   absl::Time b = a;  // Copy construction
00096   EXPECT_EQ(a, b);
00097   absl::Time c(a);  // Copy construction (again)
00098   EXPECT_EQ(a, b);
00099   EXPECT_EQ(a, c);
00100   EXPECT_EQ(b, c);
00101   b = c;       // Assignment
00102   EXPECT_EQ(a, b);
00103   EXPECT_EQ(a, c);
00104   EXPECT_EQ(b, c);
00105 }
00106 
00107 TEST(Time, UnixEpoch) {
00108   const auto ci = absl::UTCTimeZone().At(absl::UnixEpoch());
00109   EXPECT_EQ(absl::CivilSecond(1970, 1, 1, 0, 0, 0), ci.cs);
00110   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
00111   EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs)));
00112 }
00113 
00114 TEST(Time, Breakdown) {
00115   absl::TimeZone tz = absl::time_internal::LoadTimeZone("America/New_York");
00116   absl::Time t = absl::UnixEpoch();
00117 
00118   // The Unix epoch as seen in NYC.
00119   auto ci = tz.At(t);
00120   EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 19, 0, 0, -18000, false);
00121   EXPECT_EQ(absl::ZeroDuration(), ci.subsecond);
00122   EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs)));
00123 
00124   // Just before the epoch.
00125   t -= absl::Nanoseconds(1);
00126   ci = tz.At(t);
00127   EXPECT_CIVIL_INFO(ci, 1969, 12, 31, 18, 59, 59, -18000, false);
00128   EXPECT_EQ(absl::Nanoseconds(999999999), ci.subsecond);
00129   EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(absl::CivilDay(ci.cs)));
00130 
00131   // Some time later.
00132   t += absl::Hours(24) * 2735;
00133   t += absl::Hours(18) + absl::Minutes(30) + absl::Seconds(15) +
00134        absl::Nanoseconds(9);
00135   ci = tz.At(t);
00136   EXPECT_CIVIL_INFO(ci, 1977, 6, 28, 14, 30, 15, -14400, true);
00137   EXPECT_EQ(8, ci.subsecond / absl::Nanoseconds(1));
00138   EXPECT_EQ(absl::Weekday::tuesday, absl::GetWeekday(absl::CivilDay(ci.cs)));
00139 }
00140 
00141 TEST(Time, AdditiveOperators) {
00142   const absl::Duration d = absl::Nanoseconds(1);
00143   const absl::Time t0;
00144   const absl::Time t1 = t0 + d;
00145 
00146   EXPECT_EQ(d, t1 - t0);
00147   EXPECT_EQ(-d, t0 - t1);
00148   EXPECT_EQ(t0, t1 - d);
00149 
00150   absl::Time t(t0);
00151   EXPECT_EQ(t0, t);
00152   t += d;
00153   EXPECT_EQ(t0 + d, t);
00154   EXPECT_EQ(d, t - t0);
00155   t -= d;
00156   EXPECT_EQ(t0, t);
00157 
00158   // Tests overflow between subseconds and seconds.
00159   t = absl::UnixEpoch();
00160   t += absl::Milliseconds(500);
00161   EXPECT_EQ(absl::UnixEpoch() + absl::Milliseconds(500), t);
00162   t += absl::Milliseconds(600);
00163   EXPECT_EQ(absl::UnixEpoch() + absl::Milliseconds(1100), t);
00164   t -= absl::Milliseconds(600);
00165   EXPECT_EQ(absl::UnixEpoch() + absl::Milliseconds(500), t);
00166   t -= absl::Milliseconds(500);
00167   EXPECT_EQ(absl::UnixEpoch(), t);
00168 }
00169 
00170 TEST(Time, RelationalOperators) {
00171   constexpr absl::Time t1 = absl::FromUnixNanos(0);
00172   constexpr absl::Time t2 = absl::FromUnixNanos(1);
00173   constexpr absl::Time t3 = absl::FromUnixNanos(2);
00174 
00175   static_assert(absl::Time() == t1, "");
00176   static_assert(t1 == t1, "");
00177   static_assert(t2 == t2, "");
00178   static_assert(t3 == t3, "");
00179 
00180   static_assert(t1 < t2, "");
00181   static_assert(t2 < t3, "");
00182   static_assert(t1 < t3, "");
00183 
00184   static_assert(t1 <= t1, "");
00185   static_assert(t1 <= t2, "");
00186   static_assert(t2 <= t2, "");
00187   static_assert(t2 <= t3, "");
00188   static_assert(t3 <= t3, "");
00189   static_assert(t1 <= t3, "");
00190 
00191   static_assert(t2 > t1, "");
00192   static_assert(t3 > t2, "");
00193   static_assert(t3 > t1, "");
00194 
00195   static_assert(t2 >= t2, "");
00196   static_assert(t2 >= t1, "");
00197   static_assert(t3 >= t3, "");
00198   static_assert(t3 >= t2, "");
00199   static_assert(t1 >= t1, "");
00200   static_assert(t3 >= t1, "");
00201 }
00202 
00203 TEST(Time, Infinity) {
00204   constexpr absl::Time ifuture = absl::InfiniteFuture();
00205   constexpr absl::Time ipast = absl::InfinitePast();
00206 
00207   static_assert(ifuture == ifuture, "");
00208   static_assert(ipast == ipast, "");
00209   static_assert(ipast < ifuture, "");
00210   static_assert(ifuture > ipast, "");
00211 
00212   // Arithmetic saturates
00213   EXPECT_EQ(ifuture, ifuture + absl::Seconds(1));
00214   EXPECT_EQ(ifuture, ifuture - absl::Seconds(1));
00215   EXPECT_EQ(ipast, ipast + absl::Seconds(1));
00216   EXPECT_EQ(ipast, ipast - absl::Seconds(1));
00217 
00218   EXPECT_EQ(absl::InfiniteDuration(), ifuture - ifuture);
00219   EXPECT_EQ(absl::InfiniteDuration(), ifuture - ipast);
00220   EXPECT_EQ(-absl::InfiniteDuration(), ipast - ifuture);
00221   EXPECT_EQ(-absl::InfiniteDuration(), ipast - ipast);
00222 
00223   constexpr absl::Time t = absl::UnixEpoch();  // Any finite time.
00224   static_assert(t < ifuture, "");
00225   static_assert(t > ipast, "");
00226 }
00227 
00228 TEST(Time, FloorConversion) {
00229 #define TEST_FLOOR_CONVERSION(TO, FROM) \
00230   EXPECT_EQ(1, TO(FROM(1001)));         \
00231   EXPECT_EQ(1, TO(FROM(1000)));         \
00232   EXPECT_EQ(0, TO(FROM(999)));          \
00233   EXPECT_EQ(0, TO(FROM(1)));            \
00234   EXPECT_EQ(0, TO(FROM(0)));            \
00235   EXPECT_EQ(-1, TO(FROM(-1)));          \
00236   EXPECT_EQ(-1, TO(FROM(-999)));        \
00237   EXPECT_EQ(-1, TO(FROM(-1000)));       \
00238   EXPECT_EQ(-2, TO(FROM(-1001)));
00239 
00240   TEST_FLOOR_CONVERSION(absl::ToUnixMicros, absl::FromUnixNanos);
00241   TEST_FLOOR_CONVERSION(absl::ToUnixMillis, absl::FromUnixMicros);
00242   TEST_FLOOR_CONVERSION(absl::ToUnixSeconds, absl::FromUnixMillis);
00243   TEST_FLOOR_CONVERSION(absl::ToTimeT, absl::FromUnixMillis);
00244 
00245 #undef TEST_FLOOR_CONVERSION
00246 
00247   // Tests ToUnixNanos.
00248   EXPECT_EQ(1, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(3) / 2));
00249   EXPECT_EQ(1, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(1)));
00250   EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(1) / 2));
00251   EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(0)));
00252   EXPECT_EQ(-1,
00253             absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(1) / 2));
00254   EXPECT_EQ(-1, absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(1)));
00255   EXPECT_EQ(-2,
00256             absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(3) / 2));
00257 
00258   // Tests ToUniversal, which uses a different epoch than the tests above.
00259   EXPECT_EQ(1,
00260             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(101)));
00261   EXPECT_EQ(1,
00262             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(100)));
00263   EXPECT_EQ(0,
00264             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(99)));
00265   EXPECT_EQ(0,
00266             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(1)));
00267   EXPECT_EQ(0,
00268             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(0)));
00269   EXPECT_EQ(-1,
00270             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-1)));
00271   EXPECT_EQ(-1,
00272             absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-99)));
00273   EXPECT_EQ(
00274       -1, absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-100)));
00275   EXPECT_EQ(
00276       -2, absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-101)));
00277 
00278   // Tests ToTimespec()/TimeFromTimespec()
00279   const struct {
00280     absl::Time t;
00281     timespec ts;
00282   } to_ts[] = {
00283       {absl::FromUnixSeconds(1) + absl::Nanoseconds(1), {1, 1}},
00284       {absl::FromUnixSeconds(1) + absl::Nanoseconds(1) / 2, {1, 0}},
00285       {absl::FromUnixSeconds(1) + absl::Nanoseconds(0), {1, 0}},
00286       {absl::FromUnixSeconds(0) + absl::Nanoseconds(0), {0, 0}},
00287       {absl::FromUnixSeconds(0) - absl::Nanoseconds(1) / 2, {-1, 999999999}},
00288       {absl::FromUnixSeconds(0) - absl::Nanoseconds(1), {-1, 999999999}},
00289       {absl::FromUnixSeconds(-1) + absl::Nanoseconds(1), {-1, 1}},
00290       {absl::FromUnixSeconds(-1) + absl::Nanoseconds(1) / 2, {-1, 0}},
00291       {absl::FromUnixSeconds(-1) + absl::Nanoseconds(0), {-1, 0}},
00292       {absl::FromUnixSeconds(-1) - absl::Nanoseconds(1) / 2, {-2, 999999999}},
00293   };
00294   for (const auto& test : to_ts) {
00295     EXPECT_THAT(absl::ToTimespec(test.t), TimespecMatcher(test.ts));
00296   }
00297   const struct {
00298     timespec ts;
00299     absl::Time t;
00300   } from_ts[] = {
00301       {{1, 1}, absl::FromUnixSeconds(1) + absl::Nanoseconds(1)},
00302       {{1, 0}, absl::FromUnixSeconds(1) + absl::Nanoseconds(0)},
00303       {{0, 0}, absl::FromUnixSeconds(0) + absl::Nanoseconds(0)},
00304       {{0, -1}, absl::FromUnixSeconds(0) - absl::Nanoseconds(1)},
00305       {{-1, 999999999}, absl::FromUnixSeconds(0) - absl::Nanoseconds(1)},
00306       {{-1, 1}, absl::FromUnixSeconds(-1) + absl::Nanoseconds(1)},
00307       {{-1, 0}, absl::FromUnixSeconds(-1) + absl::Nanoseconds(0)},
00308       {{-1, -1}, absl::FromUnixSeconds(-1) - absl::Nanoseconds(1)},
00309       {{-2, 999999999}, absl::FromUnixSeconds(-1) - absl::Nanoseconds(1)},
00310   };
00311   for (const auto& test : from_ts) {
00312     EXPECT_EQ(test.t, absl::TimeFromTimespec(test.ts));
00313   }
00314 
00315   // Tests ToTimeval()/TimeFromTimeval() (same as timespec above)
00316   const struct {
00317     absl::Time t;
00318     timeval tv;
00319   } to_tv[] = {
00320       {absl::FromUnixSeconds(1) + absl::Microseconds(1), {1, 1}},
00321       {absl::FromUnixSeconds(1) + absl::Microseconds(1) / 2, {1, 0}},
00322       {absl::FromUnixSeconds(1) + absl::Microseconds(0), {1, 0}},
00323       {absl::FromUnixSeconds(0) + absl::Microseconds(0), {0, 0}},
00324       {absl::FromUnixSeconds(0) - absl::Microseconds(1) / 2, {-1, 999999}},
00325       {absl::FromUnixSeconds(0) - absl::Microseconds(1), {-1, 999999}},
00326       {absl::FromUnixSeconds(-1) + absl::Microseconds(1), {-1, 1}},
00327       {absl::FromUnixSeconds(-1) + absl::Microseconds(1) / 2, {-1, 0}},
00328       {absl::FromUnixSeconds(-1) + absl::Microseconds(0), {-1, 0}},
00329       {absl::FromUnixSeconds(-1) - absl::Microseconds(1) / 2, {-2, 999999}},
00330   };
00331   for (const auto& test : to_tv) {
00332     EXPECT_THAT(ToTimeval(test.t), TimevalMatcher(test.tv));
00333   }
00334   const struct {
00335     timeval tv;
00336     absl::Time t;
00337   } from_tv[] = {
00338       {{1, 1}, absl::FromUnixSeconds(1) + absl::Microseconds(1)},
00339       {{1, 0}, absl::FromUnixSeconds(1) + absl::Microseconds(0)},
00340       {{0, 0}, absl::FromUnixSeconds(0) + absl::Microseconds(0)},
00341       {{0, -1}, absl::FromUnixSeconds(0) - absl::Microseconds(1)},
00342       {{-1, 999999}, absl::FromUnixSeconds(0) - absl::Microseconds(1)},
00343       {{-1, 1}, absl::FromUnixSeconds(-1) + absl::Microseconds(1)},
00344       {{-1, 0}, absl::FromUnixSeconds(-1) + absl::Microseconds(0)},
00345       {{-1, -1}, absl::FromUnixSeconds(-1) - absl::Microseconds(1)},
00346       {{-2, 999999}, absl::FromUnixSeconds(-1) - absl::Microseconds(1)},
00347   };
00348   for (const auto& test : from_tv) {
00349     EXPECT_EQ(test.t, absl::TimeFromTimeval(test.tv));
00350   }
00351 
00352   // Tests flooring near negative infinity.
00353   const int64_t min_plus_1 = std::numeric_limits<int64_t>::min() + 1;
00354   EXPECT_EQ(min_plus_1, absl::ToUnixSeconds(absl::FromUnixSeconds(min_plus_1)));
00355   EXPECT_EQ(std::numeric_limits<int64_t>::min(),
00356             absl::ToUnixSeconds(
00357                 absl::FromUnixSeconds(min_plus_1) - absl::Nanoseconds(1) / 2));
00358 
00359   // Tests flooring near positive infinity.
00360   EXPECT_EQ(std::numeric_limits<int64_t>::max(),
00361             absl::ToUnixSeconds(absl::FromUnixSeconds(
00362                 std::numeric_limits<int64_t>::max()) + absl::Nanoseconds(1) / 2));
00363   EXPECT_EQ(std::numeric_limits<int64_t>::max(),
00364             absl::ToUnixSeconds(
00365                 absl::FromUnixSeconds(std::numeric_limits<int64_t>::max())));
00366   EXPECT_EQ(std::numeric_limits<int64_t>::max() - 1,
00367             absl::ToUnixSeconds(absl::FromUnixSeconds(
00368                 std::numeric_limits<int64_t>::max()) - absl::Nanoseconds(1) / 2));
00369 }
00370 
00371 TEST(Time, RoundtripConversion) {
00372 #define TEST_CONVERSION_ROUND_TRIP(SOURCE, FROM, TO, MATCHER) \
00373   EXPECT_THAT(TO(FROM(SOURCE)), MATCHER(SOURCE))
00374 
00375   // FromUnixNanos() and ToUnixNanos()
00376   int64_t now_ns = absl::GetCurrentTimeNanos();
00377   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixNanos, absl::ToUnixNanos,
00378                              testing::Eq);
00379   TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixNanos, absl::ToUnixNanos,
00380                              testing::Eq);
00381   TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixNanos, absl::ToUnixNanos,
00382                              testing::Eq);
00383   TEST_CONVERSION_ROUND_TRIP(now_ns, absl::FromUnixNanos, absl::ToUnixNanos,
00384                              testing::Eq)
00385       << now_ns;
00386 
00387   // FromUnixMicros() and ToUnixMicros()
00388   int64_t now_us = absl::GetCurrentTimeNanos() / 1000;
00389   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixMicros, absl::ToUnixMicros,
00390                              testing::Eq);
00391   TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixMicros, absl::ToUnixMicros,
00392                              testing::Eq);
00393   TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixMicros, absl::ToUnixMicros,
00394                              testing::Eq);
00395   TEST_CONVERSION_ROUND_TRIP(now_us, absl::FromUnixMicros, absl::ToUnixMicros,
00396                              testing::Eq)
00397       << now_us;
00398 
00399   // FromUnixMillis() and ToUnixMillis()
00400   int64_t now_ms = absl::GetCurrentTimeNanos() / 1000000;
00401   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixMillis, absl::ToUnixMillis,
00402                              testing::Eq);
00403   TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixMillis, absl::ToUnixMillis,
00404                              testing::Eq);
00405   TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixMillis, absl::ToUnixMillis,
00406                              testing::Eq);
00407   TEST_CONVERSION_ROUND_TRIP(now_ms, absl::FromUnixMillis, absl::ToUnixMillis,
00408                              testing::Eq)
00409       << now_ms;
00410 
00411   // FromUnixSeconds() and ToUnixSeconds()
00412   int64_t now_s = std::time(nullptr);
00413   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUnixSeconds, absl::ToUnixSeconds,
00414                              testing::Eq);
00415   TEST_CONVERSION_ROUND_TRIP(0, absl::FromUnixSeconds, absl::ToUnixSeconds,
00416                              testing::Eq);
00417   TEST_CONVERSION_ROUND_TRIP(1, absl::FromUnixSeconds, absl::ToUnixSeconds,
00418                              testing::Eq);
00419   TEST_CONVERSION_ROUND_TRIP(now_s, absl::FromUnixSeconds, absl::ToUnixSeconds,
00420                              testing::Eq)
00421       << now_s;
00422 
00423   // FromTimeT() and ToTimeT()
00424   time_t now_time_t = std::time(nullptr);
00425   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromTimeT, absl::ToTimeT, testing::Eq);
00426   TEST_CONVERSION_ROUND_TRIP(0, absl::FromTimeT, absl::ToTimeT, testing::Eq);
00427   TEST_CONVERSION_ROUND_TRIP(1, absl::FromTimeT, absl::ToTimeT, testing::Eq);
00428   TEST_CONVERSION_ROUND_TRIP(now_time_t, absl::FromTimeT, absl::ToTimeT,
00429                              testing::Eq)
00430       << now_time_t;
00431 
00432   // TimeFromTimeval() and ToTimeval()
00433   timeval tv;
00434   tv.tv_sec = -1;
00435   tv.tv_usec = 0;
00436   TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
00437                              TimevalMatcher);
00438   tv.tv_sec = -1;
00439   tv.tv_usec = 999999;
00440   TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
00441                              TimevalMatcher);
00442   tv.tv_sec = 0;
00443   tv.tv_usec = 0;
00444   TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
00445                              TimevalMatcher);
00446   tv.tv_sec = 0;
00447   tv.tv_usec = 1;
00448   TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
00449                              TimevalMatcher);
00450   tv.tv_sec = 1;
00451   tv.tv_usec = 0;
00452   TEST_CONVERSION_ROUND_TRIP(tv, absl::TimeFromTimeval, absl::ToTimeval,
00453                              TimevalMatcher);
00454 
00455   // TimeFromTimespec() and ToTimespec()
00456   timespec ts;
00457   ts.tv_sec = -1;
00458   ts.tv_nsec = 0;
00459   TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
00460                              TimespecMatcher);
00461   ts.tv_sec = -1;
00462   ts.tv_nsec = 999999999;
00463   TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
00464                              TimespecMatcher);
00465   ts.tv_sec = 0;
00466   ts.tv_nsec = 0;
00467   TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
00468                              TimespecMatcher);
00469   ts.tv_sec = 0;
00470   ts.tv_nsec = 1;
00471   TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
00472                              TimespecMatcher);
00473   ts.tv_sec = 1;
00474   ts.tv_nsec = 0;
00475   TEST_CONVERSION_ROUND_TRIP(ts, absl::TimeFromTimespec, absl::ToTimespec,
00476                              TimespecMatcher);
00477 
00478   // FromUDate() and ToUDate()
00479   double now_ud = absl::GetCurrentTimeNanos() / 1000000;
00480   TEST_CONVERSION_ROUND_TRIP(-1.5, absl::FromUDate, absl::ToUDate,
00481                              testing::DoubleEq);
00482   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUDate, absl::ToUDate,
00483                              testing::DoubleEq);
00484   TEST_CONVERSION_ROUND_TRIP(-0.5, absl::FromUDate, absl::ToUDate,
00485                              testing::DoubleEq);
00486   TEST_CONVERSION_ROUND_TRIP(0, absl::FromUDate, absl::ToUDate,
00487                              testing::DoubleEq);
00488   TEST_CONVERSION_ROUND_TRIP(0.5, absl::FromUDate, absl::ToUDate,
00489                              testing::DoubleEq);
00490   TEST_CONVERSION_ROUND_TRIP(1, absl::FromUDate, absl::ToUDate,
00491                              testing::DoubleEq);
00492   TEST_CONVERSION_ROUND_TRIP(1.5, absl::FromUDate, absl::ToUDate,
00493                              testing::DoubleEq);
00494   TEST_CONVERSION_ROUND_TRIP(now_ud, absl::FromUDate, absl::ToUDate,
00495                              testing::DoubleEq)
00496       << std::fixed << std::setprecision(17) << now_ud;
00497 
00498   // FromUniversal() and ToUniversal()
00499   int64_t now_uni = ((719162LL * (24 * 60 * 60)) * (1000 * 1000 * 10)) +
00500                     (absl::GetCurrentTimeNanos() / 100);
00501   TEST_CONVERSION_ROUND_TRIP(-1, absl::FromUniversal, absl::ToUniversal,
00502                              testing::Eq);
00503   TEST_CONVERSION_ROUND_TRIP(0, absl::FromUniversal, absl::ToUniversal,
00504                              testing::Eq);
00505   TEST_CONVERSION_ROUND_TRIP(1, absl::FromUniversal, absl::ToUniversal,
00506                              testing::Eq);
00507   TEST_CONVERSION_ROUND_TRIP(now_uni, absl::FromUniversal, absl::ToUniversal,
00508                              testing::Eq)
00509       << now_uni;
00510 
00511 #undef TEST_CONVERSION_ROUND_TRIP
00512 }
00513 
00514 template <typename Duration>
00515 std::chrono::system_clock::time_point MakeChronoUnixTime(const Duration& d) {
00516   return std::chrono::system_clock::from_time_t(0) + d;
00517 }
00518 
00519 TEST(Time, FromChrono) {
00520   EXPECT_EQ(absl::FromTimeT(-1),
00521             absl::FromChrono(std::chrono::system_clock::from_time_t(-1)));
00522   EXPECT_EQ(absl::FromTimeT(0),
00523             absl::FromChrono(std::chrono::system_clock::from_time_t(0)));
00524   EXPECT_EQ(absl::FromTimeT(1),
00525             absl::FromChrono(std::chrono::system_clock::from_time_t(1)));
00526 
00527   EXPECT_EQ(
00528       absl::FromUnixMillis(-1),
00529       absl::FromChrono(MakeChronoUnixTime(std::chrono::milliseconds(-1))));
00530   EXPECT_EQ(absl::FromUnixMillis(0),
00531             absl::FromChrono(MakeChronoUnixTime(std::chrono::milliseconds(0))));
00532   EXPECT_EQ(absl::FromUnixMillis(1),
00533             absl::FromChrono(MakeChronoUnixTime(std::chrono::milliseconds(1))));
00534 
00535   // Chrono doesn't define exactly its range and precision (neither does
00536   // absl::Time), so let's simply test +/- ~100 years to make sure things work.
00537   const auto century_sec = 60 * 60 * 24 * 365 * int64_t{100};
00538   const auto century = std::chrono::seconds(century_sec);
00539   const auto chrono_future = MakeChronoUnixTime(century);
00540   const auto chrono_past = MakeChronoUnixTime(-century);
00541   EXPECT_EQ(absl::FromUnixSeconds(century_sec),
00542             absl::FromChrono(chrono_future));
00543   EXPECT_EQ(absl::FromUnixSeconds(-century_sec), absl::FromChrono(chrono_past));
00544 
00545   // Roundtrip them both back to chrono.
00546   EXPECT_EQ(chrono_future,
00547             absl::ToChronoTime(absl::FromUnixSeconds(century_sec)));
00548   EXPECT_EQ(chrono_past,
00549             absl::ToChronoTime(absl::FromUnixSeconds(-century_sec)));
00550 }
00551 
00552 TEST(Time, ToChronoTime) {
00553   EXPECT_EQ(std::chrono::system_clock::from_time_t(-1),
00554             absl::ToChronoTime(absl::FromTimeT(-1)));
00555   EXPECT_EQ(std::chrono::system_clock::from_time_t(0),
00556             absl::ToChronoTime(absl::FromTimeT(0)));
00557   EXPECT_EQ(std::chrono::system_clock::from_time_t(1),
00558             absl::ToChronoTime(absl::FromTimeT(1)));
00559 
00560   EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(-1)),
00561             absl::ToChronoTime(absl::FromUnixMillis(-1)));
00562   EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(0)),
00563             absl::ToChronoTime(absl::FromUnixMillis(0)));
00564   EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(1)),
00565             absl::ToChronoTime(absl::FromUnixMillis(1)));
00566 
00567   // Time before the Unix epoch should floor, not trunc.
00568   const auto tick = absl::Nanoseconds(1) / 4;
00569   EXPECT_EQ(std::chrono::system_clock::from_time_t(0) -
00570                 std::chrono::system_clock::duration(1),
00571             absl::ToChronoTime(absl::UnixEpoch() - tick));
00572 }
00573 
00574 TEST(Time, TimeZoneAt) {
00575   const absl::TimeZone nyc =
00576       absl::time_internal::LoadTimeZone("America/New_York");
00577   const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)";
00578 
00579   // A non-transition where the civil time is unique.
00580   absl::CivilSecond nov01(2013, 11, 1, 8, 30, 0);
00581   const auto nov01_ci = nyc.At(nov01);
00582   EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, nov01_ci.kind);
00583   EXPECT_EQ("Fri,  1 Nov 2013 08:30:00 -0400 (EDT)",
00584             absl::FormatTime(fmt, nov01_ci.pre, nyc));
00585   EXPECT_EQ(nov01_ci.pre, nov01_ci.trans);
00586   EXPECT_EQ(nov01_ci.pre, nov01_ci.post);
00587   EXPECT_EQ(nov01_ci.pre, absl::FromCivil(nov01, nyc));
00588 
00589   // A Spring DST transition, when there is a gap in civil time
00590   // and we prefer the later of the possible interpretations of a
00591   // non-existent time.
00592   absl::CivilSecond mar13(2011, 3, 13, 2, 15, 0);
00593   const auto mar_ci = nyc.At(mar13);
00594   EXPECT_EQ(absl::TimeZone::TimeInfo::SKIPPED, mar_ci.kind);
00595   EXPECT_EQ("Sun, 13 Mar 2011 03:15:00 -0400 (EDT)",
00596             absl::FormatTime(fmt, mar_ci.pre, nyc));
00597   EXPECT_EQ("Sun, 13 Mar 2011 03:00:00 -0400 (EDT)",
00598             absl::FormatTime(fmt, mar_ci.trans, nyc));
00599   EXPECT_EQ("Sun, 13 Mar 2011 01:15:00 -0500 (EST)",
00600             absl::FormatTime(fmt, mar_ci.post, nyc));
00601   EXPECT_EQ(mar_ci.trans, absl::FromCivil(mar13, nyc));
00602 
00603   // A Fall DST transition, when civil times are repeated and
00604   // we prefer the earlier of the possible interpretations of an
00605   // ambiguous time.
00606   absl::CivilSecond nov06(2011, 11, 6, 1, 15, 0);
00607   const auto nov06_ci = nyc.At(nov06);
00608   EXPECT_EQ(absl::TimeZone::TimeInfo::REPEATED, nov06_ci.kind);
00609   EXPECT_EQ("Sun,  6 Nov 2011 01:15:00 -0400 (EDT)",
00610             absl::FormatTime(fmt, nov06_ci.pre, nyc));
00611   EXPECT_EQ("Sun,  6 Nov 2011 01:00:00 -0500 (EST)",
00612             absl::FormatTime(fmt, nov06_ci.trans, nyc));
00613   EXPECT_EQ("Sun,  6 Nov 2011 01:15:00 -0500 (EST)",
00614             absl::FormatTime(fmt, nov06_ci.post, nyc));
00615   EXPECT_EQ(nov06_ci.pre, absl::FromCivil(nov06, nyc));
00616 
00617   // Check that (time_t) -1 is handled correctly.
00618   absl::CivilSecond minus1(1969, 12, 31, 18, 59, 59);
00619   const auto minus1_cl = nyc.At(minus1);
00620   EXPECT_EQ(absl::TimeZone::TimeInfo::UNIQUE, minus1_cl.kind);
00621   EXPECT_EQ(-1, absl::ToTimeT(minus1_cl.pre));
00622   EXPECT_EQ("Wed, 31 Dec 1969 18:59:59 -0500 (EST)",
00623             absl::FormatTime(fmt, minus1_cl.pre, nyc));
00624   EXPECT_EQ("Wed, 31 Dec 1969 23:59:59 +0000 (UTC)",
00625             absl::FormatTime(fmt, minus1_cl.pre, absl::UTCTimeZone()));
00626 }
00627 
00628 // FromCivil(CivilSecond(year, mon, day, hour, min, sec), UTCTimeZone())
00629 // has a specialized fastpath implementation, which we exercise here.
00630 TEST(Time, FromCivilUTC) {
00631   const absl::TimeZone utc = absl::UTCTimeZone();
00632   const std::string fmt = "%a, %e %b %Y %H:%M:%S %z (%Z)";
00633   const int kMax = std::numeric_limits<int>::max();
00634   const int kMin = std::numeric_limits<int>::min();
00635   absl::Time t;
00636 
00637   // 292091940881 is the last positive year to use the fastpath.
00638   t = absl::FromCivil(
00639       absl::CivilSecond(292091940881, kMax, kMax, kMax, kMax, kMax), utc);
00640   EXPECT_EQ("Fri, 25 Nov 292277026596 12:21:07 +0000 (UTC)",
00641             absl::FormatTime(fmt, t, utc));
00642   t = absl::FromCivil(
00643       absl::CivilSecond(292091940882, kMax, kMax, kMax, kMax, kMax), utc);
00644   EXPECT_EQ("infinite-future", absl::FormatTime(fmt, t, utc));  // no overflow
00645 
00646   // -292091936940 is the last negative year to use the fastpath.
00647   t = absl::FromCivil(
00648       absl::CivilSecond(-292091936940, kMin, kMin, kMin, kMin, kMin), utc);
00649   EXPECT_EQ("Fri,  1 Nov -292277022657 10:37:52 +0000 (UTC)",
00650             absl::FormatTime(fmt, t, utc));
00651   t = absl::FromCivil(
00652       absl::CivilSecond(-292091936941, kMin, kMin, kMin, kMin, kMin), utc);
00653   EXPECT_EQ("infinite-past", absl::FormatTime(fmt, t, utc));  // no underflow
00654 
00655   // Check that we're counting leap years correctly.
00656   t = absl::FromCivil(absl::CivilSecond(1900, 2, 28, 23, 59, 59), utc);
00657   EXPECT_EQ("Wed, 28 Feb 1900 23:59:59 +0000 (UTC)",
00658             absl::FormatTime(fmt, t, utc));
00659   t = absl::FromCivil(absl::CivilSecond(1900, 3, 1, 0, 0, 0), utc);
00660   EXPECT_EQ("Thu,  1 Mar 1900 00:00:00 +0000 (UTC)",
00661             absl::FormatTime(fmt, t, utc));
00662   t = absl::FromCivil(absl::CivilSecond(2000, 2, 29, 23, 59, 59), utc);
00663   EXPECT_EQ("Tue, 29 Feb 2000 23:59:59 +0000 (UTC)",
00664             absl::FormatTime(fmt, t, utc));
00665   t = absl::FromCivil(absl::CivilSecond(2000, 3, 1, 0, 0, 0), utc);
00666   EXPECT_EQ("Wed,  1 Mar 2000 00:00:00 +0000 (UTC)",
00667             absl::FormatTime(fmt, t, utc));
00668 }
00669 
00670 TEST(Time, ToTM) {
00671   const absl::TimeZone utc = absl::UTCTimeZone();
00672 
00673   // Compares the results of ToTM() to gmtime_r() for lots of times over the
00674   // course of a few days.
00675   const absl::Time start =
00676       absl::FromCivil(absl::CivilSecond(2014, 1, 2, 3, 4, 5), utc);
00677   const absl::Time end =
00678       absl::FromCivil(absl::CivilSecond(2014, 1, 5, 3, 4, 5), utc);
00679   for (absl::Time t = start; t < end; t += absl::Seconds(30)) {
00680     const struct tm tm_bt = ToTM(t, utc);
00681     const time_t tt = absl::ToTimeT(t);
00682     struct tm tm_lc;
00683 #ifdef _WIN32
00684     gmtime_s(&tm_lc, &tt);
00685 #else
00686     gmtime_r(&tt, &tm_lc);
00687 #endif
00688     EXPECT_EQ(tm_lc.tm_year, tm_bt.tm_year);
00689     EXPECT_EQ(tm_lc.tm_mon, tm_bt.tm_mon);
00690     EXPECT_EQ(tm_lc.tm_mday, tm_bt.tm_mday);
00691     EXPECT_EQ(tm_lc.tm_hour, tm_bt.tm_hour);
00692     EXPECT_EQ(tm_lc.tm_min, tm_bt.tm_min);
00693     EXPECT_EQ(tm_lc.tm_sec, tm_bt.tm_sec);
00694     EXPECT_EQ(tm_lc.tm_wday, tm_bt.tm_wday);
00695     EXPECT_EQ(tm_lc.tm_yday, tm_bt.tm_yday);
00696     EXPECT_EQ(tm_lc.tm_isdst, tm_bt.tm_isdst);
00697 
00698     ASSERT_FALSE(HasFailure());
00699   }
00700 
00701   // Checks that the tm_isdst field is correct when in standard time.
00702   const absl::TimeZone nyc =
00703       absl::time_internal::LoadTimeZone("America/New_York");
00704   absl::Time t = absl::FromCivil(absl::CivilSecond(2014, 3, 1, 0, 0, 0), nyc);
00705   struct tm tm = ToTM(t, nyc);
00706   EXPECT_FALSE(tm.tm_isdst);
00707 
00708   // Checks that the tm_isdst field is correct when in daylight time.
00709   t = absl::FromCivil(absl::CivilSecond(2014, 4, 1, 0, 0, 0), nyc);
00710   tm = ToTM(t, nyc);
00711   EXPECT_TRUE(tm.tm_isdst);
00712 
00713   // Checks overflow.
00714   tm = ToTM(absl::InfiniteFuture(), nyc);
00715   EXPECT_EQ(std::numeric_limits<int>::max() - 1900, tm.tm_year);
00716   EXPECT_EQ(11, tm.tm_mon);
00717   EXPECT_EQ(31, tm.tm_mday);
00718   EXPECT_EQ(23, tm.tm_hour);
00719   EXPECT_EQ(59, tm.tm_min);
00720   EXPECT_EQ(59, tm.tm_sec);
00721   EXPECT_EQ(4, tm.tm_wday);
00722   EXPECT_EQ(364, tm.tm_yday);
00723   EXPECT_FALSE(tm.tm_isdst);
00724 
00725   // Checks underflow.
00726   tm = ToTM(absl::InfinitePast(), nyc);
00727   EXPECT_EQ(std::numeric_limits<int>::min(), tm.tm_year);
00728   EXPECT_EQ(0, tm.tm_mon);
00729   EXPECT_EQ(1, tm.tm_mday);
00730   EXPECT_EQ(0, tm.tm_hour);
00731   EXPECT_EQ(0, tm.tm_min);
00732   EXPECT_EQ(0, tm.tm_sec);
00733   EXPECT_EQ(0, tm.tm_wday);
00734   EXPECT_EQ(0, tm.tm_yday);
00735   EXPECT_FALSE(tm.tm_isdst);
00736 }
00737 
00738 TEST(Time, FromTM) {
00739   const absl::TimeZone nyc =
00740       absl::time_internal::LoadTimeZone("America/New_York");
00741 
00742   // Verifies that tm_isdst doesn't affect anything when the time is unique.
00743   struct tm tm;
00744   std::memset(&tm, 0, sizeof(tm));
00745   tm.tm_year = 2014 - 1900;
00746   tm.tm_mon = 6 - 1;
00747   tm.tm_mday = 28;
00748   tm.tm_hour = 1;
00749   tm.tm_min = 2;
00750   tm.tm_sec = 3;
00751   tm.tm_isdst = -1;
00752   absl::Time t = FromTM(tm, nyc);
00753   EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc));  // DST
00754   tm.tm_isdst = 0;
00755   t = FromTM(tm, nyc);
00756   EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc));  // DST
00757   tm.tm_isdst = 1;
00758   t = FromTM(tm, nyc);
00759   EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc));  // DST
00760 
00761   // Adjusts tm to refer to an ambiguous time.
00762   tm.tm_year = 2014 - 1900;
00763   tm.tm_mon = 11 - 1;
00764   tm.tm_mday = 2;
00765   tm.tm_hour = 1;
00766   tm.tm_min = 30;
00767   tm.tm_sec = 42;
00768   tm.tm_isdst = -1;
00769   t = FromTM(tm, nyc);
00770   EXPECT_EQ("2014-11-02T01:30:42-04:00", absl::FormatTime(t, nyc));  // DST
00771   tm.tm_isdst = 0;
00772   t = FromTM(tm, nyc);
00773   EXPECT_EQ("2014-11-02T01:30:42-05:00", absl::FormatTime(t, nyc));  // STD
00774   tm.tm_isdst = 1;
00775   t = FromTM(tm, nyc);
00776   EXPECT_EQ("2014-11-02T01:30:42-04:00", absl::FormatTime(t, nyc));  // DST
00777 
00778   // Adjusts tm to refer to a skipped time.
00779   tm.tm_year = 2014 - 1900;
00780   tm.tm_mon = 3 - 1;
00781   tm.tm_mday = 9;
00782   tm.tm_hour = 2;
00783   tm.tm_min = 30;
00784   tm.tm_sec = 42;
00785   tm.tm_isdst = -1;
00786   t = FromTM(tm, nyc);
00787   EXPECT_EQ("2014-03-09T03:30:42-04:00", absl::FormatTime(t, nyc));  // DST
00788   tm.tm_isdst = 0;
00789   t = FromTM(tm, nyc);
00790   EXPECT_EQ("2014-03-09T01:30:42-05:00", absl::FormatTime(t, nyc));  // STD
00791   tm.tm_isdst = 1;
00792   t = FromTM(tm, nyc);
00793   EXPECT_EQ("2014-03-09T03:30:42-04:00", absl::FormatTime(t, nyc));  // DST
00794 }
00795 
00796 TEST(Time, TMRoundTrip) {
00797   const absl::TimeZone nyc =
00798       absl::time_internal::LoadTimeZone("America/New_York");
00799 
00800   // Test round-tripping across a skipped transition
00801   absl::Time start = absl::FromCivil(absl::CivilHour(2014, 3, 9, 0), nyc);
00802   absl::Time end = absl::FromCivil(absl::CivilHour(2014, 3, 9, 4), nyc);
00803   for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
00804     struct tm tm = ToTM(t, nyc);
00805     absl::Time rt = FromTM(tm, nyc);
00806     EXPECT_EQ(rt, t);
00807   }
00808 
00809   // Test round-tripping across an ambiguous transition
00810   start = absl::FromCivil(absl::CivilHour(2014, 11, 2, 0), nyc);
00811   end = absl::FromCivil(absl::CivilHour(2014, 11, 2, 4), nyc);
00812   for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
00813     struct tm tm = ToTM(t, nyc);
00814     absl::Time rt = FromTM(tm, nyc);
00815     EXPECT_EQ(rt, t);
00816   }
00817 
00818   // Test round-tripping of unique instants crossing a day boundary
00819   start = absl::FromCivil(absl::CivilHour(2014, 6, 27, 22), nyc);
00820   end = absl::FromCivil(absl::CivilHour(2014, 6, 28, 4), nyc);
00821   for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
00822     struct tm tm = ToTM(t, nyc);
00823     absl::Time rt = FromTM(tm, nyc);
00824     EXPECT_EQ(rt, t);
00825   }
00826 }
00827 
00828 TEST(Time, Range) {
00829   // The API's documented range is +/- 100 billion years.
00830   const absl::Duration range = absl::Hours(24) * 365.2425 * 100000000000;
00831 
00832   // Arithmetic and comparison still works at +/-range around base values.
00833   absl::Time bases[2] = {absl::UnixEpoch(), absl::Now()};
00834   for (const auto base : bases) {
00835     absl::Time bottom = base - range;
00836     EXPECT_GT(bottom, bottom - absl::Nanoseconds(1));
00837     EXPECT_LT(bottom, bottom + absl::Nanoseconds(1));
00838     absl::Time top = base + range;
00839     EXPECT_GT(top, top - absl::Nanoseconds(1));
00840     EXPECT_LT(top, top + absl::Nanoseconds(1));
00841     absl::Duration full_range = 2 * range;
00842     EXPECT_EQ(full_range, top - bottom);
00843     EXPECT_EQ(-full_range, bottom - top);
00844   }
00845 }
00846 
00847 TEST(Time, Limits) {
00848   // It is an implementation detail that Time().rep_ == ZeroDuration(),
00849   // and that the resolution of a Duration is 1/4 of a nanosecond.
00850   const absl::Time zero;
00851   const absl::Time max =
00852       zero + absl::Seconds(std::numeric_limits<int64_t>::max()) +
00853       absl::Nanoseconds(999999999) + absl::Nanoseconds(3) / 4;
00854   const absl::Time min =
00855       zero + absl::Seconds(std::numeric_limits<int64_t>::min());
00856 
00857   // Some simple max/min bounds checks.
00858   EXPECT_LT(max, absl::InfiniteFuture());
00859   EXPECT_GT(min, absl::InfinitePast());
00860   EXPECT_LT(zero, max);
00861   EXPECT_GT(zero, min);
00862   EXPECT_GE(absl::UnixEpoch(), min);
00863   EXPECT_LT(absl::UnixEpoch(), max);
00864 
00865   // Check sign of Time differences.
00866   EXPECT_LT(absl::ZeroDuration(), max - zero);
00867   EXPECT_LT(absl::ZeroDuration(),
00868             zero - absl::Nanoseconds(1) / 4 - min);  // avoid zero - min
00869 
00870   // Arithmetic works at max - 0.25ns and min + 0.25ns.
00871   EXPECT_GT(max, max - absl::Nanoseconds(1) / 4);
00872   EXPECT_LT(min, min + absl::Nanoseconds(1) / 4);
00873 }
00874 
00875 TEST(Time, ConversionSaturation) {
00876   const absl::TimeZone utc = absl::UTCTimeZone();
00877   absl::Time t;
00878 
00879   const auto max_time_t = std::numeric_limits<time_t>::max();
00880   const auto min_time_t = std::numeric_limits<time_t>::min();
00881   time_t tt = max_time_t - 1;
00882   t = absl::FromTimeT(tt);
00883   tt = absl::ToTimeT(t);
00884   EXPECT_EQ(max_time_t - 1, tt);
00885   t += absl::Seconds(1);
00886   tt = absl::ToTimeT(t);
00887   EXPECT_EQ(max_time_t, tt);
00888   t += absl::Seconds(1);  // no effect
00889   tt = absl::ToTimeT(t);
00890   EXPECT_EQ(max_time_t, tt);
00891 
00892   tt = min_time_t + 1;
00893   t = absl::FromTimeT(tt);
00894   tt = absl::ToTimeT(t);
00895   EXPECT_EQ(min_time_t + 1, tt);
00896   t -= absl::Seconds(1);
00897   tt = absl::ToTimeT(t);
00898   EXPECT_EQ(min_time_t, tt);
00899   t -= absl::Seconds(1);  // no effect
00900   tt = absl::ToTimeT(t);
00901   EXPECT_EQ(min_time_t, tt);
00902 
00903   const auto max_timeval_sec =
00904       std::numeric_limits<decltype(timeval::tv_sec)>::max();
00905   const auto min_timeval_sec =
00906       std::numeric_limits<decltype(timeval::tv_sec)>::min();
00907   timeval tv;
00908   tv.tv_sec = max_timeval_sec;
00909   tv.tv_usec = 999998;
00910   t = absl::TimeFromTimeval(tv);
00911   tv = ToTimeval(t);
00912   EXPECT_EQ(max_timeval_sec, tv.tv_sec);
00913   EXPECT_EQ(999998, tv.tv_usec);
00914   t += absl::Microseconds(1);
00915   tv = ToTimeval(t);
00916   EXPECT_EQ(max_timeval_sec, tv.tv_sec);
00917   EXPECT_EQ(999999, tv.tv_usec);
00918   t += absl::Microseconds(1);  // no effect
00919   tv = ToTimeval(t);
00920   EXPECT_EQ(max_timeval_sec, tv.tv_sec);
00921   EXPECT_EQ(999999, tv.tv_usec);
00922 
00923   tv.tv_sec = min_timeval_sec;
00924   tv.tv_usec = 1;
00925   t = absl::TimeFromTimeval(tv);
00926   tv = ToTimeval(t);
00927   EXPECT_EQ(min_timeval_sec, tv.tv_sec);
00928   EXPECT_EQ(1, tv.tv_usec);
00929   t -= absl::Microseconds(1);
00930   tv = ToTimeval(t);
00931   EXPECT_EQ(min_timeval_sec, tv.tv_sec);
00932   EXPECT_EQ(0, tv.tv_usec);
00933   t -= absl::Microseconds(1);  // no effect
00934   tv = ToTimeval(t);
00935   EXPECT_EQ(min_timeval_sec, tv.tv_sec);
00936   EXPECT_EQ(0, tv.tv_usec);
00937 
00938   const auto max_timespec_sec =
00939       std::numeric_limits<decltype(timespec::tv_sec)>::max();
00940   const auto min_timespec_sec =
00941       std::numeric_limits<decltype(timespec::tv_sec)>::min();
00942   timespec ts;
00943   ts.tv_sec = max_timespec_sec;
00944   ts.tv_nsec = 999999998;
00945   t = absl::TimeFromTimespec(ts);
00946   ts = absl::ToTimespec(t);
00947   EXPECT_EQ(max_timespec_sec, ts.tv_sec);
00948   EXPECT_EQ(999999998, ts.tv_nsec);
00949   t += absl::Nanoseconds(1);
00950   ts = absl::ToTimespec(t);
00951   EXPECT_EQ(max_timespec_sec, ts.tv_sec);
00952   EXPECT_EQ(999999999, ts.tv_nsec);
00953   t += absl::Nanoseconds(1);  // no effect
00954   ts = absl::ToTimespec(t);
00955   EXPECT_EQ(max_timespec_sec, ts.tv_sec);
00956   EXPECT_EQ(999999999, ts.tv_nsec);
00957 
00958   ts.tv_sec = min_timespec_sec;
00959   ts.tv_nsec = 1;
00960   t = absl::TimeFromTimespec(ts);
00961   ts = absl::ToTimespec(t);
00962   EXPECT_EQ(min_timespec_sec, ts.tv_sec);
00963   EXPECT_EQ(1, ts.tv_nsec);
00964   t -= absl::Nanoseconds(1);
00965   ts = absl::ToTimespec(t);
00966   EXPECT_EQ(min_timespec_sec, ts.tv_sec);
00967   EXPECT_EQ(0, ts.tv_nsec);
00968   t -= absl::Nanoseconds(1);  // no effect
00969   ts = absl::ToTimespec(t);
00970   EXPECT_EQ(min_timespec_sec, ts.tv_sec);
00971   EXPECT_EQ(0, ts.tv_nsec);
00972 
00973   // Checks how TimeZone::At() saturates on infinities.
00974   auto ci = utc.At(absl::InfiniteFuture());
00975   EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::max(), 12, 31, 23,
00976                             59, 59, 0, false);
00977   EXPECT_EQ(absl::InfiniteDuration(), ci.subsecond);
00978   EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(absl::CivilDay(ci.cs)));
00979   EXPECT_EQ(365, absl::GetYearDay(absl::CivilDay(ci.cs)));
00980   EXPECT_STREQ("-00", ci.zone_abbr);  // artifact of TimeZone::At()
00981   ci = utc.At(absl::InfinitePast());
00982   EXPECT_CIVIL_INFO(ci, std::numeric_limits<int64_t>::min(), 1, 1, 0, 0,
00983                             0, 0, false);
00984   EXPECT_EQ(-absl::InfiniteDuration(), ci.subsecond);
00985   EXPECT_EQ(absl::Weekday::sunday, absl::GetWeekday(absl::CivilDay(ci.cs)));
00986   EXPECT_EQ(1, absl::GetYearDay(absl::CivilDay(ci.cs)));
00987   EXPECT_STREQ("-00", ci.zone_abbr);  // artifact of TimeZone::At()
00988 
00989   // Approach the maximal Time value from below.
00990   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 6), utc);
00991   EXPECT_EQ("292277026596-12-04T15:30:06+00:00",
00992             absl::FormatTime(absl::RFC3339_full, t, utc));
00993   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 7), utc);
00994   EXPECT_EQ("292277026596-12-04T15:30:07+00:00",
00995             absl::FormatTime(absl::RFC3339_full, t, utc));
00996   EXPECT_EQ(
00997       absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::max()), t);
00998 
00999   // Checks that we can also get the maximal Time value for a far-east zone.
01000   const absl::TimeZone plus14 = absl::FixedTimeZone(14 * 60 * 60);
01001   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 5, 30, 7), plus14);
01002   EXPECT_EQ("292277026596-12-05T05:30:07+14:00",
01003             absl::FormatTime(absl::RFC3339_full, t, plus14));
01004   EXPECT_EQ(
01005       absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::max()), t);
01006 
01007   // One second later should push us to infinity.
01008   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 15, 30, 8), utc);
01009   EXPECT_EQ("infinite-future", absl::FormatTime(absl::RFC3339_full, t, utc));
01010 
01011   // Approach the minimal Time value from above.
01012   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 53), utc);
01013   EXPECT_EQ("-292277022657-01-27T08:29:53+00:00",
01014             absl::FormatTime(absl::RFC3339_full, t, utc));
01015   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 52), utc);
01016   EXPECT_EQ("-292277022657-01-27T08:29:52+00:00",
01017             absl::FormatTime(absl::RFC3339_full, t, utc));
01018   EXPECT_EQ(
01019       absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::min()), t);
01020 
01021   // Checks that we can also get the minimal Time value for a far-west zone.
01022   const absl::TimeZone minus12 = absl::FixedTimeZone(-12 * 60 * 60);
01023   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 26, 20, 29, 52),
01024                       minus12);
01025   EXPECT_EQ("-292277022657-01-26T20:29:52-12:00",
01026             absl::FormatTime(absl::RFC3339_full, t, minus12));
01027   EXPECT_EQ(
01028       absl::UnixEpoch() + absl::Seconds(std::numeric_limits<int64_t>::min()), t);
01029 
01030   // One second before should push us to -infinity.
01031   t = absl::FromCivil(absl::CivilSecond(-292277022657, 1, 27, 8, 29, 51), utc);
01032   EXPECT_EQ("infinite-past", absl::FormatTime(absl::RFC3339_full, t, utc));
01033 }
01034 
01035 // In zones with POSIX-style recurring rules we use special logic to
01036 // handle conversions in the distant future.  Here we check the limits
01037 // of those conversions, particularly with respect to integer overflow.
01038 TEST(Time, ExtendedConversionSaturation) {
01039   const absl::TimeZone syd =
01040       absl::time_internal::LoadTimeZone("Australia/Sydney");
01041   const absl::TimeZone nyc =
01042       absl::time_internal::LoadTimeZone("America/New_York");
01043   const absl::Time max =
01044       absl::FromUnixSeconds(std::numeric_limits<int64_t>::max());
01045   absl::TimeZone::CivilInfo ci;
01046   absl::Time t;
01047 
01048   // The maximal time converted in each zone.
01049   ci = syd.At(max);
01050   EXPECT_CIVIL_INFO(ci, 292277026596, 12, 5, 2, 30, 7, 39600, true);
01051   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 7), syd);
01052   EXPECT_EQ(max, t);
01053   ci = nyc.At(max);
01054   EXPECT_CIVIL_INFO(ci, 292277026596, 12, 4, 10, 30, 7, -18000, false);
01055   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 7), nyc);
01056   EXPECT_EQ(max, t);
01057 
01058   // One second later should push us to infinity.
01059   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 8), syd);
01060   EXPECT_EQ(absl::InfiniteFuture(), t);
01061   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 8), nyc);
01062   EXPECT_EQ(absl::InfiniteFuture(), t);
01063 
01064   // And we should stick there.
01065   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 5, 2, 30, 9), syd);
01066   EXPECT_EQ(absl::InfiniteFuture(), t);
01067   t = absl::FromCivil(absl::CivilSecond(292277026596, 12, 4, 10, 30, 9), nyc);
01068   EXPECT_EQ(absl::InfiniteFuture(), t);
01069 
01070   // All the way up to a saturated date/time, without overflow.
01071   t = absl::FromCivil(absl::CivilSecond::max(), syd);
01072   EXPECT_EQ(absl::InfiniteFuture(), t);
01073   t = absl::FromCivil(absl::CivilSecond::max(), nyc);
01074   EXPECT_EQ(absl::InfiniteFuture(), t);
01075 }
01076 
01077 TEST(Time, FromCivilAlignment) {
01078   const absl::TimeZone utc = absl::UTCTimeZone();
01079   const absl::CivilSecond cs(2015, 2, 3, 4, 5, 6);
01080   absl::Time t = absl::FromCivil(cs, utc);
01081   EXPECT_EQ("2015-02-03T04:05:06+00:00", absl::FormatTime(t, utc));
01082   t = absl::FromCivil(absl::CivilMinute(cs), utc);
01083   EXPECT_EQ("2015-02-03T04:05:00+00:00", absl::FormatTime(t, utc));
01084   t = absl::FromCivil(absl::CivilHour(cs), utc);
01085   EXPECT_EQ("2015-02-03T04:00:00+00:00", absl::FormatTime(t, utc));
01086   t = absl::FromCivil(absl::CivilDay(cs), utc);
01087   EXPECT_EQ("2015-02-03T00:00:00+00:00", absl::FormatTime(t, utc));
01088   t = absl::FromCivil(absl::CivilMonth(cs), utc);
01089   EXPECT_EQ("2015-02-01T00:00:00+00:00", absl::FormatTime(t, utc));
01090   t = absl::FromCivil(absl::CivilYear(cs), utc);
01091   EXPECT_EQ("2015-01-01T00:00:00+00:00", absl::FormatTime(t, utc));
01092 }
01093 
01094 TEST(Time, LegacyDateTime) {
01095   const absl::TimeZone utc = absl::UTCTimeZone();
01096   const std::string ymdhms = "%Y-%m-%d %H:%M:%S";
01097   const int kMax = std::numeric_limits<int>::max();
01098   const int kMin = std::numeric_limits<int>::min();
01099   absl::Time t;
01100 
01101   t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::max(),
01102                          kMax, kMax, kMax, kMax, kMax, utc);
01103   EXPECT_EQ("infinite-future",
01104             absl::FormatTime(ymdhms, t, utc));  // no overflow
01105   t = absl::FromDateTime(std::numeric_limits<absl::civil_year_t>::min(),
01106                          kMin, kMin, kMin, kMin, kMin, utc);
01107   EXPECT_EQ("infinite-past",
01108             absl::FormatTime(ymdhms, t, utc));  // no overflow
01109 
01110   // Check normalization.
01111   EXPECT_TRUE(absl::ConvertDateTime(2013, 10, 32, 8, 30, 0, utc).normalized);
01112   t = absl::FromDateTime(2015, 1, 1, 0, 0, 60, utc);
01113   EXPECT_EQ("2015-01-01 00:01:00", absl::FormatTime(ymdhms, t, utc));
01114   t = absl::FromDateTime(2015, 1, 1, 0, 60, 0, utc);
01115   EXPECT_EQ("2015-01-01 01:00:00", absl::FormatTime(ymdhms, t, utc));
01116   t = absl::FromDateTime(2015, 1, 1, 24, 0, 0, utc);
01117   EXPECT_EQ("2015-01-02 00:00:00", absl::FormatTime(ymdhms, t, utc));
01118   t = absl::FromDateTime(2015, 1, 32, 0, 0, 0, utc);
01119   EXPECT_EQ("2015-02-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
01120   t = absl::FromDateTime(2015, 13, 1, 0, 0, 0, utc);
01121   EXPECT_EQ("2016-01-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
01122   t = absl::FromDateTime(2015, 13, 32, 60, 60, 60, utc);
01123   EXPECT_EQ("2016-02-03 13:01:00", absl::FormatTime(ymdhms, t, utc));
01124   t = absl::FromDateTime(2015, 1, 1, 0, 0, -1, utc);
01125   EXPECT_EQ("2014-12-31 23:59:59", absl::FormatTime(ymdhms, t, utc));
01126   t = absl::FromDateTime(2015, 1, 1, 0, -1, 0, utc);
01127   EXPECT_EQ("2014-12-31 23:59:00", absl::FormatTime(ymdhms, t, utc));
01128   t = absl::FromDateTime(2015, 1, 1, -1, 0, 0, utc);
01129   EXPECT_EQ("2014-12-31 23:00:00", absl::FormatTime(ymdhms, t, utc));
01130   t = absl::FromDateTime(2015, 1, -1, 0, 0, 0, utc);
01131   EXPECT_EQ("2014-12-30 00:00:00", absl::FormatTime(ymdhms, t, utc));
01132   t = absl::FromDateTime(2015, -1, 1, 0, 0, 0, utc);
01133   EXPECT_EQ("2014-11-01 00:00:00", absl::FormatTime(ymdhms, t, utc));
01134   t = absl::FromDateTime(2015, -1, -1, -1, -1, -1, utc);
01135   EXPECT_EQ("2014-10-29 22:58:59", absl::FormatTime(ymdhms, t, utc));
01136 }
01137 
01138 TEST(Time, NextTransitionUTC) {
01139   const auto tz = absl::UTCTimeZone();
01140   absl::TimeZone::CivilTransition trans;
01141 
01142   auto t = absl::InfinitePast();
01143   EXPECT_FALSE(tz.NextTransition(t, &trans));
01144 
01145   t = absl::InfiniteFuture();
01146   EXPECT_FALSE(tz.NextTransition(t, &trans));
01147 }
01148 
01149 TEST(Time, PrevTransitionUTC) {
01150   const auto tz = absl::UTCTimeZone();
01151   absl::TimeZone::CivilTransition trans;
01152 
01153   auto t = absl::InfiniteFuture();
01154   EXPECT_FALSE(tz.PrevTransition(t, &trans));
01155 
01156   t = absl::InfinitePast();
01157   EXPECT_FALSE(tz.PrevTransition(t, &trans));
01158 }
01159 
01160 TEST(Time, NextTransitionNYC) {
01161   const auto tz = absl::time_internal::LoadTimeZone("America/New_York");
01162   absl::TimeZone::CivilTransition trans;
01163 
01164   auto t = absl::FromCivil(absl::CivilSecond(2018, 6, 30, 0, 0, 0), tz);
01165   EXPECT_TRUE(tz.NextTransition(t, &trans));
01166   EXPECT_EQ(absl::CivilSecond(2018, 11, 4, 2, 0, 0), trans.from);
01167   EXPECT_EQ(absl::CivilSecond(2018, 11, 4, 1, 0, 0), trans.to);
01168 
01169   t = absl::InfiniteFuture();
01170   EXPECT_FALSE(tz.NextTransition(t, &trans));
01171 
01172   t = absl::InfinitePast();
01173   EXPECT_TRUE(tz.NextTransition(t, &trans));
01174   if (trans.from == absl::CivilSecond(1918, 03, 31, 2, 0, 0)) {
01175     // It looks like the tzdata is only 32 bit (probably macOS),
01176     // which bottoms out at 1901-12-13T20:45:52+00:00.
01177     EXPECT_EQ(absl::CivilSecond(1918, 3, 31, 3, 0, 0), trans.to);
01178   } else {
01179     EXPECT_EQ(absl::CivilSecond(1883, 11, 18, 12, 3, 58), trans.from);
01180     EXPECT_EQ(absl::CivilSecond(1883, 11, 18, 12, 0, 0), trans.to);
01181   }
01182 }
01183 
01184 TEST(Time, PrevTransitionNYC) {
01185   const auto tz = absl::time_internal::LoadTimeZone("America/New_York");
01186   absl::TimeZone::CivilTransition trans;
01187 
01188   auto t = absl::FromCivil(absl::CivilSecond(2018, 6, 30, 0, 0, 0), tz);
01189   EXPECT_TRUE(tz.PrevTransition(t, &trans));
01190   EXPECT_EQ(absl::CivilSecond(2018, 3, 11, 2, 0, 0), trans.from);
01191   EXPECT_EQ(absl::CivilSecond(2018, 3, 11, 3, 0, 0), trans.to);
01192 
01193   t = absl::InfinitePast();
01194   EXPECT_FALSE(tz.PrevTransition(t, &trans));
01195 
01196   t = absl::InfiniteFuture();
01197   EXPECT_TRUE(tz.PrevTransition(t, &trans));
01198   // We have a transition but we don't know which one.
01199 }
01200 
01201 }  // namespace


abseil_cpp
Author(s):
autogenerated on Wed Jun 19 2019 19:42:15