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


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