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


abseil_cpp
Author(s):
autogenerated on Tue Jun 18 2019 19:44:35