civil_time_test.cc
Go to the documentation of this file.
00001 // Copyright 2018 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/civil_time.h"
00016 
00017 #include <limits>
00018 #include <sstream>
00019 #include <type_traits>
00020 
00021 #include "absl/base/macros.h"
00022 #include "gtest/gtest.h"
00023 
00024 namespace {
00025 
00026 TEST(CivilTime, DefaultConstruction) {
00027   absl::CivilSecond ss;
00028   EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
00029 
00030   absl::CivilMinute mm;
00031   EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
00032 
00033   absl::CivilHour hh;
00034   EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
00035 
00036   absl::CivilDay d;
00037   EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
00038 
00039   absl::CivilMonth m;
00040   EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
00041 
00042   absl::CivilYear y;
00043   EXPECT_EQ("1970", absl::FormatCivilTime(y));
00044 }
00045 
00046 TEST(CivilTime, StructMember) {
00047   struct S {
00048     absl::CivilDay day;
00049   };
00050   S s = {};
00051   EXPECT_EQ(absl::CivilDay{}, s.day);
00052 }
00053 
00054 TEST(CivilTime, FieldsConstruction) {
00055   EXPECT_EQ("2015-01-02T03:04:05",
00056             absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4, 5)));
00057   EXPECT_EQ("2015-01-02T03:04:00",
00058             absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4)));
00059   EXPECT_EQ("2015-01-02T03:00:00",
00060             absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3)));
00061   EXPECT_EQ("2015-01-02T00:00:00",
00062             absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2)));
00063   EXPECT_EQ("2015-01-01T00:00:00",
00064             absl::FormatCivilTime(absl::CivilSecond(2015, 1)));
00065   EXPECT_EQ("2015-01-01T00:00:00",
00066             absl::FormatCivilTime(absl::CivilSecond(2015)));
00067 
00068   EXPECT_EQ("2015-01-02T03:04",
00069             absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4, 5)));
00070   EXPECT_EQ("2015-01-02T03:04",
00071             absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4)));
00072   EXPECT_EQ("2015-01-02T03:00",
00073             absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3)));
00074   EXPECT_EQ("2015-01-02T00:00",
00075             absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2)));
00076   EXPECT_EQ("2015-01-01T00:00",
00077             absl::FormatCivilTime(absl::CivilMinute(2015, 1)));
00078   EXPECT_EQ("2015-01-01T00:00",
00079             absl::FormatCivilTime(absl::CivilMinute(2015)));
00080 
00081   EXPECT_EQ("2015-01-02T03",
00082             absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4, 5)));
00083   EXPECT_EQ("2015-01-02T03",
00084             absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4)));
00085   EXPECT_EQ("2015-01-02T03",
00086             absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3)));
00087   EXPECT_EQ("2015-01-02T00",
00088             absl::FormatCivilTime(absl::CivilHour(2015, 1, 2)));
00089   EXPECT_EQ("2015-01-01T00",
00090             absl::FormatCivilTime(absl::CivilHour(2015, 1)));
00091   EXPECT_EQ("2015-01-01T00",
00092             absl::FormatCivilTime(absl::CivilHour(2015)));
00093 
00094   EXPECT_EQ("2015-01-02",
00095             absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4, 5)));
00096   EXPECT_EQ("2015-01-02",
00097             absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4)));
00098   EXPECT_EQ("2015-01-02",
00099             absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3)));
00100   EXPECT_EQ("2015-01-02",
00101             absl::FormatCivilTime(absl::CivilDay(2015, 1, 2)));
00102   EXPECT_EQ("2015-01-01",
00103             absl::FormatCivilTime(absl::CivilDay(2015, 1)));
00104   EXPECT_EQ("2015-01-01",
00105             absl::FormatCivilTime(absl::CivilDay(2015)));
00106 
00107   EXPECT_EQ("2015-01",
00108             absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4, 5)));
00109   EXPECT_EQ("2015-01",
00110             absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4)));
00111   EXPECT_EQ("2015-01",
00112             absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3)));
00113   EXPECT_EQ("2015-01",
00114             absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2)));
00115   EXPECT_EQ("2015-01",
00116             absl::FormatCivilTime(absl::CivilMonth(2015, 1)));
00117   EXPECT_EQ("2015-01",
00118             absl::FormatCivilTime(absl::CivilMonth(2015)));
00119 
00120   EXPECT_EQ("2015",
00121             absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4, 5)));
00122   EXPECT_EQ("2015",
00123             absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4)));
00124   EXPECT_EQ("2015",
00125             absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3)));
00126   EXPECT_EQ("2015",
00127             absl::FormatCivilTime(absl::CivilYear(2015, 1, 2)));
00128   EXPECT_EQ("2015",
00129             absl::FormatCivilTime(absl::CivilYear(2015, 1)));
00130   EXPECT_EQ("2015",
00131             absl::FormatCivilTime(absl::CivilYear(2015)));
00132 }
00133 
00134 TEST(CivilTime, FieldsConstructionLimits) {
00135   const int kIntMax = std::numeric_limits<int>::max();
00136   EXPECT_EQ("2038-01-19T03:14:07",
00137             absl::FormatCivilTime(absl::CivilSecond(
00138                 1970, 1, 1, 0, 0, kIntMax)));
00139   EXPECT_EQ("6121-02-11T05:21:07",
00140             absl::FormatCivilTime(absl::CivilSecond(
00141                 1970, 1, 1, 0, kIntMax, kIntMax)));
00142   EXPECT_EQ("251104-11-20T12:21:07",
00143             absl::FormatCivilTime(absl::CivilSecond(
00144                 1970, 1, 1, kIntMax, kIntMax, kIntMax)));
00145   EXPECT_EQ("6130715-05-30T12:21:07",
00146             absl::FormatCivilTime(absl::CivilSecond(
00147                 1970, 1, kIntMax, kIntMax, kIntMax, kIntMax)));
00148   EXPECT_EQ("185087685-11-26T12:21:07",
00149             absl::FormatCivilTime(absl::CivilSecond(
00150                 1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax)));
00151 
00152   const int kIntMin = std::numeric_limits<int>::min();
00153   EXPECT_EQ("1901-12-13T20:45:52",
00154             absl::FormatCivilTime(absl::CivilSecond(
00155                 1970, 1, 1, 0, 0, kIntMin)));
00156   EXPECT_EQ("-2182-11-20T18:37:52",
00157             absl::FormatCivilTime(absl::CivilSecond(
00158                 1970, 1, 1, 0, kIntMin, kIntMin)));
00159   EXPECT_EQ("-247165-02-11T10:37:52",
00160             absl::FormatCivilTime(absl::CivilSecond(
00161                 1970, 1, 1, kIntMin, kIntMin, kIntMin)));
00162   EXPECT_EQ("-6126776-08-01T10:37:52",
00163             absl::FormatCivilTime(absl::CivilSecond(
00164                 1970, 1, kIntMin, kIntMin, kIntMin, kIntMin)));
00165   EXPECT_EQ("-185083747-10-31T10:37:52",
00166             absl::FormatCivilTime(absl::CivilSecond(
00167                 1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
00168 }
00169 
00170 TEST(CivilTime, RangeLimits) {
00171   const absl::civil_year_t kYearMax =
00172       std::numeric_limits<absl::civil_year_t>::max();
00173   EXPECT_EQ(absl::CivilYear(kYearMax),
00174             absl::CivilYear::max());
00175   EXPECT_EQ(absl::CivilMonth(kYearMax, 12),
00176             absl::CivilMonth::max());
00177   EXPECT_EQ(absl::CivilDay(kYearMax, 12, 31),
00178             absl::CivilDay::max());
00179   EXPECT_EQ(absl::CivilHour(kYearMax, 12, 31, 23),
00180             absl::CivilHour::max());
00181   EXPECT_EQ(absl::CivilMinute(kYearMax, 12, 31, 23, 59),
00182             absl::CivilMinute::max());
00183   EXPECT_EQ(absl::CivilSecond(kYearMax, 12, 31, 23, 59, 59),
00184             absl::CivilSecond::max());
00185 
00186   const absl::civil_year_t kYearMin =
00187       std::numeric_limits<absl::civil_year_t>::min();
00188   EXPECT_EQ(absl::CivilYear(kYearMin),
00189             absl::CivilYear::min());
00190   EXPECT_EQ(absl::CivilMonth(kYearMin, 1),
00191             absl::CivilMonth::min());
00192   EXPECT_EQ(absl::CivilDay(kYearMin, 1, 1),
00193             absl::CivilDay::min());
00194   EXPECT_EQ(absl::CivilHour(kYearMin, 1, 1, 0),
00195             absl::CivilHour::min());
00196   EXPECT_EQ(absl::CivilMinute(kYearMin, 1, 1, 0, 0),
00197             absl::CivilMinute::min());
00198   EXPECT_EQ(absl::CivilSecond(kYearMin, 1, 1, 0, 0, 0),
00199             absl::CivilSecond::min());
00200 }
00201 
00202 TEST(CivilTime, ImplicitCrossAlignment) {
00203   absl::CivilYear year(2015);
00204   absl::CivilMonth month = year;
00205   absl::CivilDay day = month;
00206   absl::CivilHour hour = day;
00207   absl::CivilMinute minute = hour;
00208   absl::CivilSecond second = minute;
00209 
00210   second = year;
00211   EXPECT_EQ(second, year);
00212   second = month;
00213   EXPECT_EQ(second, month);
00214   second = day;
00215   EXPECT_EQ(second, day);
00216   second = hour;
00217   EXPECT_EQ(second, hour);
00218   second = minute;
00219   EXPECT_EQ(second, minute);
00220 
00221   minute = year;
00222   EXPECT_EQ(minute, year);
00223   minute = month;
00224   EXPECT_EQ(minute, month);
00225   minute = day;
00226   EXPECT_EQ(minute, day);
00227   minute = hour;
00228   EXPECT_EQ(minute, hour);
00229 
00230   hour = year;
00231   EXPECT_EQ(hour, year);
00232   hour = month;
00233   EXPECT_EQ(hour, month);
00234   hour = day;
00235   EXPECT_EQ(hour, day);
00236 
00237   day = year;
00238   EXPECT_EQ(day, year);
00239   day = month;
00240   EXPECT_EQ(day, month);
00241 
00242   month = year;
00243   EXPECT_EQ(month, year);
00244 
00245   // Ensures unsafe conversions are not allowed.
00246   EXPECT_FALSE(
00247       (std::is_convertible<absl::CivilSecond, absl::CivilMinute>::value));
00248   EXPECT_FALSE(
00249       (std::is_convertible<absl::CivilSecond, absl::CivilHour>::value));
00250   EXPECT_FALSE(
00251       (std::is_convertible<absl::CivilSecond, absl::CivilDay>::value));
00252   EXPECT_FALSE(
00253       (std::is_convertible<absl::CivilSecond, absl::CivilMonth>::value));
00254   EXPECT_FALSE(
00255       (std::is_convertible<absl::CivilSecond, absl::CivilYear>::value));
00256 
00257   EXPECT_FALSE(
00258       (std::is_convertible<absl::CivilMinute, absl::CivilHour>::value));
00259   EXPECT_FALSE(
00260       (std::is_convertible<absl::CivilMinute, absl::CivilDay>::value));
00261   EXPECT_FALSE(
00262       (std::is_convertible<absl::CivilMinute, absl::CivilMonth>::value));
00263   EXPECT_FALSE(
00264       (std::is_convertible<absl::CivilMinute, absl::CivilYear>::value));
00265 
00266   EXPECT_FALSE(
00267       (std::is_convertible<absl::CivilHour, absl::CivilDay>::value));
00268   EXPECT_FALSE(
00269       (std::is_convertible<absl::CivilHour, absl::CivilMonth>::value));
00270   EXPECT_FALSE(
00271       (std::is_convertible<absl::CivilHour, absl::CivilYear>::value));
00272 
00273   EXPECT_FALSE(
00274       (std::is_convertible<absl::CivilDay, absl::CivilMonth>::value));
00275   EXPECT_FALSE(
00276       (std::is_convertible<absl::CivilDay, absl::CivilYear>::value));
00277 
00278   EXPECT_FALSE(
00279       (std::is_convertible<absl::CivilMonth, absl::CivilYear>::value));
00280 }
00281 
00282 TEST(CivilTime, ExplicitCrossAlignment) {
00283   //
00284   // Assign from smaller units -> larger units
00285   //
00286 
00287   absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
00288   EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second));
00289 
00290   absl::CivilMinute minute(second);
00291   EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute));
00292 
00293   absl::CivilHour hour(minute);
00294   EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour));
00295 
00296   absl::CivilDay day(hour);
00297   EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day));
00298 
00299   absl::CivilMonth month(day);
00300   EXPECT_EQ("2015-01", absl::FormatCivilTime(month));
00301 
00302   absl::CivilYear year(month);
00303   EXPECT_EQ("2015", absl::FormatCivilTime(year));
00304 
00305   //
00306   // Now assign from larger units -> smaller units
00307   //
00308 
00309   month = absl::CivilMonth(year);
00310   EXPECT_EQ("2015-01", absl::FormatCivilTime(month));
00311 
00312   day = absl::CivilDay(month);
00313   EXPECT_EQ("2015-01-01", absl::FormatCivilTime(day));
00314 
00315   hour = absl::CivilHour(day);
00316   EXPECT_EQ("2015-01-01T00", absl::FormatCivilTime(hour));
00317 
00318   minute = absl::CivilMinute(hour);
00319   EXPECT_EQ("2015-01-01T00:00", absl::FormatCivilTime(minute));
00320 
00321   second = absl::CivilSecond(minute);
00322   EXPECT_EQ("2015-01-01T00:00:00", absl::FormatCivilTime(second));
00323 }
00324 
00325 // Metafunction to test whether difference is allowed between two types.
00326 template <typename T1, typename T2>
00327 struct HasDiff {
00328   template <typename U1, typename U2>
00329   static std::false_type test(...);
00330   template <typename U1, typename U2>
00331   static std::true_type test(decltype(std::declval<U1>() - std::declval<U2>()));
00332   static constexpr bool value = decltype(test<T1, T2>(0))::value;
00333 };
00334 
00335 TEST(CivilTime, DisallowCrossAlignedDifference) {
00336   // Difference is allowed between types with the same alignment.
00337   static_assert(HasDiff<absl::CivilSecond, absl::CivilSecond>::value, "");
00338   static_assert(HasDiff<absl::CivilMinute, absl::CivilMinute>::value, "");
00339   static_assert(HasDiff<absl::CivilHour, absl::CivilHour>::value, "");
00340   static_assert(HasDiff<absl::CivilDay, absl::CivilDay>::value, "");
00341   static_assert(HasDiff<absl::CivilMonth, absl::CivilMonth>::value, "");
00342   static_assert(HasDiff<absl::CivilYear, absl::CivilYear>::value, "");
00343 
00344   // Difference is disallowed between types with different alignments.
00345   static_assert(!HasDiff<absl::CivilSecond, absl::CivilMinute>::value, "");
00346   static_assert(!HasDiff<absl::CivilSecond, absl::CivilHour>::value, "");
00347   static_assert(!HasDiff<absl::CivilSecond, absl::CivilDay>::value, "");
00348   static_assert(!HasDiff<absl::CivilSecond, absl::CivilMonth>::value, "");
00349   static_assert(!HasDiff<absl::CivilSecond, absl::CivilYear>::value, "");
00350 
00351   static_assert(!HasDiff<absl::CivilMinute, absl::CivilHour>::value, "");
00352   static_assert(!HasDiff<absl::CivilMinute, absl::CivilDay>::value, "");
00353   static_assert(!HasDiff<absl::CivilMinute, absl::CivilMonth>::value, "");
00354   static_assert(!HasDiff<absl::CivilMinute, absl::CivilYear>::value, "");
00355 
00356   static_assert(!HasDiff<absl::CivilHour, absl::CivilDay>::value, "");
00357   static_assert(!HasDiff<absl::CivilHour, absl::CivilMonth>::value, "");
00358   static_assert(!HasDiff<absl::CivilHour, absl::CivilYear>::value, "");
00359 
00360   static_assert(!HasDiff<absl::CivilDay, absl::CivilMonth>::value, "");
00361   static_assert(!HasDiff<absl::CivilDay, absl::CivilYear>::value, "");
00362 
00363   static_assert(!HasDiff<absl::CivilMonth, absl::CivilYear>::value, "");
00364 }
00365 
00366 TEST(CivilTime, ValueSemantics) {
00367   const absl::CivilHour a(2015, 1, 2, 3);
00368   const absl::CivilHour b = a;
00369   const absl::CivilHour c(b);
00370   absl::CivilHour d;
00371   d = c;
00372   EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(d));
00373 }
00374 
00375 TEST(CivilTime, Relational) {
00376   // Tests that the alignment unit is ignored in comparison.
00377   const absl::CivilYear year(2014);
00378   const absl::CivilMonth month(year);
00379   EXPECT_EQ(year, month);
00380 
00381 #define TEST_RELATIONAL(OLDER, YOUNGER) \
00382   do {                                  \
00383     EXPECT_FALSE(OLDER < OLDER);        \
00384     EXPECT_FALSE(OLDER > OLDER);        \
00385     EXPECT_TRUE(OLDER >= OLDER);        \
00386     EXPECT_TRUE(OLDER <= OLDER);        \
00387     EXPECT_FALSE(YOUNGER < YOUNGER);    \
00388     EXPECT_FALSE(YOUNGER > YOUNGER);    \
00389     EXPECT_TRUE(YOUNGER >= YOUNGER);    \
00390     EXPECT_TRUE(YOUNGER <= YOUNGER);    \
00391     EXPECT_EQ(OLDER, OLDER);            \
00392     EXPECT_NE(OLDER, YOUNGER);          \
00393     EXPECT_LT(OLDER, YOUNGER);          \
00394     EXPECT_LE(OLDER, YOUNGER);          \
00395     EXPECT_GT(YOUNGER, OLDER);          \
00396     EXPECT_GE(YOUNGER, OLDER);          \
00397   } while (0)
00398 
00399   // Alignment is ignored in comparison (verified above), so CivilSecond is
00400   // used to test comparison in all field positions.
00401   TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
00402                   absl::CivilSecond(2015, 1, 1, 0, 0, 0));
00403   TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
00404                   absl::CivilSecond(2014, 2, 1, 0, 0, 0));
00405   TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
00406                   absl::CivilSecond(2014, 1, 2, 0, 0, 0));
00407   TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
00408                   absl::CivilSecond(2014, 1, 1, 1, 0, 0));
00409   TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 0, 0),
00410                   absl::CivilSecond(2014, 1, 1, 1, 1, 0));
00411   TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 1, 0),
00412                   absl::CivilSecond(2014, 1, 1, 1, 1, 1));
00413 
00414   // Tests the relational operators of two different civil-time types.
00415   TEST_RELATIONAL(absl::CivilDay(2014, 1, 1),
00416                   absl::CivilMinute(2014, 1, 1, 1, 1));
00417   TEST_RELATIONAL(absl::CivilDay(2014, 1, 1),
00418                   absl::CivilMonth(2014, 2));
00419 
00420 #undef TEST_RELATIONAL
00421 }
00422 
00423 TEST(CivilTime, Arithmetic) {
00424   absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
00425   EXPECT_EQ("2015-01-02T03:04:06", absl::FormatCivilTime(second += 1));
00426   EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second + 1));
00427   EXPECT_EQ("2015-01-02T03:04:08", absl::FormatCivilTime(2 + second));
00428   EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second - 1));
00429   EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second -= 1));
00430   EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second++));
00431   EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(++second));
00432   EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second--));
00433   EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(--second));
00434 
00435   absl::CivilMinute minute(2015, 1, 2, 3, 4);
00436   EXPECT_EQ("2015-01-02T03:05", absl::FormatCivilTime(minute += 1));
00437   EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute + 1));
00438   EXPECT_EQ("2015-01-02T03:07", absl::FormatCivilTime(2 + minute));
00439   EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute - 1));
00440   EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute -= 1));
00441   EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute++));
00442   EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(++minute));
00443   EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute--));
00444   EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(--minute));
00445 
00446   absl::CivilHour hour(2015, 1, 2, 3);
00447   EXPECT_EQ("2015-01-02T04", absl::FormatCivilTime(hour += 1));
00448   EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour + 1));
00449   EXPECT_EQ("2015-01-02T06", absl::FormatCivilTime(2 + hour));
00450   EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour - 1));
00451   EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour -= 1));
00452   EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour++));
00453   EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(++hour));
00454   EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour--));
00455   EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(--hour));
00456 
00457   absl::CivilDay day(2015, 1, 2);
00458   EXPECT_EQ("2015-01-03", absl::FormatCivilTime(day += 1));
00459   EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day + 1));
00460   EXPECT_EQ("2015-01-05", absl::FormatCivilTime(2 + day));
00461   EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day - 1));
00462   EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day -= 1));
00463   EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day++));
00464   EXPECT_EQ("2015-01-04", absl::FormatCivilTime(++day));
00465   EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day--));
00466   EXPECT_EQ("2015-01-02", absl::FormatCivilTime(--day));
00467 
00468   absl::CivilMonth month(2015, 1);
00469   EXPECT_EQ("2015-02", absl::FormatCivilTime(month += 1));
00470   EXPECT_EQ("2015-03", absl::FormatCivilTime(month + 1));
00471   EXPECT_EQ("2015-04", absl::FormatCivilTime(2 + month));
00472   EXPECT_EQ("2015-01", absl::FormatCivilTime(month - 1));
00473   EXPECT_EQ("2015-01", absl::FormatCivilTime(month -= 1));
00474   EXPECT_EQ("2015-01", absl::FormatCivilTime(month++));
00475   EXPECT_EQ("2015-03", absl::FormatCivilTime(++month));
00476   EXPECT_EQ("2015-03", absl::FormatCivilTime(month--));
00477   EXPECT_EQ("2015-01", absl::FormatCivilTime(--month));
00478 
00479   absl::CivilYear year(2015);
00480   EXPECT_EQ("2016", absl::FormatCivilTime(year += 1));
00481   EXPECT_EQ("2017", absl::FormatCivilTime(year + 1));
00482   EXPECT_EQ("2018", absl::FormatCivilTime(2 + year));
00483   EXPECT_EQ("2015", absl::FormatCivilTime(year - 1));
00484   EXPECT_EQ("2015", absl::FormatCivilTime(year -= 1));
00485   EXPECT_EQ("2015", absl::FormatCivilTime(year++));
00486   EXPECT_EQ("2017", absl::FormatCivilTime(++year));
00487   EXPECT_EQ("2017", absl::FormatCivilTime(year--));
00488   EXPECT_EQ("2015", absl::FormatCivilTime(--year));
00489 }
00490 
00491 TEST(CivilTime, ArithmeticLimits) {
00492   const int kIntMax = std::numeric_limits<int>::max();
00493   const int kIntMin = std::numeric_limits<int>::min();
00494 
00495   absl::CivilSecond second(1970, 1, 1, 0, 0, 0);
00496   second += kIntMax;
00497   EXPECT_EQ("2038-01-19T03:14:07", absl::FormatCivilTime(second));
00498   second -= kIntMax;
00499   EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second));
00500   second += kIntMin;
00501   EXPECT_EQ("1901-12-13T20:45:52", absl::FormatCivilTime(second));
00502   second -= kIntMin;
00503   EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second));
00504 
00505   absl::CivilMinute minute(1970, 1, 1, 0, 0);
00506   minute += kIntMax;
00507   EXPECT_EQ("6053-01-23T02:07", absl::FormatCivilTime(minute));
00508   minute -= kIntMax;
00509   EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute));
00510   minute += kIntMin;
00511   EXPECT_EQ("-2114-12-08T21:52", absl::FormatCivilTime(minute));
00512   minute -= kIntMin;
00513   EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute));
00514 
00515   absl::CivilHour hour(1970, 1, 1, 0);
00516   hour += kIntMax;
00517   EXPECT_EQ("246953-10-09T07", absl::FormatCivilTime(hour));
00518   hour -= kIntMax;
00519   EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour));
00520   hour += kIntMin;
00521   EXPECT_EQ("-243014-03-24T16", absl::FormatCivilTime(hour));
00522   hour -= kIntMin;
00523   EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour));
00524 
00525   absl::CivilDay day(1970, 1, 1);
00526   day += kIntMax;
00527   EXPECT_EQ("5881580-07-11", absl::FormatCivilTime(day));
00528   day -= kIntMax;
00529   EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day));
00530   day += kIntMin;
00531   EXPECT_EQ("-5877641-06-23", absl::FormatCivilTime(day));
00532   day -= kIntMin;
00533   EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day));
00534 
00535   absl::CivilMonth month(1970, 1);
00536   month += kIntMax;
00537   EXPECT_EQ("178958940-08", absl::FormatCivilTime(month));
00538   month -= kIntMax;
00539   EXPECT_EQ("1970-01", absl::FormatCivilTime(month));
00540   month += kIntMin;
00541   EXPECT_EQ("-178955001-05", absl::FormatCivilTime(month));
00542   month -= kIntMin;
00543   EXPECT_EQ("1970-01", absl::FormatCivilTime(month));
00544 
00545   absl::CivilYear year(0);
00546   year += kIntMax;
00547   EXPECT_EQ("2147483647", absl::FormatCivilTime(year));
00548   year -= kIntMax;
00549   EXPECT_EQ("0", absl::FormatCivilTime(year));
00550   year += kIntMin;
00551   EXPECT_EQ("-2147483648", absl::FormatCivilTime(year));
00552   year -= kIntMin;
00553   EXPECT_EQ("0", absl::FormatCivilTime(year));
00554 }
00555 
00556 TEST(CivilTime, Difference) {
00557   absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
00558   EXPECT_EQ(0, second - second);
00559   EXPECT_EQ(10, (second + 10) - second);
00560   EXPECT_EQ(-10, (second - 10) - second);
00561 
00562   absl::CivilMinute minute(2015, 1, 2, 3, 4);
00563   EXPECT_EQ(0, minute - minute);
00564   EXPECT_EQ(10, (minute + 10) - minute);
00565   EXPECT_EQ(-10, (minute - 10) - minute);
00566 
00567   absl::CivilHour hour(2015, 1, 2, 3);
00568   EXPECT_EQ(0, hour - hour);
00569   EXPECT_EQ(10, (hour + 10) - hour);
00570   EXPECT_EQ(-10, (hour - 10) - hour);
00571 
00572   absl::CivilDay day(2015, 1, 2);
00573   EXPECT_EQ(0, day - day);
00574   EXPECT_EQ(10, (day + 10) - day);
00575   EXPECT_EQ(-10, (day - 10) - day);
00576 
00577   absl::CivilMonth month(2015, 1);
00578   EXPECT_EQ(0, month - month);
00579   EXPECT_EQ(10, (month + 10) - month);
00580   EXPECT_EQ(-10, (month - 10) - month);
00581 
00582   absl::CivilYear year(2015);
00583   EXPECT_EQ(0, year - year);
00584   EXPECT_EQ(10, (year + 10) - year);
00585   EXPECT_EQ(-10, (year - 10) - year);
00586 }
00587 
00588 TEST(CivilTime, DifferenceLimits) {
00589   const absl::civil_diff_t kDiffMax =
00590       std::numeric_limits<absl::civil_diff_t>::max();
00591   const absl::civil_diff_t kDiffMin =
00592       std::numeric_limits<absl::civil_diff_t>::min();
00593 
00594   // Check day arithmetic at the end of the year range.
00595   const absl::CivilDay max_day(kDiffMax, 12, 31);
00596   EXPECT_EQ(1, max_day - (max_day - 1));
00597   EXPECT_EQ(-1, (max_day - 1) - max_day);
00598 
00599   // Check day arithmetic at the start of the year range.
00600   const absl::CivilDay min_day(kDiffMin, 1, 1);
00601   EXPECT_EQ(1, (min_day + 1) - min_day);
00602   EXPECT_EQ(-1, min_day - (min_day + 1));
00603 
00604   // Check the limits of the return value.
00605   const absl::CivilDay d1(1970, 1, 1);
00606   const absl::CivilDay d2(25252734927768524, 7, 27);
00607   EXPECT_EQ(kDiffMax, d2 - d1);
00608   EXPECT_EQ(kDiffMin, d1 - (d2 + 1));
00609 }
00610 
00611 TEST(CivilTime, Properties) {
00612   absl::CivilSecond ss(2015, 2, 3, 4, 5, 6);
00613   EXPECT_EQ(2015, ss.year());
00614   EXPECT_EQ(2, ss.month());
00615   EXPECT_EQ(3, ss.day());
00616   EXPECT_EQ(4, ss.hour());
00617   EXPECT_EQ(5, ss.minute());
00618   EXPECT_EQ(6, ss.second());
00619 
00620   absl::CivilMinute mm(2015, 2, 3, 4, 5, 6);
00621   EXPECT_EQ(2015, mm.year());
00622   EXPECT_EQ(2, mm.month());
00623   EXPECT_EQ(3, mm.day());
00624   EXPECT_EQ(4, mm.hour());
00625   EXPECT_EQ(5, mm.minute());
00626   EXPECT_EQ(0, mm.second());
00627 
00628   absl::CivilHour hh(2015, 2, 3, 4, 5, 6);
00629   EXPECT_EQ(2015, hh.year());
00630   EXPECT_EQ(2, hh.month());
00631   EXPECT_EQ(3, hh.day());
00632   EXPECT_EQ(4, hh.hour());
00633   EXPECT_EQ(0, hh.minute());
00634   EXPECT_EQ(0, hh.second());
00635 
00636   absl::CivilDay d(2015, 2, 3, 4, 5, 6);
00637   EXPECT_EQ(2015, d.year());
00638   EXPECT_EQ(2, d.month());
00639   EXPECT_EQ(3, d.day());
00640   EXPECT_EQ(0, d.hour());
00641   EXPECT_EQ(0, d.minute());
00642   EXPECT_EQ(0, d.second());
00643 
00644   absl::CivilMonth m(2015, 2, 3, 4, 5, 6);
00645   EXPECT_EQ(2015, m.year());
00646   EXPECT_EQ(2, m.month());
00647   EXPECT_EQ(1, m.day());
00648   EXPECT_EQ(0, m.hour());
00649   EXPECT_EQ(0, m.minute());
00650   EXPECT_EQ(0, m.second());
00651 
00652   absl::CivilYear y(2015, 2, 3, 4, 5, 6);
00653   EXPECT_EQ(2015, y.year());
00654   EXPECT_EQ(1, y.month());
00655   EXPECT_EQ(1, y.day());
00656   EXPECT_EQ(0, y.hour());
00657   EXPECT_EQ(0, y.minute());
00658   EXPECT_EQ(0, y.second());
00659 }
00660 
00661 TEST(CivilTime, Format) {
00662   absl::CivilSecond ss;
00663   EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
00664 
00665   absl::CivilMinute mm;
00666   EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
00667 
00668   absl::CivilHour hh;
00669   EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
00670 
00671   absl::CivilDay d;
00672   EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
00673 
00674   absl::CivilMonth m;
00675   EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
00676 
00677   absl::CivilYear y;
00678   EXPECT_EQ("1970", absl::FormatCivilTime(y));
00679 }
00680 
00681 TEST(CivilTime, FormatAndParseLenient) {
00682   absl::CivilSecond ss;
00683   EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
00684 
00685   absl::CivilMinute mm;
00686   EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
00687 
00688   absl::CivilHour hh;
00689   EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
00690 
00691   absl::CivilDay d;
00692   EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
00693 
00694   absl::CivilMonth m;
00695   EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
00696 
00697   absl::CivilYear y;
00698   EXPECT_EQ("1970", absl::FormatCivilTime(y));
00699 }
00700 
00701 TEST(CivilTime, OutputStream) {
00702   absl::CivilSecond cs(2016, 2, 3, 4, 5, 6);
00703   {
00704     std::stringstream ss;
00705     ss << std::left << std::setfill('.');
00706     ss << std::setw(3) << 'X';
00707     ss << std::setw(21) << absl::CivilYear(cs);
00708     ss << std::setw(3) << 'X';
00709     EXPECT_EQ("X..2016.................X..", ss.str());
00710   }
00711   {
00712     std::stringstream ss;
00713     ss << std::left << std::setfill('.');
00714     ss << std::setw(3) << 'X';
00715     ss << std::setw(21) << absl::CivilMonth(cs);
00716     ss << std::setw(3) << 'X';
00717     EXPECT_EQ("X..2016-02..............X..", ss.str());
00718   }
00719   {
00720     std::stringstream ss;
00721     ss << std::left << std::setfill('.');
00722     ss << std::setw(3) << 'X';
00723     ss << std::setw(21) << absl::CivilDay(cs);
00724     ss << std::setw(3) << 'X';
00725     EXPECT_EQ("X..2016-02-03...........X..", ss.str());
00726   }
00727   {
00728     std::stringstream ss;
00729     ss << std::left << std::setfill('.');
00730     ss << std::setw(3) << 'X';
00731     ss << std::setw(21) << absl::CivilHour(cs);
00732     ss << std::setw(3) << 'X';
00733     EXPECT_EQ("X..2016-02-03T04........X..", ss.str());
00734   }
00735   {
00736     std::stringstream ss;
00737     ss << std::left << std::setfill('.');
00738     ss << std::setw(3) << 'X';
00739     ss << std::setw(21) << absl::CivilMinute(cs);
00740     ss << std::setw(3) << 'X';
00741     EXPECT_EQ("X..2016-02-03T04:05.....X..", ss.str());
00742   }
00743   {
00744     std::stringstream ss;
00745     ss << std::left << std::setfill('.');
00746     ss << std::setw(3) << 'X';
00747     ss << std::setw(21) << absl::CivilSecond(cs);
00748     ss << std::setw(3) << 'X';
00749     EXPECT_EQ("X..2016-02-03T04:05:06..X..", ss.str());
00750   }
00751   {
00752     std::stringstream ss;
00753     ss << std::left << std::setfill('.');
00754     ss << std::setw(3) << 'X';
00755     ss << std::setw(21) << absl::Weekday::wednesday;
00756     ss << std::setw(3) << 'X';
00757     EXPECT_EQ("X..Wednesday............X..", ss.str());
00758   }
00759 }
00760 
00761 TEST(CivilTime, Weekday) {
00762   absl::CivilDay d(1970, 1, 1);
00763   EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(d)) << d;
00764 
00765   // We used to get this wrong for years < -30.
00766   d = absl::CivilDay(-31, 12, 24);
00767   EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(d)) << d;
00768 }
00769 
00770 TEST(CivilTime, NextPrevWeekday) {
00771   // Jan 1, 1970 was a Thursday.
00772   const absl::CivilDay thursday(1970, 1, 1);
00773 
00774   // Thursday -> Thursday
00775   absl::CivilDay d = absl::NextWeekday(thursday, absl::Weekday::thursday);
00776   EXPECT_EQ(7, d - thursday) << d;
00777   EXPECT_EQ(d - 14, absl::PrevWeekday(thursday, absl::Weekday::thursday));
00778 
00779   // Thursday -> Friday
00780   d = absl::NextWeekday(thursday, absl::Weekday::friday);
00781   EXPECT_EQ(1, d - thursday) << d;
00782   EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::friday));
00783 
00784   // Thursday -> Saturday
00785   d = absl::NextWeekday(thursday, absl::Weekday::saturday);
00786   EXPECT_EQ(2, d - thursday) << d;
00787   EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::saturday));
00788 
00789   // Thursday -> Sunday
00790   d = absl::NextWeekday(thursday, absl::Weekday::sunday);
00791   EXPECT_EQ(3, d - thursday) << d;
00792   EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::sunday));
00793 
00794   // Thursday -> Monday
00795   d = absl::NextWeekday(thursday, absl::Weekday::monday);
00796   EXPECT_EQ(4, d - thursday) << d;
00797   EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::monday));
00798 
00799   // Thursday -> Tuesday
00800   d = absl::NextWeekday(thursday, absl::Weekday::tuesday);
00801   EXPECT_EQ(5, d - thursday) << d;
00802   EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::tuesday));
00803 
00804   // Thursday -> Wednesday
00805   d = absl::NextWeekday(thursday, absl::Weekday::wednesday);
00806   EXPECT_EQ(6, d - thursday) << d;
00807   EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::wednesday));
00808 }
00809 
00810 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
00811 TEST(CivilTime, DifferenceWithHugeYear) {
00812   absl::CivilDay d1(9223372036854775807, 1, 1);
00813   absl::CivilDay d2(9223372036854775807, 12, 31);
00814   EXPECT_EQ(364, d2 - d1);
00815 
00816   d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1);
00817   d2 = absl::CivilDay(-9223372036854775807 - 1, 12, 31);
00818   EXPECT_EQ(365, d2 - d1);
00819 
00820   // Check the limits of the return value at the end of the year range.
00821   d1 = absl::CivilDay(9223372036854775807, 1, 1);
00822   d2 = absl::CivilDay(9198119301927009252, 6, 6);
00823   EXPECT_EQ(9223372036854775807, d1 - d2);
00824   d2 = d2 - 1;
00825   EXPECT_EQ(-9223372036854775807 - 1, d2 - d1);
00826 
00827   // Check the limits of the return value at the start of the year range.
00828   d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1);
00829   d2 = absl::CivilDay(-9198119301927009254, 7, 28);
00830   EXPECT_EQ(9223372036854775807, d2 - d1);
00831   d2 = d2 + 1;
00832   EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
00833 
00834   // Check the limits of the return value from either side of year 0.
00835   d1 = absl::CivilDay(-12626367463883278, 9, 3);
00836   d2 = absl::CivilDay(12626367463883277, 3, 28);
00837   EXPECT_EQ(9223372036854775807, d2 - d1);
00838   d2 = d2 + 1;
00839   EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
00840 }
00841 
00842 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
00843 TEST(CivilTime, DifferenceNoIntermediateOverflow) {
00844   // The difference up to the minute field would be below the minimum
00845   // int64_t, but the 52 extra seconds brings us back to the minimum.
00846   absl::CivilSecond s1(-292277022657, 1, 27, 8, 29 - 1, 52);
00847   absl::CivilSecond s2(1970, 1, 1, 0, 0 - 1, 0);
00848   EXPECT_EQ(-9223372036854775807 - 1, s1 - s2);
00849 
00850   // The difference up to the minute field would be above the maximum
00851   // int64_t, but the -53 extra seconds brings us back to the maximum.
00852   s1 = absl::CivilSecond(292277026596, 12, 4, 15, 30, 7 - 7);
00853   s2 = absl::CivilSecond(1970, 1, 1, 0, 0, 0 - 7);
00854   EXPECT_EQ(9223372036854775807, s1 - s2);
00855 }
00856 
00857 TEST(CivilTime, NormalizeSimpleOverflow) {
00858   absl::CivilSecond cs;
00859   cs = absl::CivilSecond(2013, 11, 15, 16, 32, 59 + 1);
00860   EXPECT_EQ("2013-11-15T16:33:00", absl::FormatCivilTime(cs));
00861   cs = absl::CivilSecond(2013, 11, 15, 16, 59 + 1, 14);
00862   EXPECT_EQ("2013-11-15T17:00:14", absl::FormatCivilTime(cs));
00863   cs = absl::CivilSecond(2013, 11, 15, 23 + 1, 32, 14);
00864   EXPECT_EQ("2013-11-16T00:32:14", absl::FormatCivilTime(cs));
00865   cs = absl::CivilSecond(2013, 11, 30 + 1, 16, 32, 14);
00866   EXPECT_EQ("2013-12-01T16:32:14", absl::FormatCivilTime(cs));
00867   cs = absl::CivilSecond(2013, 12 + 1, 15, 16, 32, 14);
00868   EXPECT_EQ("2014-01-15T16:32:14", absl::FormatCivilTime(cs));
00869 }
00870 
00871 TEST(CivilTime, NormalizeSimpleUnderflow) {
00872   absl::CivilSecond cs;
00873   cs = absl::CivilSecond(2013, 11, 15, 16, 32, 0 - 1);
00874   EXPECT_EQ("2013-11-15T16:31:59", absl::FormatCivilTime(cs));
00875   cs = absl::CivilSecond(2013, 11, 15, 16, 0 - 1, 14);
00876   EXPECT_EQ("2013-11-15T15:59:14", absl::FormatCivilTime(cs));
00877   cs = absl::CivilSecond(2013, 11, 15, 0 - 1, 32, 14);
00878   EXPECT_EQ("2013-11-14T23:32:14", absl::FormatCivilTime(cs));
00879   cs = absl::CivilSecond(2013, 11, 1 - 1, 16, 32, 14);
00880   EXPECT_EQ("2013-10-31T16:32:14", absl::FormatCivilTime(cs));
00881   cs = absl::CivilSecond(2013, 1 - 1, 15, 16, 32, 14);
00882   EXPECT_EQ("2012-12-15T16:32:14", absl::FormatCivilTime(cs));
00883 }
00884 
00885 TEST(CivilTime, NormalizeMultipleOverflow) {
00886   absl::CivilSecond cs(2013, 12, 31, 23, 59, 59 + 1);
00887   EXPECT_EQ("2014-01-01T00:00:00", absl::FormatCivilTime(cs));
00888 }
00889 
00890 TEST(CivilTime, NormalizeMultipleUnderflow) {
00891   absl::CivilSecond cs(2014, 1, 1, 0, 0, 0 - 1);
00892   EXPECT_EQ("2013-12-31T23:59:59", absl::FormatCivilTime(cs));
00893 }
00894 
00895 TEST(CivilTime, NormalizeOverflowLimits) {
00896   absl::CivilSecond cs;
00897 
00898   const int kintmax = std::numeric_limits<int>::max();
00899   cs = absl::CivilSecond(0, kintmax, kintmax, kintmax, kintmax, kintmax);
00900   EXPECT_EQ("185085715-11-27T12:21:07", absl::FormatCivilTime(cs));
00901 
00902   const int kintmin = std::numeric_limits<int>::min();
00903   cs = absl::CivilSecond(0, kintmin, kintmin, kintmin, kintmin, kintmin);
00904   EXPECT_EQ("-185085717-10-31T10:37:52", absl::FormatCivilTime(cs));
00905 }
00906 
00907 TEST(CivilTime, NormalizeComplexOverflow) {
00908   absl::CivilSecond cs;
00909   cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 + 123456789);
00910   EXPECT_EQ("2017-10-14T14:05:23", absl::FormatCivilTime(cs));
00911   cs = absl::CivilSecond(2013, 11, 15, 16, 32 + 1234567, 14);
00912   EXPECT_EQ("2016-03-22T00:39:14", absl::FormatCivilTime(cs));
00913   cs = absl::CivilSecond(2013, 11, 15, 16 + 123456, 32, 14);
00914   EXPECT_EQ("2027-12-16T16:32:14", absl::FormatCivilTime(cs));
00915   cs = absl::CivilSecond(2013, 11, 15 + 1234, 16, 32, 14);
00916   EXPECT_EQ("2017-04-02T16:32:14", absl::FormatCivilTime(cs));
00917   cs = absl::CivilSecond(2013, 11 + 123, 15, 16, 32, 14);
00918   EXPECT_EQ("2024-02-15T16:32:14", absl::FormatCivilTime(cs));
00919 }
00920 
00921 TEST(CivilTime, NormalizeComplexUnderflow) {
00922   absl::CivilSecond cs;
00923   cs = absl::CivilSecond(1999, 3, 0, 0, 0, 0);  // year 400
00924   EXPECT_EQ("1999-02-28T00:00:00", absl::FormatCivilTime(cs));
00925   cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 - 123456789);
00926   EXPECT_EQ("2009-12-17T18:59:05", absl::FormatCivilTime(cs));
00927   cs = absl::CivilSecond(2013, 11, 15, 16, 32 - 1234567, 14);
00928   EXPECT_EQ("2011-07-12T08:25:14", absl::FormatCivilTime(cs));
00929   cs = absl::CivilSecond(2013, 11, 15, 16 - 123456, 32, 14);
00930   EXPECT_EQ("1999-10-16T16:32:14", absl::FormatCivilTime(cs));
00931   cs = absl::CivilSecond(2013, 11, 15 - 1234, 16, 32, 14);
00932   EXPECT_EQ("2010-06-30T16:32:14", absl::FormatCivilTime(cs));
00933   cs = absl::CivilSecond(2013, 11 - 123, 15, 16, 32, 14);
00934   EXPECT_EQ("2003-08-15T16:32:14", absl::FormatCivilTime(cs));
00935 }
00936 
00937 TEST(CivilTime, NormalizeMishmash) {
00938   absl::CivilSecond cs;
00939   cs = absl::CivilSecond(2013, 11 - 123, 15 + 1234, 16 - 123456, 32 + 1234567,
00940                          14 - 123456789);
00941   EXPECT_EQ("1991-05-09T03:06:05", absl::FormatCivilTime(cs));
00942   cs = absl::CivilSecond(2013, 11 + 123, 15 - 1234, 16 + 123456, 32 - 1234567,
00943                          14 + 123456789);
00944   EXPECT_EQ("2036-05-24T05:58:23", absl::FormatCivilTime(cs));
00945 
00946   cs = absl::CivilSecond(2013, 11, -146097 + 1, 16, 32, 14);
00947   EXPECT_EQ("1613-11-01T16:32:14", absl::FormatCivilTime(cs));
00948   cs = absl::CivilSecond(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14);
00949   EXPECT_EQ("2013-11-01T16:32:14", absl::FormatCivilTime(cs));
00950 }
00951 
00952 // Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31)
00953 // and check that they normalize to the expected time.  146097 days span
00954 // the 400-year Gregorian cycle used during normalization.
00955 TEST(CivilTime, NormalizeAllTheDays) {
00956   absl::CivilDay expected(1970, 1, 1);
00957   for (int day = 1; day <= 146097; ++day) {
00958     absl::CivilSecond cs(1970, 1, day, 0, 0, 0);
00959     EXPECT_EQ(expected, cs);
00960     ++expected;
00961   }
00962 }
00963 
00964 TEST(CivilTime, NormalizeWithHugeYear) {
00965   absl::CivilMonth c(9223372036854775807, 1);
00966   EXPECT_EQ("9223372036854775807-01", absl::FormatCivilTime(c));
00967   c = c - 1;  // Causes normalization
00968   EXPECT_EQ("9223372036854775806-12", absl::FormatCivilTime(c));
00969 
00970   c = absl::CivilMonth(-9223372036854775807 - 1, 1);
00971   EXPECT_EQ("-9223372036854775808-01", absl::FormatCivilTime(c));
00972   c = c + 12;  // Causes normalization
00973   EXPECT_EQ("-9223372036854775807-01", absl::FormatCivilTime(c));
00974 }
00975 
00976 TEST(CivilTime, LeapYears) {
00977   const absl::CivilSecond s1(2013, 2, 28 + 1, 0, 0, 0);
00978   EXPECT_EQ("2013-03-01T00:00:00", absl::FormatCivilTime(s1));
00979 
00980   const absl::CivilSecond s2(2012, 2, 28 + 1, 0, 0, 0);
00981   EXPECT_EQ("2012-02-29T00:00:00", absl::FormatCivilTime(s2));
00982 
00983   const absl::CivilSecond s3(1900, 2, 28 + 1, 0, 0, 0);
00984   EXPECT_EQ("1900-03-01T00:00:00", absl::FormatCivilTime(s3));
00985 
00986   const struct {
00987     int year;
00988     int days;
00989     struct {
00990       int month;
00991       int day;
00992     } leap_day;  // The date of the day after Feb 28.
00993   } kLeapYearTable[]{
00994       {1900, 365, {3, 1}},
00995       {1999, 365, {3, 1}},
00996       {2000, 366, {2, 29}},  // leap year
00997       {2001, 365, {3, 1}},
00998       {2002, 365, {3, 1}},
00999       {2003, 365, {3, 1}},
01000       {2004, 366, {2, 29}},  // leap year
01001       {2005, 365, {3, 1}},
01002       {2006, 365, {3, 1}},
01003       {2007, 365, {3, 1}},
01004       {2008, 366, {2, 29}},  // leap year
01005       {2009, 365, {3, 1}},
01006       {2100, 365, {3, 1}},
01007   };
01008 
01009   for (int i = 0; i < ABSL_ARRAYSIZE(kLeapYearTable); ++i) {
01010     const int y = kLeapYearTable[i].year;
01011     const int m = kLeapYearTable[i].leap_day.month;
01012     const int d = kLeapYearTable[i].leap_day.day;
01013     const int n = kLeapYearTable[i].days;
01014 
01015     // Tests incrementing through the leap day.
01016     const absl::CivilDay feb28(y, 2, 28);
01017     const absl::CivilDay next_day = feb28 + 1;
01018     EXPECT_EQ(m, next_day.month());
01019     EXPECT_EQ(d, next_day.day());
01020 
01021     // Tests difference in days of leap years.
01022     const absl::CivilYear year(feb28);
01023     const absl::CivilYear next_year = year + 1;
01024     EXPECT_EQ(n, absl::CivilDay(next_year) - absl::CivilDay(year));
01025   }
01026 }
01027 
01028 TEST(CivilTime, FirstThursdayInMonth) {
01029   const absl::CivilDay nov1(2014, 11, 1);
01030   const absl::CivilDay thursday =
01031       absl::PrevWeekday(nov1, absl::Weekday::thursday) + 7;
01032   EXPECT_EQ("2014-11-06", absl::FormatCivilTime(thursday));
01033 
01034   // Bonus: Date of Thanksgiving in the United States
01035   // Rule: Fourth Thursday of November
01036   const absl::CivilDay thanksgiving = thursday +  7 * 3;
01037   EXPECT_EQ("2014-11-27", absl::FormatCivilTime(thanksgiving));
01038 }
01039 
01040 TEST(CivilTime, DocumentationExample) {
01041   absl::CivilSecond second(2015, 6, 28, 1, 2, 3);  // 2015-06-28 01:02:03
01042   absl::CivilMinute minute(second);                // 2015-06-28 01:02:00
01043   absl::CivilDay day(minute);                      // 2015-06-28 00:00:00
01044 
01045   second -= 1;                    // 2015-06-28 01:02:02
01046   --second;                       // 2015-06-28 01:02:01
01047   EXPECT_EQ(minute, second - 1);  // Comparison between types
01048   EXPECT_LT(minute, second);
01049 
01050   // int diff = second - minute;  // ERROR: Mixed types, won't compile
01051 
01052   absl::CivilDay june_1(2015, 6, 1);  // Pass fields to c'tor.
01053   int diff = day - june_1;            // Num days between 'day' and June 1
01054   EXPECT_EQ(27, diff);
01055 
01056   // Fields smaller than alignment are floored to their minimum value.
01057   absl::CivilDay day_floor(2015, 1, 2, 9, 9, 9);
01058   EXPECT_EQ(0, day_floor.hour());  // 09:09:09 is floored
01059   EXPECT_EQ(absl::CivilDay(2015, 1, 2), day_floor);
01060 
01061   // Unspecified fields default to their minium value
01062   absl::CivilDay day_default(2015);  // Defaults to Jan 1
01063   EXPECT_EQ(absl::CivilDay(2015, 1, 1), day_default);
01064 
01065   // Iterates all the days of June.
01066   absl::CivilMonth june(day);  // CivilDay -> CivilMonth
01067   absl::CivilMonth july = june + 1;
01068   for (absl::CivilDay day = june_1; day < july; ++day) {
01069     // ...
01070   }
01071 }
01072 
01073 }  // namespace


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