1 // Copyright 2016 Google Inc. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //   https://www.apache.org/licenses/LICENSE-2.0
8 //
9 //   Unless required by applicable law or agreed to in writing, software
10 //   distributed under the License is distributed on an "AS IS" BASIS,
11 //   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 //   See the License for the specific language governing permissions and
13 //   limitations under the License.
14 
15 #include "absl/time/internal/cctz/include/cctz/civil_time.h"
16 
17 #include <iomanip>
18 #include <limits>
19 #include <sstream>
20 #include <string>
21 #include <type_traits>
22 
23 #include "gtest/gtest.h"
24 #include "absl/base/config.h"
25 
26 namespace absl {
27 ABSL_NAMESPACE_BEGIN
28 namespace time_internal {
29 namespace cctz {
30 
31 namespace {
32 
33 template <typename T>
Format(const T & t)34 std::string Format(const T& t) {
35   std::stringstream ss;
36   ss << t;
37   return ss.str();
38 }
39 
40 }  // namespace
41 
42 #if __cpp_constexpr >= 201304 || (defined(_MSC_VER) && _MSC_VER >= 1910)
43 // Construction constexpr tests
44 
TEST(CivilTime,Normal)45 TEST(CivilTime, Normal) {
46   constexpr civil_second css(2016, 1, 28, 17, 14, 12);
47   static_assert(css.second() == 12, "Normal.second");
48   constexpr civil_minute cmm(2016, 1, 28, 17, 14);
49   static_assert(cmm.minute() == 14, "Normal.minute");
50   constexpr civil_hour chh(2016, 1, 28, 17);
51   static_assert(chh.hour() == 17, "Normal.hour");
52   constexpr civil_day cd(2016, 1, 28);
53   static_assert(cd.day() == 28, "Normal.day");
54   constexpr civil_month cm(2016, 1);
55   static_assert(cm.month() == 1, "Normal.month");
56   constexpr civil_year cy(2016);
57   static_assert(cy.year() == 2016, "Normal.year");
58 }
59 
TEST(CivilTime,Conversion)60 TEST(CivilTime, Conversion) {
61   constexpr civil_year cy(2016);
62   static_assert(cy.year() == 2016, "Conversion.year");
63   constexpr civil_month cm(cy);
64   static_assert(cm.month() == 1, "Conversion.month");
65   constexpr civil_day cd(cm);
66   static_assert(cd.day() == 1, "Conversion.day");
67   constexpr civil_hour chh(cd);
68   static_assert(chh.hour() == 0, "Conversion.hour");
69   constexpr civil_minute cmm(chh);
70   static_assert(cmm.minute() == 0, "Conversion.minute");
71   constexpr civil_second css(cmm);
72   static_assert(css.second() == 0, "Conversion.second");
73 }
74 
75 // Normalization constexpr tests
76 
TEST(CivilTime,Normalized)77 TEST(CivilTime, Normalized) {
78   constexpr civil_second cs(2016, 1, 28, 17, 14, 12);
79   static_assert(cs.year() == 2016, "Normalized.year");
80   static_assert(cs.month() == 1, "Normalized.month");
81   static_assert(cs.day() == 28, "Normalized.day");
82   static_assert(cs.hour() == 17, "Normalized.hour");
83   static_assert(cs.minute() == 14, "Normalized.minute");
84   static_assert(cs.second() == 12, "Normalized.second");
85 }
86 
TEST(CivilTime,SecondOverflow)87 TEST(CivilTime, SecondOverflow) {
88   constexpr civil_second cs(2016, 1, 28, 17, 14, 121);
89   static_assert(cs.year() == 2016, "SecondOverflow.year");
90   static_assert(cs.month() == 1, "SecondOverflow.month");
91   static_assert(cs.day() == 28, "SecondOverflow.day");
92   static_assert(cs.hour() == 17, "SecondOverflow.hour");
93   static_assert(cs.minute() == 16, "SecondOverflow.minute");
94   static_assert(cs.second() == 1, "SecondOverflow.second");
95 }
96 
TEST(CivilTime,SecondUnderflow)97 TEST(CivilTime, SecondUnderflow) {
98   constexpr civil_second cs(2016, 1, 28, 17, 14, -121);
99   static_assert(cs.year() == 2016, "SecondUnderflow.year");
100   static_assert(cs.month() == 1, "SecondUnderflow.month");
101   static_assert(cs.day() == 28, "SecondUnderflow.day");
102   static_assert(cs.hour() == 17, "SecondUnderflow.hour");
103   static_assert(cs.minute() == 11, "SecondUnderflow.minute");
104   static_assert(cs.second() == 59, "SecondUnderflow.second");
105 }
106 
TEST(CivilTime,MinuteOverflow)107 TEST(CivilTime, MinuteOverflow) {
108   constexpr civil_second cs(2016, 1, 28, 17, 121, 12);
109   static_assert(cs.year() == 2016, "MinuteOverflow.year");
110   static_assert(cs.month() == 1, "MinuteOverflow.month");
111   static_assert(cs.day() == 28, "MinuteOverflow.day");
112   static_assert(cs.hour() == 19, "MinuteOverflow.hour");
113   static_assert(cs.minute() == 1, "MinuteOverflow.minute");
114   static_assert(cs.second() == 12, "MinuteOverflow.second");
115 }
116 
TEST(CivilTime,MinuteUnderflow)117 TEST(CivilTime, MinuteUnderflow) {
118   constexpr civil_second cs(2016, 1, 28, 17, -121, 12);
119   static_assert(cs.year() == 2016, "MinuteUnderflow.year");
120   static_assert(cs.month() == 1, "MinuteUnderflow.month");
121   static_assert(cs.day() == 28, "MinuteUnderflow.day");
122   static_assert(cs.hour() == 14, "MinuteUnderflow.hour");
123   static_assert(cs.minute() == 59, "MinuteUnderflow.minute");
124   static_assert(cs.second() == 12, "MinuteUnderflow.second");
125 }
126 
TEST(CivilTime,HourOverflow)127 TEST(CivilTime, HourOverflow) {
128   constexpr civil_second cs(2016, 1, 28, 49, 14, 12);
129   static_assert(cs.year() == 2016, "HourOverflow.year");
130   static_assert(cs.month() == 1, "HourOverflow.month");
131   static_assert(cs.day() == 30, "HourOverflow.day");
132   static_assert(cs.hour() == 1, "HourOverflow.hour");
133   static_assert(cs.minute() == 14, "HourOverflow.minute");
134   static_assert(cs.second() == 12, "HourOverflow.second");
135 }
136 
TEST(CivilTime,HourUnderflow)137 TEST(CivilTime, HourUnderflow) {
138   constexpr civil_second cs(2016, 1, 28, -49, 14, 12);
139   static_assert(cs.year() == 2016, "HourUnderflow.year");
140   static_assert(cs.month() == 1, "HourUnderflow.month");
141   static_assert(cs.day() == 25, "HourUnderflow.day");
142   static_assert(cs.hour() == 23, "HourUnderflow.hour");
143   static_assert(cs.minute() == 14, "HourUnderflow.minute");
144   static_assert(cs.second() == 12, "HourUnderflow.second");
145 }
146 
TEST(CivilTime,MonthOverflow)147 TEST(CivilTime, MonthOverflow) {
148   constexpr civil_second cs(2016, 25, 28, 17, 14, 12);
149   static_assert(cs.year() == 2018, "MonthOverflow.year");
150   static_assert(cs.month() == 1, "MonthOverflow.month");
151   static_assert(cs.day() == 28, "MonthOverflow.day");
152   static_assert(cs.hour() == 17, "MonthOverflow.hour");
153   static_assert(cs.minute() == 14, "MonthOverflow.minute");
154   static_assert(cs.second() == 12, "MonthOverflow.second");
155 }
156 
TEST(CivilTime,MonthUnderflow)157 TEST(CivilTime, MonthUnderflow) {
158   constexpr civil_second cs(2016, -25, 28, 17, 14, 12);
159   static_assert(cs.year() == 2013, "MonthUnderflow.year");
160   static_assert(cs.month() == 11, "MonthUnderflow.month");
161   static_assert(cs.day() == 28, "MonthUnderflow.day");
162   static_assert(cs.hour() == 17, "MonthUnderflow.hour");
163   static_assert(cs.minute() == 14, "MonthUnderflow.minute");
164   static_assert(cs.second() == 12, "MonthUnderflow.second");
165 }
166 
TEST(CivilTime,C4Overflow)167 TEST(CivilTime, C4Overflow) {
168   constexpr civil_second cs(2016, 1, 292195, 17, 14, 12);
169   static_assert(cs.year() == 2816, "C4Overflow.year");
170   static_assert(cs.month() == 1, "C4Overflow.month");
171   static_assert(cs.day() == 1, "C4Overflow.day");
172   static_assert(cs.hour() == 17, "C4Overflow.hour");
173   static_assert(cs.minute() == 14, "C4Overflow.minute");
174   static_assert(cs.second() == 12, "C4Overflow.second");
175 }
176 
TEST(CivilTime,C4Underflow)177 TEST(CivilTime, C4Underflow) {
178   constexpr civil_second cs(2016, 1, -292195, 17, 14, 12);
179   static_assert(cs.year() == 1215, "C4Underflow.year");
180   static_assert(cs.month() == 12, "C4Underflow.month");
181   static_assert(cs.day() == 30, "C4Underflow.day");
182   static_assert(cs.hour() == 17, "C4Underflow.hour");
183   static_assert(cs.minute() == 14, "C4Underflow.minute");
184   static_assert(cs.second() == 12, "C4Underflow.second");
185 }
186 
TEST(CivilTime,MixedNormalization)187 TEST(CivilTime, MixedNormalization) {
188   constexpr civil_second cs(2016, -42, 122, 99, -147, 4949);
189   static_assert(cs.year() == 2012, "MixedNormalization.year");
190   static_assert(cs.month() == 10, "MixedNormalization.month");
191   static_assert(cs.day() == 4, "MixedNormalization.day");
192   static_assert(cs.hour() == 1, "MixedNormalization.hour");
193   static_assert(cs.minute() == 55, "MixedNormalization.minute");
194   static_assert(cs.second() == 29, "MixedNormalization.second");
195 }
196 
197 // Relational constexpr tests
198 
TEST(CivilTime,Less)199 TEST(CivilTime, Less) {
200   constexpr civil_second cs1(2016, 1, 28, 17, 14, 12);
201   constexpr civil_second cs2(2016, 1, 28, 17, 14, 13);
202   constexpr bool less = cs1 < cs2;
203   static_assert(less, "Less");
204 }
205 
206 // Arithmetic constexpr tests
207 
TEST(CivilTime,Addition)208 TEST(CivilTime, Addition) {
209   constexpr civil_second cs1(2016, 1, 28, 17, 14, 12);
210   constexpr civil_second cs2 = cs1 + 50;
211   static_assert(cs2.year() == 2016, "Addition.year");
212   static_assert(cs2.month() == 1, "Addition.month");
213   static_assert(cs2.day() == 28, "Addition.day");
214   static_assert(cs2.hour() == 17, "Addition.hour");
215   static_assert(cs2.minute() == 15, "Addition.minute");
216   static_assert(cs2.second() == 2, "Addition.second");
217 }
218 
TEST(CivilTime,Subtraction)219 TEST(CivilTime, Subtraction) {
220   constexpr civil_second cs1(2016, 1, 28, 17, 14, 12);
221   constexpr civil_second cs2 = cs1 - 50;
222   static_assert(cs2.year() == 2016, "Subtraction.year");
223   static_assert(cs2.month() == 1, "Subtraction.month");
224   static_assert(cs2.day() == 28, "Subtraction.day");
225   static_assert(cs2.hour() == 17, "Subtraction.hour");
226   static_assert(cs2.minute() == 13, "Subtraction.minute");
227   static_assert(cs2.second() == 22, "Subtraction.second");
228 }
229 
TEST(CivilTime,Difference)230 TEST(CivilTime, Difference) {
231   constexpr civil_day cd1(2016, 1, 28);
232   constexpr civil_day cd2(2015, 1, 28);
233   constexpr int diff = cd1 - cd2;
234   static_assert(diff == 365, "Difference");
235 }
236 
237 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
TEST(CivilTime,ConstructionWithHugeYear)238 TEST(CivilTime, ConstructionWithHugeYear) {
239   constexpr civil_hour h(-9223372036854775807, 1, 1, -1);
240   static_assert(h.year() == -9223372036854775807 - 1,
241                 "ConstructionWithHugeYear");
242   static_assert(h.month() == 12, "ConstructionWithHugeYear");
243   static_assert(h.day() == 31, "ConstructionWithHugeYear");
244   static_assert(h.hour() == 23, "ConstructionWithHugeYear");
245 }
246 
247 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
TEST(CivilTime,DifferenceWithHugeYear)248 TEST(CivilTime, DifferenceWithHugeYear) {
249   {
250     constexpr civil_day d1(9223372036854775807, 1, 1);
251     constexpr civil_day d2(9223372036854775807, 12, 31);
252     static_assert(d2 - d1 == 364, "DifferenceWithHugeYear");
253   }
254   {
255     constexpr civil_day d1(-9223372036854775807 - 1, 1, 1);
256     constexpr civil_day d2(-9223372036854775807 - 1, 12, 31);
257     static_assert(d2 - d1 == 365, "DifferenceWithHugeYear");
258   }
259   {
260     // Check the limits of the return value at the end of the year range.
261     constexpr civil_day d1(9223372036854775807, 1, 1);
262     constexpr civil_day d2(9198119301927009252, 6, 6);
263     static_assert(d1 - d2 == 9223372036854775807, "DifferenceWithHugeYear");
264     static_assert((d2 - 1) - d1 == -9223372036854775807 - 1,
265                   "DifferenceWithHugeYear");
266   }
267   {
268     // Check the limits of the return value at the start of the year range.
269     constexpr civil_day d1(-9223372036854775807 - 1, 1, 1);
270     constexpr civil_day d2(-9198119301927009254, 7, 28);
271     static_assert(d2 - d1 == 9223372036854775807, "DifferenceWithHugeYear");
272     static_assert(d1 - (d2 + 1) == -9223372036854775807 - 1,
273                   "DifferenceWithHugeYear");
274   }
275   {
276     // Check the limits of the return value from either side of year 0.
277     constexpr civil_day d1(-12626367463883278, 9, 3);
278     constexpr civil_day d2(12626367463883277, 3, 28);
279     static_assert(d2 - d1 == 9223372036854775807, "DifferenceWithHugeYear");
280     static_assert(d1 - (d2 + 1) == -9223372036854775807 - 1,
281                   "DifferenceWithHugeYear");
282   }
283 }
284 
285 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
TEST(CivilTime,DifferenceNoIntermediateOverflow)286 TEST(CivilTime, DifferenceNoIntermediateOverflow) {
287   {
288     // The difference up to the minute field would be below the minimum
289     // diff_t, but the 52 extra seconds brings us back to the minimum.
290     constexpr civil_second s1(-292277022657, 1, 27, 8, 29 - 1, 52);
291     constexpr civil_second s2(1970, 1, 1, 0, 0 - 1, 0);
292     static_assert(s1 - s2 == -9223372036854775807 - 1,
293                   "DifferenceNoIntermediateOverflow");
294   }
295   {
296     // The difference up to the minute field would be above the maximum
297     // diff_t, but the -53 extra seconds brings us back to the maximum.
298     constexpr civil_second s1(292277026596, 12, 4, 15, 30, 7 - 7);
299     constexpr civil_second s2(1970, 1, 1, 0, 0, 0 - 7);
300     static_assert(s1 - s2 == 9223372036854775807,
301                   "DifferenceNoIntermediateOverflow");
302   }
303 }
304 
305 // Helper constexpr tests
306 
TEST(CivilTime,WeekDay)307 TEST(CivilTime, WeekDay) {
308   constexpr civil_day cd(2016, 1, 28);
309   constexpr weekday wd = get_weekday(cd);
310   static_assert(wd == weekday::thursday, "Weekday");
311 }
312 
TEST(CivilTime,NextWeekDay)313 TEST(CivilTime, NextWeekDay) {
314   constexpr civil_day cd(2016, 1, 28);
315   constexpr civil_day next = next_weekday(cd, weekday::thursday);
316   static_assert(next.year() == 2016, "NextWeekDay.year");
317   static_assert(next.month() == 2, "NextWeekDay.month");
318   static_assert(next.day() == 4, "NextWeekDay.day");
319 }
320 
TEST(CivilTime,PrevWeekDay)321 TEST(CivilTime, PrevWeekDay) {
322   constexpr civil_day cd(2016, 1, 28);
323   constexpr civil_day prev = prev_weekday(cd, weekday::thursday);
324   static_assert(prev.year() == 2016, "PrevWeekDay.year");
325   static_assert(prev.month() == 1, "PrevWeekDay.month");
326   static_assert(prev.day() == 21, "PrevWeekDay.day");
327 }
328 
TEST(CivilTime,YearDay)329 TEST(CivilTime, YearDay) {
330   constexpr civil_day cd(2016, 1, 28);
331   constexpr int yd = get_yearday(cd);
332   static_assert(yd == 28, "YearDay");
333 }
334 #endif  // __cpp_constexpr >= 201304 || (defined(_MSC_VER) && _MSC_VER >= 1910)
335 
336 // The remaining tests do not use constexpr.
337 
TEST(CivilTime,DefaultConstruction)338 TEST(CivilTime, DefaultConstruction) {
339   civil_second ss;
340   EXPECT_EQ("1970-01-01T00:00:00", Format(ss));
341 
342   civil_minute mm;
343   EXPECT_EQ("1970-01-01T00:00", Format(mm));
344 
345   civil_hour hh;
346   EXPECT_EQ("1970-01-01T00", Format(hh));
347 
348   civil_day d;
349   EXPECT_EQ("1970-01-01", Format(d));
350 
351   civil_month m;
352   EXPECT_EQ("1970-01", Format(m));
353 
354   civil_year y;
355   EXPECT_EQ("1970", Format(y));
356 }
357 
TEST(CivilTime,StructMember)358 TEST(CivilTime, StructMember) {
359   struct S {
360     civil_day day;
361   };
362   S s = {};
363   EXPECT_EQ(civil_day{}, s.day);
364 }
365 
TEST(CivilTime,FieldsConstruction)366 TEST(CivilTime, FieldsConstruction) {
367   EXPECT_EQ("2015-01-02T03:04:05", Format(civil_second(2015, 1, 2, 3, 4, 5)));
368   EXPECT_EQ("2015-01-02T03:04:00", Format(civil_second(2015, 1, 2, 3, 4)));
369   EXPECT_EQ("2015-01-02T03:00:00", Format(civil_second(2015, 1, 2, 3)));
370   EXPECT_EQ("2015-01-02T00:00:00", Format(civil_second(2015, 1, 2)));
371   EXPECT_EQ("2015-01-01T00:00:00", Format(civil_second(2015, 1)));
372   EXPECT_EQ("2015-01-01T00:00:00", Format(civil_second(2015)));
373 
374   EXPECT_EQ("2015-01-02T03:04", Format(civil_minute(2015, 1, 2, 3, 4, 5)));
375   EXPECT_EQ("2015-01-02T03:04", Format(civil_minute(2015, 1, 2, 3, 4)));
376   EXPECT_EQ("2015-01-02T03:00", Format(civil_minute(2015, 1, 2, 3)));
377   EXPECT_EQ("2015-01-02T00:00", Format(civil_minute(2015, 1, 2)));
378   EXPECT_EQ("2015-01-01T00:00", Format(civil_minute(2015, 1)));
379   EXPECT_EQ("2015-01-01T00:00", Format(civil_minute(2015)));
380 
381   EXPECT_EQ("2015-01-02T03", Format(civil_hour(2015, 1, 2, 3, 4, 5)));
382   EXPECT_EQ("2015-01-02T03", Format(civil_hour(2015, 1, 2, 3, 4)));
383   EXPECT_EQ("2015-01-02T03", Format(civil_hour(2015, 1, 2, 3)));
384   EXPECT_EQ("2015-01-02T00", Format(civil_hour(2015, 1, 2)));
385   EXPECT_EQ("2015-01-01T00", Format(civil_hour(2015, 1)));
386   EXPECT_EQ("2015-01-01T00", Format(civil_hour(2015)));
387 
388   EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2, 3, 4, 5)));
389   EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2, 3, 4)));
390   EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2, 3)));
391   EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2)));
392   EXPECT_EQ("2015-01-01", Format(civil_day(2015, 1)));
393   EXPECT_EQ("2015-01-01", Format(civil_day(2015)));
394 
395   EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2, 3, 4, 5)));
396   EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2, 3, 4)));
397   EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2, 3)));
398   EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2)));
399   EXPECT_EQ("2015-01", Format(civil_month(2015, 1)));
400   EXPECT_EQ("2015-01", Format(civil_month(2015)));
401 
402   EXPECT_EQ("2015", Format(civil_year(2015, 1, 2, 3, 4, 5)));
403   EXPECT_EQ("2015", Format(civil_year(2015, 1, 2, 3, 4)));
404   EXPECT_EQ("2015", Format(civil_year(2015, 1, 2, 3)));
405   EXPECT_EQ("2015", Format(civil_year(2015, 1, 2)));
406   EXPECT_EQ("2015", Format(civil_year(2015, 1)));
407   EXPECT_EQ("2015", Format(civil_year(2015)));
408 }
409 
TEST(CivilTime,FieldsConstructionLimits)410 TEST(CivilTime, FieldsConstructionLimits) {
411   const int kIntMax = std::numeric_limits<int>::max();
412   EXPECT_EQ("2038-01-19T03:14:07",
413             Format(civil_second(1970, 1, 1, 0, 0, kIntMax)));
414   EXPECT_EQ("6121-02-11T05:21:07",
415             Format(civil_second(1970, 1, 1, 0, kIntMax, kIntMax)));
416   EXPECT_EQ("251104-11-20T12:21:07",
417             Format(civil_second(1970, 1, 1, kIntMax, kIntMax, kIntMax)));
418   EXPECT_EQ("6130715-05-30T12:21:07",
419             Format(civil_second(1970, 1, kIntMax, kIntMax, kIntMax, kIntMax)));
420   EXPECT_EQ(
421       "185087685-11-26T12:21:07",
422       Format(civil_second(1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax)));
423 
424   const int kIntMin = std::numeric_limits<int>::min();
425   EXPECT_EQ("1901-12-13T20:45:52",
426             Format(civil_second(1970, 1, 1, 0, 0, kIntMin)));
427   EXPECT_EQ("-2182-11-20T18:37:52",
428             Format(civil_second(1970, 1, 1, 0, kIntMin, kIntMin)));
429   EXPECT_EQ("-247165-02-11T10:37:52",
430             Format(civil_second(1970, 1, 1, kIntMin, kIntMin, kIntMin)));
431   EXPECT_EQ("-6126776-08-01T10:37:52",
432             Format(civil_second(1970, 1, kIntMin, kIntMin, kIntMin, kIntMin)));
433   EXPECT_EQ(
434       "-185083747-10-31T10:37:52",
435       Format(civil_second(1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
436 }
437 
TEST(CivilTime,ImplicitCrossAlignment)438 TEST(CivilTime, ImplicitCrossAlignment) {
439   civil_year year(2015);
440   civil_month month = year;
441   civil_day day = month;
442   civil_hour hour = day;
443   civil_minute minute = hour;
444   civil_second second = minute;
445 
446   second = year;
447   EXPECT_EQ(second, year);
448   second = month;
449   EXPECT_EQ(second, month);
450   second = day;
451   EXPECT_EQ(second, day);
452   second = hour;
453   EXPECT_EQ(second, hour);
454   second = minute;
455   EXPECT_EQ(second, minute);
456 
457   minute = year;
458   EXPECT_EQ(minute, year);
459   minute = month;
460   EXPECT_EQ(minute, month);
461   minute = day;
462   EXPECT_EQ(minute, day);
463   minute = hour;
464   EXPECT_EQ(minute, hour);
465 
466   hour = year;
467   EXPECT_EQ(hour, year);
468   hour = month;
469   EXPECT_EQ(hour, month);
470   hour = day;
471   EXPECT_EQ(hour, day);
472 
473   day = year;
474   EXPECT_EQ(day, year);
475   day = month;
476   EXPECT_EQ(day, month);
477 
478   month = year;
479   EXPECT_EQ(month, year);
480 
481   // Ensures unsafe conversions are not allowed.
482   EXPECT_FALSE((std::is_convertible<civil_second, civil_minute>::value));
483   EXPECT_FALSE((std::is_convertible<civil_second, civil_hour>::value));
484   EXPECT_FALSE((std::is_convertible<civil_second, civil_day>::value));
485   EXPECT_FALSE((std::is_convertible<civil_second, civil_month>::value));
486   EXPECT_FALSE((std::is_convertible<civil_second, civil_year>::value));
487 
488   EXPECT_FALSE((std::is_convertible<civil_minute, civil_hour>::value));
489   EXPECT_FALSE((std::is_convertible<civil_minute, civil_day>::value));
490   EXPECT_FALSE((std::is_convertible<civil_minute, civil_month>::value));
491   EXPECT_FALSE((std::is_convertible<civil_minute, civil_year>::value));
492 
493   EXPECT_FALSE((std::is_convertible<civil_hour, civil_day>::value));
494   EXPECT_FALSE((std::is_convertible<civil_hour, civil_month>::value));
495   EXPECT_FALSE((std::is_convertible<civil_hour, civil_year>::value));
496 
497   EXPECT_FALSE((std::is_convertible<civil_day, civil_month>::value));
498   EXPECT_FALSE((std::is_convertible<civil_day, civil_year>::value));
499 
500   EXPECT_FALSE((std::is_convertible<civil_month, civil_year>::value));
501 }
502 
TEST(CivilTime,ExplicitCrossAlignment)503 TEST(CivilTime, ExplicitCrossAlignment) {
504   //
505   // Assign from smaller units -> larger units
506   //
507 
508   civil_second second(2015, 1, 2, 3, 4, 5);
509   EXPECT_EQ("2015-01-02T03:04:05", Format(second));
510 
511   civil_minute minute(second);
512   EXPECT_EQ("2015-01-02T03:04", Format(minute));
513 
514   civil_hour hour(minute);
515   EXPECT_EQ("2015-01-02T03", Format(hour));
516 
517   civil_day day(hour);
518   EXPECT_EQ("2015-01-02", Format(day));
519 
520   civil_month month(day);
521   EXPECT_EQ("2015-01", Format(month));
522 
523   civil_year year(month);
524   EXPECT_EQ("2015", Format(year));
525 
526   //
527   // Now assign from larger units -> smaller units
528   //
529 
530   month = civil_month(year);
531   EXPECT_EQ("2015-01", Format(month));
532 
533   day = civil_day(month);
534   EXPECT_EQ("2015-01-01", Format(day));
535 
536   hour = civil_hour(day);
537   EXPECT_EQ("2015-01-01T00", Format(hour));
538 
539   minute = civil_minute(hour);
540   EXPECT_EQ("2015-01-01T00:00", Format(minute));
541 
542   second = civil_second(minute);
543   EXPECT_EQ("2015-01-01T00:00:00", Format(second));
544 }
545 
546 // Metafunction to test whether difference is allowed between two types.
547 template <typename T1, typename T2>
548 struct HasDifference {
549   template <typename U1, typename U2>
550   static std::false_type test(...);
551   template <typename U1, typename U2>
552   static std::true_type test(decltype(std::declval<U1>() - std::declval<U2>()));
553   static constexpr bool value = decltype(test<T1, T2>(0))::value;
554 };
555 
TEST(CivilTime,DisallowCrossAlignedDifference)556 TEST(CivilTime, DisallowCrossAlignedDifference) {
557   // Difference is allowed between types with the same alignment.
558   static_assert(HasDifference<civil_second, civil_second>::value, "");
559   static_assert(HasDifference<civil_minute, civil_minute>::value, "");
560   static_assert(HasDifference<civil_hour, civil_hour>::value, "");
561   static_assert(HasDifference<civil_day, civil_day>::value, "");
562   static_assert(HasDifference<civil_month, civil_month>::value, "");
563   static_assert(HasDifference<civil_year, civil_year>::value, "");
564 
565   // Difference is disallowed between types with different alignments.
566   static_assert(!HasDifference<civil_second, civil_minute>::value, "");
567   static_assert(!HasDifference<civil_second, civil_hour>::value, "");
568   static_assert(!HasDifference<civil_second, civil_day>::value, "");
569   static_assert(!HasDifference<civil_second, civil_month>::value, "");
570   static_assert(!HasDifference<civil_second, civil_year>::value, "");
571 
572   static_assert(!HasDifference<civil_minute, civil_hour>::value, "");
573   static_assert(!HasDifference<civil_minute, civil_day>::value, "");
574   static_assert(!HasDifference<civil_minute, civil_month>::value, "");
575   static_assert(!HasDifference<civil_minute, civil_year>::value, "");
576 
577   static_assert(!HasDifference<civil_hour, civil_day>::value, "");
578   static_assert(!HasDifference<civil_hour, civil_month>::value, "");
579   static_assert(!HasDifference<civil_hour, civil_year>::value, "");
580 
581   static_assert(!HasDifference<civil_day, civil_month>::value, "");
582   static_assert(!HasDifference<civil_day, civil_year>::value, "");
583 
584   static_assert(!HasDifference<civil_month, civil_year>::value, "");
585 }
586 
TEST(CivilTime,ValueSemantics)587 TEST(CivilTime, ValueSemantics) {
588   const civil_hour a(2015, 1, 2, 3);
589   const civil_hour b = a;
590   const civil_hour c(b);
591   civil_hour d;
592   d = c;
593   EXPECT_EQ("2015-01-02T03", Format(d));
594 }
595 
TEST(CivilTime,Relational)596 TEST(CivilTime, Relational) {
597   // Tests that the alignment unit is ignored in comparison.
598   const civil_year year(2014);
599   const civil_month month(year);
600   EXPECT_EQ(year, month);
601 
602 #define TEST_RELATIONAL(OLDER, YOUNGER) \
603   do {                                  \
604     EXPECT_FALSE(OLDER < OLDER);        \
605     EXPECT_FALSE(OLDER > OLDER);        \
606     EXPECT_TRUE(OLDER >= OLDER);        \
607     EXPECT_TRUE(OLDER <= OLDER);        \
608     EXPECT_FALSE(YOUNGER < YOUNGER);    \
609     EXPECT_FALSE(YOUNGER > YOUNGER);    \
610     EXPECT_TRUE(YOUNGER >= YOUNGER);    \
611     EXPECT_TRUE(YOUNGER <= YOUNGER);    \
612     EXPECT_EQ(OLDER, OLDER);            \
613     EXPECT_NE(OLDER, YOUNGER);          \
614     EXPECT_LT(OLDER, YOUNGER);          \
615     EXPECT_LE(OLDER, YOUNGER);          \
616     EXPECT_GT(YOUNGER, OLDER);          \
617     EXPECT_GE(YOUNGER, OLDER);          \
618   } while (0)
619 
620   // Alignment is ignored in comparison (verified above), so kSecond is used
621   // to test comparison in all field positions.
622   TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
623                   civil_second(2015, 1, 1, 0, 0, 0));
624   TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
625                   civil_second(2014, 2, 1, 0, 0, 0));
626   TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
627                   civil_second(2014, 1, 2, 0, 0, 0));
628   TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
629                   civil_second(2014, 1, 1, 1, 0, 0));
630   TEST_RELATIONAL(civil_second(2014, 1, 1, 1, 0, 0),
631                   civil_second(2014, 1, 1, 1, 1, 0));
632   TEST_RELATIONAL(civil_second(2014, 1, 1, 1, 1, 0),
633                   civil_second(2014, 1, 1, 1, 1, 1));
634 
635   // Tests the relational operators of two different civil-time types.
636   TEST_RELATIONAL(civil_day(2014, 1, 1), civil_minute(2014, 1, 1, 1, 1));
637   TEST_RELATIONAL(civil_day(2014, 1, 1), civil_month(2014, 2));
638 
639 #undef TEST_RELATIONAL
640 }
641 
TEST(CivilTime,Arithmetic)642 TEST(CivilTime, Arithmetic) {
643   civil_second second(2015, 1, 2, 3, 4, 5);
644   EXPECT_EQ("2015-01-02T03:04:06", Format(second += 1));
645   EXPECT_EQ("2015-01-02T03:04:07", Format(second + 1));
646   EXPECT_EQ("2015-01-02T03:04:08", Format(2 + second));
647   EXPECT_EQ("2015-01-02T03:04:05", Format(second - 1));
648   EXPECT_EQ("2015-01-02T03:04:05", Format(second -= 1));
649   EXPECT_EQ("2015-01-02T03:04:05", Format(second++));
650   EXPECT_EQ("2015-01-02T03:04:07", Format(++second));
651   EXPECT_EQ("2015-01-02T03:04:07", Format(second--));
652   EXPECT_EQ("2015-01-02T03:04:05", Format(--second));
653 
654   civil_minute minute(2015, 1, 2, 3, 4);
655   EXPECT_EQ("2015-01-02T03:05", Format(minute += 1));
656   EXPECT_EQ("2015-01-02T03:06", Format(minute + 1));
657   EXPECT_EQ("2015-01-02T03:07", Format(2 + minute));
658   EXPECT_EQ("2015-01-02T03:04", Format(minute - 1));
659   EXPECT_EQ("2015-01-02T03:04", Format(minute -= 1));
660   EXPECT_EQ("2015-01-02T03:04", Format(minute++));
661   EXPECT_EQ("2015-01-02T03:06", Format(++minute));
662   EXPECT_EQ("2015-01-02T03:06", Format(minute--));
663   EXPECT_EQ("2015-01-02T03:04", Format(--minute));
664 
665   civil_hour hour(2015, 1, 2, 3);
666   EXPECT_EQ("2015-01-02T04", Format(hour += 1));
667   EXPECT_EQ("2015-01-02T05", Format(hour + 1));
668   EXPECT_EQ("2015-01-02T06", Format(2 + hour));
669   EXPECT_EQ("2015-01-02T03", Format(hour - 1));
670   EXPECT_EQ("2015-01-02T03", Format(hour -= 1));
671   EXPECT_EQ("2015-01-02T03", Format(hour++));
672   EXPECT_EQ("2015-01-02T05", Format(++hour));
673   EXPECT_EQ("2015-01-02T05", Format(hour--));
674   EXPECT_EQ("2015-01-02T03", Format(--hour));
675 
676   civil_day day(2015, 1, 2);
677   EXPECT_EQ("2015-01-03", Format(day += 1));
678   EXPECT_EQ("2015-01-04", Format(day + 1));
679   EXPECT_EQ("2015-01-05", Format(2 + day));
680   EXPECT_EQ("2015-01-02", Format(day - 1));
681   EXPECT_EQ("2015-01-02", Format(day -= 1));
682   EXPECT_EQ("2015-01-02", Format(day++));
683   EXPECT_EQ("2015-01-04", Format(++day));
684   EXPECT_EQ("2015-01-04", Format(day--));
685   EXPECT_EQ("2015-01-02", Format(--day));
686 
687   civil_month month(2015, 1);
688   EXPECT_EQ("2015-02", Format(month += 1));
689   EXPECT_EQ("2015-03", Format(month + 1));
690   EXPECT_EQ("2015-04", Format(2 + month));
691   EXPECT_EQ("2015-01", Format(month - 1));
692   EXPECT_EQ("2015-01", Format(month -= 1));
693   EXPECT_EQ("2015-01", Format(month++));
694   EXPECT_EQ("2015-03", Format(++month));
695   EXPECT_EQ("2015-03", Format(month--));
696   EXPECT_EQ("2015-01", Format(--month));
697 
698   civil_year year(2015);
699   EXPECT_EQ("2016", Format(year += 1));
700   EXPECT_EQ("2017", Format(year + 1));
701   EXPECT_EQ("2018", Format(2 + year));
702   EXPECT_EQ("2015", Format(year - 1));
703   EXPECT_EQ("2015", Format(year -= 1));
704   EXPECT_EQ("2015", Format(year++));
705   EXPECT_EQ("2017", Format(++year));
706   EXPECT_EQ("2017", Format(year--));
707   EXPECT_EQ("2015", Format(--year));
708 }
709 
TEST(CivilTime,ArithmeticLimits)710 TEST(CivilTime, ArithmeticLimits) {
711   const int kIntMax = std::numeric_limits<int>::max();
712   const int kIntMin = std::numeric_limits<int>::min();
713 
714   civil_second second(1970, 1, 1, 0, 0, 0);
715   second += kIntMax;
716   EXPECT_EQ("2038-01-19T03:14:07", Format(second));
717   second -= kIntMax;
718   EXPECT_EQ("1970-01-01T00:00:00", Format(second));
719   second += kIntMin;
720   EXPECT_EQ("1901-12-13T20:45:52", Format(second));
721   second -= kIntMin;
722   EXPECT_EQ("1970-01-01T00:00:00", Format(second));
723 
724   civil_minute minute(1970, 1, 1, 0, 0);
725   minute += kIntMax;
726   EXPECT_EQ("6053-01-23T02:07", Format(minute));
727   minute -= kIntMax;
728   EXPECT_EQ("1970-01-01T00:00", Format(minute));
729   minute += kIntMin;
730   EXPECT_EQ("-2114-12-08T21:52", Format(minute));
731   minute -= kIntMin;
732   EXPECT_EQ("1970-01-01T00:00", Format(minute));
733 
734   civil_hour hour(1970, 1, 1, 0);
735   hour += kIntMax;
736   EXPECT_EQ("246953-10-09T07", Format(hour));
737   hour -= kIntMax;
738   EXPECT_EQ("1970-01-01T00", Format(hour));
739   hour += kIntMin;
740   EXPECT_EQ("-243014-03-24T16", Format(hour));
741   hour -= kIntMin;
742   EXPECT_EQ("1970-01-01T00", Format(hour));
743 
744   civil_day day(1970, 1, 1);
745   day += kIntMax;
746   EXPECT_EQ("5881580-07-11", Format(day));
747   day -= kIntMax;
748   EXPECT_EQ("1970-01-01", Format(day));
749   day += kIntMin;
750   EXPECT_EQ("-5877641-06-23", Format(day));
751   day -= kIntMin;
752   EXPECT_EQ("1970-01-01", Format(day));
753 
754   civil_month month(1970, 1);
755   month += kIntMax;
756   EXPECT_EQ("178958940-08", Format(month));
757   month -= kIntMax;
758   EXPECT_EQ("1970-01", Format(month));
759   month += kIntMin;
760   EXPECT_EQ("-178955001-05", Format(month));
761   month -= kIntMin;
762   EXPECT_EQ("1970-01", Format(month));
763 
764   civil_year year(0);
765   year += kIntMax;
766   EXPECT_EQ("2147483647", Format(year));
767   year -= kIntMax;
768   EXPECT_EQ("0", Format(year));
769   year += kIntMin;
770   EXPECT_EQ("-2147483648", Format(year));
771   year -= kIntMin;
772   EXPECT_EQ("0", Format(year));
773 }
774 
TEST(CivilTime,ArithmeticDifference)775 TEST(CivilTime, ArithmeticDifference) {
776   civil_second second(2015, 1, 2, 3, 4, 5);
777   EXPECT_EQ(0, second - second);
778   EXPECT_EQ(10, (second + 10) - second);
779   EXPECT_EQ(-10, (second - 10) - second);
780 
781   civil_minute minute(2015, 1, 2, 3, 4);
782   EXPECT_EQ(0, minute - minute);
783   EXPECT_EQ(10, (minute + 10) - minute);
784   EXPECT_EQ(-10, (minute - 10) - minute);
785 
786   civil_hour hour(2015, 1, 2, 3);
787   EXPECT_EQ(0, hour - hour);
788   EXPECT_EQ(10, (hour + 10) - hour);
789   EXPECT_EQ(-10, (hour - 10) - hour);
790 
791   civil_day day(2015, 1, 2);
792   EXPECT_EQ(0, day - day);
793   EXPECT_EQ(10, (day + 10) - day);
794   EXPECT_EQ(-10, (day - 10) - day);
795 
796   civil_month month(2015, 1);
797   EXPECT_EQ(0, month - month);
798   EXPECT_EQ(10, (month + 10) - month);
799   EXPECT_EQ(-10, (month - 10) - month);
800 
801   civil_year year(2015);
802   EXPECT_EQ(0, year - year);
803   EXPECT_EQ(10, (year + 10) - year);
804   EXPECT_EQ(-10, (year - 10) - year);
805 }
806 
TEST(CivilTime,DifferenceLimits)807 TEST(CivilTime, DifferenceLimits) {
808   const int kIntMax = std::numeric_limits<int>::max();
809   const int kIntMin = std::numeric_limits<int>::min();
810 
811   // Check day arithmetic at the end of the year range.
812   const civil_day max_day(kIntMax, 12, 31);
813   EXPECT_EQ(1, max_day - (max_day - 1));
814   EXPECT_EQ(-1, (max_day - 1) - max_day);
815 
816   // Check day arithmetic at the end of the year range.
817   const civil_day min_day(kIntMin, 1, 1);
818   EXPECT_EQ(1, (min_day + 1) - min_day);
819   EXPECT_EQ(-1, min_day - (min_day + 1));
820 
821   // Check the limits of the return value.
822   const civil_day d1(1970, 1, 1);
823   const civil_day d2(5881580, 7, 11);
824   EXPECT_EQ(kIntMax, d2 - d1);
825   EXPECT_EQ(kIntMin, d1 - (d2 + 1));
826 }
827 
TEST(CivilTime,Properties)828 TEST(CivilTime, Properties) {
829   civil_second ss(2015, 2, 3, 4, 5, 6);
830   EXPECT_EQ(2015, ss.year());
831   EXPECT_EQ(2, ss.month());
832   EXPECT_EQ(3, ss.day());
833   EXPECT_EQ(4, ss.hour());
834   EXPECT_EQ(5, ss.minute());
835   EXPECT_EQ(6, ss.second());
836   EXPECT_EQ(weekday::tuesday, get_weekday(ss));
837   EXPECT_EQ(34, get_yearday(ss));
838 
839   civil_minute mm(2015, 2, 3, 4, 5, 6);
840   EXPECT_EQ(2015, mm.year());
841   EXPECT_EQ(2, mm.month());
842   EXPECT_EQ(3, mm.day());
843   EXPECT_EQ(4, mm.hour());
844   EXPECT_EQ(5, mm.minute());
845   EXPECT_EQ(0, mm.second());
846   EXPECT_EQ(weekday::tuesday, get_weekday(mm));
847   EXPECT_EQ(34, get_yearday(mm));
848 
849   civil_hour hh(2015, 2, 3, 4, 5, 6);
850   EXPECT_EQ(2015, hh.year());
851   EXPECT_EQ(2, hh.month());
852   EXPECT_EQ(3, hh.day());
853   EXPECT_EQ(4, hh.hour());
854   EXPECT_EQ(0, hh.minute());
855   EXPECT_EQ(0, hh.second());
856   EXPECT_EQ(weekday::tuesday, get_weekday(hh));
857   EXPECT_EQ(34, get_yearday(hh));
858 
859   civil_day d(2015, 2, 3, 4, 5, 6);
860   EXPECT_EQ(2015, d.year());
861   EXPECT_EQ(2, d.month());
862   EXPECT_EQ(3, d.day());
863   EXPECT_EQ(0, d.hour());
864   EXPECT_EQ(0, d.minute());
865   EXPECT_EQ(0, d.second());
866   EXPECT_EQ(weekday::tuesday, get_weekday(d));
867   EXPECT_EQ(34, get_yearday(d));
868 
869   civil_month m(2015, 2, 3, 4, 5, 6);
870   EXPECT_EQ(2015, m.year());
871   EXPECT_EQ(2, m.month());
872   EXPECT_EQ(1, m.day());
873   EXPECT_EQ(0, m.hour());
874   EXPECT_EQ(0, m.minute());
875   EXPECT_EQ(0, m.second());
876   EXPECT_EQ(weekday::sunday, get_weekday(m));
877   EXPECT_EQ(32, get_yearday(m));
878 
879   civil_year y(2015, 2, 3, 4, 5, 6);
880   EXPECT_EQ(2015, y.year());
881   EXPECT_EQ(1, y.month());
882   EXPECT_EQ(1, y.day());
883   EXPECT_EQ(0, y.hour());
884   EXPECT_EQ(0, y.minute());
885   EXPECT_EQ(0, y.second());
886   EXPECT_EQ(weekday::thursday, get_weekday(y));
887   EXPECT_EQ(1, get_yearday(y));
888 }
889 
TEST(CivilTime,OutputStream)890 TEST(CivilTime, OutputStream) {
891   // Tests formatting of civil_year, which does not pad.
892   EXPECT_EQ("2016", Format(civil_year(2016)));
893   EXPECT_EQ("123", Format(civil_year(123)));
894   EXPECT_EQ("0", Format(civil_year(0)));
895   EXPECT_EQ("-1", Format(civil_year(-1)));
896 
897   // Tests formatting of sub-year types, which pad to 2 digits
898   EXPECT_EQ("2016-02", Format(civil_month(2016, 2)));
899   EXPECT_EQ("2016-02-03", Format(civil_day(2016, 2, 3)));
900   EXPECT_EQ("2016-02-03T04", Format(civil_hour(2016, 2, 3, 4)));
901   EXPECT_EQ("2016-02-03T04:05", Format(civil_minute(2016, 2, 3, 4, 5)));
902   EXPECT_EQ("2016-02-03T04:05:06", Format(civil_second(2016, 2, 3, 4, 5, 6)));
903 
904   // Tests formatting of weekday.
905   EXPECT_EQ("Monday", Format(weekday::monday));
906   EXPECT_EQ("Tuesday", Format(weekday::tuesday));
907   EXPECT_EQ("Wednesday", Format(weekday::wednesday));
908   EXPECT_EQ("Thursday", Format(weekday::thursday));
909   EXPECT_EQ("Friday", Format(weekday::friday));
910   EXPECT_EQ("Saturday", Format(weekday::saturday));
911   EXPECT_EQ("Sunday", Format(weekday::sunday));
912 }
913 
TEST(CivilTime,OutputStreamLeftFillWidth)914 TEST(CivilTime, OutputStreamLeftFillWidth) {
915   civil_second cs(2016, 2, 3, 4, 5, 6);
916   {
917     std::stringstream ss;
918     ss << std::left << std::setfill('.');
919     ss << std::setw(3) << 'X';
920     ss << std::setw(21) << civil_year(cs);
921     ss << std::setw(3) << 'X';
922     EXPECT_EQ("X..2016.................X..", ss.str());
923   }
924   {
925     std::stringstream ss;
926     ss << std::left << std::setfill('.');
927     ss << std::setw(3) << 'X';
928     ss << std::setw(21) << civil_month(cs);
929     ss << std::setw(3) << 'X';
930     EXPECT_EQ("X..2016-02..............X..", ss.str());
931   }
932   {
933     std::stringstream ss;
934     ss << std::left << std::setfill('.');
935     ss << std::setw(3) << 'X';
936     ss << std::setw(21) << civil_day(cs);
937     ss << std::setw(3) << 'X';
938     EXPECT_EQ("X..2016-02-03...........X..", ss.str());
939   }
940   {
941     std::stringstream ss;
942     ss << std::left << std::setfill('.');
943     ss << std::setw(3) << 'X';
944     ss << std::setw(21) << civil_hour(cs);
945     ss << std::setw(3) << 'X';
946     EXPECT_EQ("X..2016-02-03T04........X..", ss.str());
947   }
948   {
949     std::stringstream ss;
950     ss << std::left << std::setfill('.');
951     ss << std::setw(3) << 'X';
952     ss << std::setw(21) << civil_minute(cs);
953     ss << std::setw(3) << 'X';
954     EXPECT_EQ("X..2016-02-03T04:05.....X..", ss.str());
955   }
956   {
957     std::stringstream ss;
958     ss << std::left << std::setfill('.');
959     ss << std::setw(3) << 'X';
960     ss << std::setw(21) << civil_second(cs);
961     ss << std::setw(3) << 'X';
962     EXPECT_EQ("X..2016-02-03T04:05:06..X..", ss.str());
963   }
964 }
965 
TEST(CivilTime,NextPrevWeekday)966 TEST(CivilTime, NextPrevWeekday) {
967   // Jan 1, 1970 was a Thursday.
968   const civil_day thursday(1970, 1, 1);
969   EXPECT_EQ(weekday::thursday, get_weekday(thursday));
970 
971   // Thursday -> Thursday
972   civil_day d = next_weekday(thursday, weekday::thursday);
973   EXPECT_EQ(7, d - thursday) << Format(d);
974   EXPECT_EQ(d - 14, prev_weekday(thursday, weekday::thursday));
975 
976   // Thursday -> Friday
977   d = next_weekday(thursday, weekday::friday);
978   EXPECT_EQ(1, d - thursday) << Format(d);
979   EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::friday));
980 
981   // Thursday -> Saturday
982   d = next_weekday(thursday, weekday::saturday);
983   EXPECT_EQ(2, d - thursday) << Format(d);
984   EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::saturday));
985 
986   // Thursday -> Sunday
987   d = next_weekday(thursday, weekday::sunday);
988   EXPECT_EQ(3, d - thursday) << Format(d);
989   EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::sunday));
990 
991   // Thursday -> Monday
992   d = next_weekday(thursday, weekday::monday);
993   EXPECT_EQ(4, d - thursday) << Format(d);
994   EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::monday));
995 
996   // Thursday -> Tuesday
997   d = next_weekday(thursday, weekday::tuesday);
998   EXPECT_EQ(5, d - thursday) << Format(d);
999   EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::tuesday));
1000 
1001   // Thursday -> Wednesday
1002   d = next_weekday(thursday, weekday::wednesday);
1003   EXPECT_EQ(6, d - thursday) << Format(d);
1004   EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::wednesday));
1005 }
1006 
TEST(CivilTime,NormalizeWithHugeYear)1007 TEST(CivilTime, NormalizeWithHugeYear) {
1008   civil_month c(9223372036854775807, 1);
1009   EXPECT_EQ("9223372036854775807-01", Format(c));
1010   c = c - 1;  // Causes normalization
1011   EXPECT_EQ("9223372036854775806-12", Format(c));
1012 
1013   c = civil_month(-9223372036854775807 - 1, 1);
1014   EXPECT_EQ("-9223372036854775808-01", Format(c));
1015   c = c + 12;  // Causes normalization
1016   EXPECT_EQ("-9223372036854775807-01", Format(c));
1017 }
1018 
TEST(CivilTime,LeapYears)1019 TEST(CivilTime, LeapYears) {
1020   // Test data for leap years.
1021   const struct {
1022     int year;
1023     int days;
1024     struct {
1025       int month;
1026       int day;
1027     } leap_day;  // The date of the day after Feb 28.
1028   } kLeapYearTable[]{
1029       {1900, 365, {3, 1}},  {1999, 365, {3, 1}},
1030       {2000, 366, {2, 29}},  // leap year
1031       {2001, 365, {3, 1}},  {2002, 365, {3, 1}},
1032       {2003, 365, {3, 1}},  {2004, 366, {2, 29}},  // leap year
1033       {2005, 365, {3, 1}},  {2006, 365, {3, 1}},
1034       {2007, 365, {3, 1}},  {2008, 366, {2, 29}},  // leap year
1035       {2009, 365, {3, 1}},  {2100, 365, {3, 1}},
1036   };
1037 
1038   for (const auto& e : kLeapYearTable) {
1039     // Tests incrementing through the leap day.
1040     const civil_day feb28(e.year, 2, 28);
1041     const civil_day next_day = feb28 + 1;
1042     EXPECT_EQ(e.leap_day.month, next_day.month());
1043     EXPECT_EQ(e.leap_day.day, next_day.day());
1044 
1045     // Tests difference in days of leap years.
1046     const civil_year year(feb28);
1047     const civil_year next_year = year + 1;
1048     EXPECT_EQ(e.days, civil_day(next_year) - civil_day(year));
1049   }
1050 }
1051 
TEST(CivilTime,FirstThursdayInMonth)1052 TEST(CivilTime, FirstThursdayInMonth) {
1053   const civil_day nov1(2014, 11, 1);
1054   const civil_day thursday = next_weekday(nov1 - 1, weekday::thursday);
1055   EXPECT_EQ("2014-11-06", Format(thursday));
1056 
1057   // Bonus: Date of Thanksgiving in the United States
1058   // Rule: Fourth Thursday of November
1059   const civil_day thanksgiving = thursday + 7 * 3;
1060   EXPECT_EQ("2014-11-27", Format(thanksgiving));
1061 }
1062 
1063 }  // namespace cctz
1064 }  // namespace time_internal
1065 ABSL_NAMESPACE_END
1066 }  // namespace absl
1067