civil_time_test.cc
Go to the documentation of this file.
1 // Copyright 2018 The Abseil Authors.
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/civil_time.h"
16 
17 #include <limits>
18 #include <sstream>
19 #include <type_traits>
20 
21 #include "absl/base/macros.h"
22 #include "gtest/gtest.h"
23 
24 namespace {
25 
26 TEST(CivilTime, DefaultConstruction) {
28  EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
29 
31  EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
32 
33  absl::CivilHour hh;
34  EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
35 
37  EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
38 
40  EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
41 
43  EXPECT_EQ("1970", absl::FormatCivilTime(y));
44 }
45 
46 TEST(CivilTime, StructMember) {
47  struct S {
48  absl::CivilDay day;
49  };
50  S s = {};
51  EXPECT_EQ(absl::CivilDay{}, s.day);
52 }
53 
54 TEST(CivilTime, FieldsConstruction) {
55  EXPECT_EQ("2015-01-02T03:04:05",
56  absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4, 5)));
57  EXPECT_EQ("2015-01-02T03:04:00",
58  absl::FormatCivilTime(absl::CivilSecond(2015, 1, 2, 3, 4)));
59  EXPECT_EQ("2015-01-02T03:00:00",
61  EXPECT_EQ("2015-01-02T00:00:00",
63  EXPECT_EQ("2015-01-01T00:00:00",
65  EXPECT_EQ("2015-01-01T00:00:00",
67 
68  EXPECT_EQ("2015-01-02T03:04",
69  absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4, 5)));
70  EXPECT_EQ("2015-01-02T03:04",
71  absl::FormatCivilTime(absl::CivilMinute(2015, 1, 2, 3, 4)));
72  EXPECT_EQ("2015-01-02T03:00",
74  EXPECT_EQ("2015-01-02T00:00",
76  EXPECT_EQ("2015-01-01T00:00",
78  EXPECT_EQ("2015-01-01T00:00",
80 
81  EXPECT_EQ("2015-01-02T03",
82  absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4, 5)));
83  EXPECT_EQ("2015-01-02T03",
84  absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3, 4)));
85  EXPECT_EQ("2015-01-02T03",
86  absl::FormatCivilTime(absl::CivilHour(2015, 1, 2, 3)));
87  EXPECT_EQ("2015-01-02T00",
89  EXPECT_EQ("2015-01-01T00",
91  EXPECT_EQ("2015-01-01T00",
93 
94  EXPECT_EQ("2015-01-02",
95  absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4, 5)));
96  EXPECT_EQ("2015-01-02",
97  absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3, 4)));
98  EXPECT_EQ("2015-01-02",
99  absl::FormatCivilTime(absl::CivilDay(2015, 1, 2, 3)));
100  EXPECT_EQ("2015-01-02",
102  EXPECT_EQ("2015-01-01",
104  EXPECT_EQ("2015-01-01",
106 
107  EXPECT_EQ("2015-01",
108  absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4, 5)));
109  EXPECT_EQ("2015-01",
110  absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3, 4)));
111  EXPECT_EQ("2015-01",
112  absl::FormatCivilTime(absl::CivilMonth(2015, 1, 2, 3)));
113  EXPECT_EQ("2015-01",
115  EXPECT_EQ("2015-01",
117  EXPECT_EQ("2015-01",
119 
120  EXPECT_EQ("2015",
121  absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4, 5)));
122  EXPECT_EQ("2015",
123  absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3, 4)));
124  EXPECT_EQ("2015",
125  absl::FormatCivilTime(absl::CivilYear(2015, 1, 2, 3)));
126  EXPECT_EQ("2015",
128  EXPECT_EQ("2015",
130  EXPECT_EQ("2015",
132 }
133 
134 TEST(CivilTime, FieldsConstructionLimits) {
135  const int kIntMax = std::numeric_limits<int>::max();
136  EXPECT_EQ("2038-01-19T03:14:07",
138  1970, 1, 1, 0, 0, kIntMax)));
139  EXPECT_EQ("6121-02-11T05:21:07",
141  1970, 1, 1, 0, kIntMax, kIntMax)));
142  EXPECT_EQ("251104-11-20T12:21:07",
144  1970, 1, 1, kIntMax, kIntMax, kIntMax)));
145  EXPECT_EQ("6130715-05-30T12:21:07",
147  1970, 1, kIntMax, kIntMax, kIntMax, kIntMax)));
148  EXPECT_EQ("185087685-11-26T12:21:07",
150  1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax)));
151 
152  const int kIntMin = std::numeric_limits<int>::min();
153  EXPECT_EQ("1901-12-13T20:45:52",
155  1970, 1, 1, 0, 0, kIntMin)));
156  EXPECT_EQ("-2182-11-20T18:37:52",
158  1970, 1, 1, 0, kIntMin, kIntMin)));
159  EXPECT_EQ("-247165-02-11T10:37:52",
161  1970, 1, 1, kIntMin, kIntMin, kIntMin)));
162  EXPECT_EQ("-6126776-08-01T10:37:52",
164  1970, 1, kIntMin, kIntMin, kIntMin, kIntMin)));
165  EXPECT_EQ("-185083747-10-31T10:37:52",
167  1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
168 }
169 
170 TEST(CivilTime, RangeLimits) {
171  const absl::civil_year_t kYearMax =
172  std::numeric_limits<absl::civil_year_t>::max();
173  EXPECT_EQ(absl::CivilYear(kYearMax),
175  EXPECT_EQ(absl::CivilMonth(kYearMax, 12),
177  EXPECT_EQ(absl::CivilDay(kYearMax, 12, 31),
179  EXPECT_EQ(absl::CivilHour(kYearMax, 12, 31, 23),
181  EXPECT_EQ(absl::CivilMinute(kYearMax, 12, 31, 23, 59),
183  EXPECT_EQ(absl::CivilSecond(kYearMax, 12, 31, 23, 59, 59),
185 
186  const absl::civil_year_t kYearMin =
187  std::numeric_limits<absl::civil_year_t>::min();
188  EXPECT_EQ(absl::CivilYear(kYearMin),
190  EXPECT_EQ(absl::CivilMonth(kYearMin, 1),
192  EXPECT_EQ(absl::CivilDay(kYearMin, 1, 1),
194  EXPECT_EQ(absl::CivilHour(kYearMin, 1, 1, 0),
196  EXPECT_EQ(absl::CivilMinute(kYearMin, 1, 1, 0, 0),
198  EXPECT_EQ(absl::CivilSecond(kYearMin, 1, 1, 0, 0, 0),
200 }
201 
202 TEST(CivilTime, ImplicitCrossAlignment) {
203  absl::CivilYear year(2015);
204  absl::CivilMonth month = year;
205  absl::CivilDay day = month;
206  absl::CivilHour hour = day;
207  absl::CivilMinute minute = hour;
208  absl::CivilSecond second = minute;
209 
210  second = year;
211  EXPECT_EQ(second, year);
212  second = month;
213  EXPECT_EQ(second, month);
214  second = day;
215  EXPECT_EQ(second, day);
216  second = hour;
217  EXPECT_EQ(second, hour);
218  second = minute;
219  EXPECT_EQ(second, minute);
220 
221  minute = year;
222  EXPECT_EQ(minute, year);
223  minute = month;
224  EXPECT_EQ(minute, month);
225  minute = day;
226  EXPECT_EQ(minute, day);
227  minute = hour;
228  EXPECT_EQ(minute, hour);
229 
230  hour = year;
231  EXPECT_EQ(hour, year);
232  hour = month;
233  EXPECT_EQ(hour, month);
234  hour = day;
235  EXPECT_EQ(hour, day);
236 
237  day = year;
238  EXPECT_EQ(day, year);
239  day = month;
240  EXPECT_EQ(day, month);
241 
242  month = year;
243  EXPECT_EQ(month, year);
244 
245  // Ensures unsafe conversions are not allowed.
246  EXPECT_FALSE(
248  EXPECT_FALSE(
250  EXPECT_FALSE(
252  EXPECT_FALSE(
254  EXPECT_FALSE(
256 
257  EXPECT_FALSE(
259  EXPECT_FALSE(
261  EXPECT_FALSE(
263  EXPECT_FALSE(
265 
266  EXPECT_FALSE(
268  EXPECT_FALSE(
270  EXPECT_FALSE(
272 
273  EXPECT_FALSE(
275  EXPECT_FALSE(
277 
278  EXPECT_FALSE(
280 }
281 
282 TEST(CivilTime, ExplicitCrossAlignment) {
283  //
284  // Assign from smaller units -> larger units
285  //
286 
287  absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
288  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second));
289 
290  absl::CivilMinute minute(second);
291  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute));
292 
293  absl::CivilHour hour(minute);
294  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour));
295 
296  absl::CivilDay day(hour);
297  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day));
298 
299  absl::CivilMonth month(day);
300  EXPECT_EQ("2015-01", absl::FormatCivilTime(month));
301 
302  absl::CivilYear year(month);
303  EXPECT_EQ("2015", absl::FormatCivilTime(year));
304 
305  //
306  // Now assign from larger units -> smaller units
307  //
308 
309  month = absl::CivilMonth(year);
310  EXPECT_EQ("2015-01", absl::FormatCivilTime(month));
311 
312  day = absl::CivilDay(month);
313  EXPECT_EQ("2015-01-01", absl::FormatCivilTime(day));
314 
315  hour = absl::CivilHour(day);
316  EXPECT_EQ("2015-01-01T00", absl::FormatCivilTime(hour));
317 
318  minute = absl::CivilMinute(hour);
319  EXPECT_EQ("2015-01-01T00:00", absl::FormatCivilTime(minute));
320 
321  second = absl::CivilSecond(minute);
322  EXPECT_EQ("2015-01-01T00:00:00", absl::FormatCivilTime(second));
323 }
324 
325 // Metafunction to test whether difference is allowed between two types.
326 template <typename T1, typename T2>
327 struct HasDiff {
328  template <typename U1, typename U2>
329  static std::false_type test(...);
330  template <typename U1, typename U2>
331  static std::true_type test(decltype(std::declval<U1>() - std::declval<U2>()));
332  static constexpr bool value = decltype(test<T1, T2>(0))::value;
333 };
334 
335 TEST(CivilTime, DisallowCrossAlignedDifference) {
336  // Difference is allowed between types with the same alignment.
343 
344  // Difference is disallowed between types with different alignments.
350 
355 
359 
362 
364 }
365 
366 TEST(CivilTime, ValueSemantics) {
367  const absl::CivilHour a(2015, 1, 2, 3);
368  const absl::CivilHour b = a;
369  const absl::CivilHour c(b);
370  absl::CivilHour d;
371  d = c;
372  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(d));
373 }
374 
375 TEST(CivilTime, Relational) {
376  // Tests that the alignment unit is ignored in comparison.
377  const absl::CivilYear year(2014);
378  const absl::CivilMonth month(year);
379  EXPECT_EQ(year, month);
380 
381 #define TEST_RELATIONAL(OLDER, YOUNGER) \
382  do { \
383  EXPECT_FALSE(OLDER < OLDER); \
384  EXPECT_FALSE(OLDER > OLDER); \
385  EXPECT_TRUE(OLDER >= OLDER); \
386  EXPECT_TRUE(OLDER <= OLDER); \
387  EXPECT_FALSE(YOUNGER < YOUNGER); \
388  EXPECT_FALSE(YOUNGER > YOUNGER); \
389  EXPECT_TRUE(YOUNGER >= YOUNGER); \
390  EXPECT_TRUE(YOUNGER <= YOUNGER); \
391  EXPECT_EQ(OLDER, OLDER); \
392  EXPECT_NE(OLDER, YOUNGER); \
393  EXPECT_LT(OLDER, YOUNGER); \
394  EXPECT_LE(OLDER, YOUNGER); \
395  EXPECT_GT(YOUNGER, OLDER); \
396  EXPECT_GE(YOUNGER, OLDER); \
397  } while (0)
398 
399  // Alignment is ignored in comparison (verified above), so CivilSecond is
400  // used to test comparison in all field positions.
401  TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
402  absl::CivilSecond(2015, 1, 1, 0, 0, 0));
403  TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
404  absl::CivilSecond(2014, 2, 1, 0, 0, 0));
405  TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
406  absl::CivilSecond(2014, 1, 2, 0, 0, 0));
407  TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 0, 0, 0),
408  absl::CivilSecond(2014, 1, 1, 1, 0, 0));
409  TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 0, 0),
410  absl::CivilSecond(2014, 1, 1, 1, 1, 0));
411  TEST_RELATIONAL(absl::CivilSecond(2014, 1, 1, 1, 1, 0),
412  absl::CivilSecond(2014, 1, 1, 1, 1, 1));
413 
414  // Tests the relational operators of two different civil-time types.
415  TEST_RELATIONAL(absl::CivilDay(2014, 1, 1),
416  absl::CivilMinute(2014, 1, 1, 1, 1));
417  TEST_RELATIONAL(absl::CivilDay(2014, 1, 1),
418  absl::CivilMonth(2014, 2));
419 
420 #undef TEST_RELATIONAL
421 }
422 
423 TEST(CivilTime, Arithmetic) {
424  absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
425  EXPECT_EQ("2015-01-02T03:04:06", absl::FormatCivilTime(second += 1));
426  EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second + 1));
427  EXPECT_EQ("2015-01-02T03:04:08", absl::FormatCivilTime(2 + second));
428  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second - 1));
429  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second -= 1));
430  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(second++));
431  EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(++second));
432  EXPECT_EQ("2015-01-02T03:04:07", absl::FormatCivilTime(second--));
433  EXPECT_EQ("2015-01-02T03:04:05", absl::FormatCivilTime(--second));
434 
435  absl::CivilMinute minute(2015, 1, 2, 3, 4);
436  EXPECT_EQ("2015-01-02T03:05", absl::FormatCivilTime(minute += 1));
437  EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute + 1));
438  EXPECT_EQ("2015-01-02T03:07", absl::FormatCivilTime(2 + minute));
439  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute - 1));
440  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute -= 1));
441  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(minute++));
442  EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(++minute));
443  EXPECT_EQ("2015-01-02T03:06", absl::FormatCivilTime(minute--));
444  EXPECT_EQ("2015-01-02T03:04", absl::FormatCivilTime(--minute));
445 
446  absl::CivilHour hour(2015, 1, 2, 3);
447  EXPECT_EQ("2015-01-02T04", absl::FormatCivilTime(hour += 1));
448  EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour + 1));
449  EXPECT_EQ("2015-01-02T06", absl::FormatCivilTime(2 + hour));
450  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour - 1));
451  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour -= 1));
452  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(hour++));
453  EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(++hour));
454  EXPECT_EQ("2015-01-02T05", absl::FormatCivilTime(hour--));
455  EXPECT_EQ("2015-01-02T03", absl::FormatCivilTime(--hour));
456 
457  absl::CivilDay day(2015, 1, 2);
458  EXPECT_EQ("2015-01-03", absl::FormatCivilTime(day += 1));
459  EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day + 1));
460  EXPECT_EQ("2015-01-05", absl::FormatCivilTime(2 + day));
461  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day - 1));
462  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day -= 1));
463  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(day++));
464  EXPECT_EQ("2015-01-04", absl::FormatCivilTime(++day));
465  EXPECT_EQ("2015-01-04", absl::FormatCivilTime(day--));
466  EXPECT_EQ("2015-01-02", absl::FormatCivilTime(--day));
467 
468  absl::CivilMonth month(2015, 1);
469  EXPECT_EQ("2015-02", absl::FormatCivilTime(month += 1));
470  EXPECT_EQ("2015-03", absl::FormatCivilTime(month + 1));
471  EXPECT_EQ("2015-04", absl::FormatCivilTime(2 + month));
472  EXPECT_EQ("2015-01", absl::FormatCivilTime(month - 1));
473  EXPECT_EQ("2015-01", absl::FormatCivilTime(month -= 1));
474  EXPECT_EQ("2015-01", absl::FormatCivilTime(month++));
475  EXPECT_EQ("2015-03", absl::FormatCivilTime(++month));
476  EXPECT_EQ("2015-03", absl::FormatCivilTime(month--));
477  EXPECT_EQ("2015-01", absl::FormatCivilTime(--month));
478 
479  absl::CivilYear year(2015);
480  EXPECT_EQ("2016", absl::FormatCivilTime(year += 1));
481  EXPECT_EQ("2017", absl::FormatCivilTime(year + 1));
482  EXPECT_EQ("2018", absl::FormatCivilTime(2 + year));
483  EXPECT_EQ("2015", absl::FormatCivilTime(year - 1));
484  EXPECT_EQ("2015", absl::FormatCivilTime(year -= 1));
485  EXPECT_EQ("2015", absl::FormatCivilTime(year++));
486  EXPECT_EQ("2017", absl::FormatCivilTime(++year));
487  EXPECT_EQ("2017", absl::FormatCivilTime(year--));
488  EXPECT_EQ("2015", absl::FormatCivilTime(--year));
489 }
490 
491 TEST(CivilTime, ArithmeticLimits) {
492  const int kIntMax = std::numeric_limits<int>::max();
493  const int kIntMin = std::numeric_limits<int>::min();
494 
495  absl::CivilSecond second(1970, 1, 1, 0, 0, 0);
496  second += kIntMax;
497  EXPECT_EQ("2038-01-19T03:14:07", absl::FormatCivilTime(second));
498  second -= kIntMax;
499  EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second));
500  second += kIntMin;
501  EXPECT_EQ("1901-12-13T20:45:52", absl::FormatCivilTime(second));
502  second -= kIntMin;
503  EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(second));
504 
505  absl::CivilMinute minute(1970, 1, 1, 0, 0);
506  minute += kIntMax;
507  EXPECT_EQ("6053-01-23T02:07", absl::FormatCivilTime(minute));
508  minute -= kIntMax;
509  EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute));
510  minute += kIntMin;
511  EXPECT_EQ("-2114-12-08T21:52", absl::FormatCivilTime(minute));
512  minute -= kIntMin;
513  EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(minute));
514 
515  absl::CivilHour hour(1970, 1, 1, 0);
516  hour += kIntMax;
517  EXPECT_EQ("246953-10-09T07", absl::FormatCivilTime(hour));
518  hour -= kIntMax;
519  EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour));
520  hour += kIntMin;
521  EXPECT_EQ("-243014-03-24T16", absl::FormatCivilTime(hour));
522  hour -= kIntMin;
523  EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hour));
524 
525  absl::CivilDay day(1970, 1, 1);
526  day += kIntMax;
527  EXPECT_EQ("5881580-07-11", absl::FormatCivilTime(day));
528  day -= kIntMax;
529  EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day));
530  day += kIntMin;
531  EXPECT_EQ("-5877641-06-23", absl::FormatCivilTime(day));
532  day -= kIntMin;
533  EXPECT_EQ("1970-01-01", absl::FormatCivilTime(day));
534 
535  absl::CivilMonth month(1970, 1);
536  month += kIntMax;
537  EXPECT_EQ("178958940-08", absl::FormatCivilTime(month));
538  month -= kIntMax;
539  EXPECT_EQ("1970-01", absl::FormatCivilTime(month));
540  month += kIntMin;
541  EXPECT_EQ("-178955001-05", absl::FormatCivilTime(month));
542  month -= kIntMin;
543  EXPECT_EQ("1970-01", absl::FormatCivilTime(month));
544 
545  absl::CivilYear year(0);
546  year += kIntMax;
547  EXPECT_EQ("2147483647", absl::FormatCivilTime(year));
548  year -= kIntMax;
549  EXPECT_EQ("0", absl::FormatCivilTime(year));
550  year += kIntMin;
551  EXPECT_EQ("-2147483648", absl::FormatCivilTime(year));
552  year -= kIntMin;
553  EXPECT_EQ("0", absl::FormatCivilTime(year));
554 }
555 
556 TEST(CivilTime, Difference) {
557  absl::CivilSecond second(2015, 1, 2, 3, 4, 5);
558  EXPECT_EQ(0, second - second);
559  EXPECT_EQ(10, (second + 10) - second);
560  EXPECT_EQ(-10, (second - 10) - second);
561 
562  absl::CivilMinute minute(2015, 1, 2, 3, 4);
563  EXPECT_EQ(0, minute - minute);
564  EXPECT_EQ(10, (minute + 10) - minute);
565  EXPECT_EQ(-10, (minute - 10) - minute);
566 
567  absl::CivilHour hour(2015, 1, 2, 3);
568  EXPECT_EQ(0, hour - hour);
569  EXPECT_EQ(10, (hour + 10) - hour);
570  EXPECT_EQ(-10, (hour - 10) - hour);
571 
572  absl::CivilDay day(2015, 1, 2);
573  EXPECT_EQ(0, day - day);
574  EXPECT_EQ(10, (day + 10) - day);
575  EXPECT_EQ(-10, (day - 10) - day);
576 
577  absl::CivilMonth month(2015, 1);
578  EXPECT_EQ(0, month - month);
579  EXPECT_EQ(10, (month + 10) - month);
580  EXPECT_EQ(-10, (month - 10) - month);
581 
582  absl::CivilYear year(2015);
583  EXPECT_EQ(0, year - year);
584  EXPECT_EQ(10, (year + 10) - year);
585  EXPECT_EQ(-10, (year - 10) - year);
586 }
587 
588 TEST(CivilTime, DifferenceLimits) {
589  const absl::civil_diff_t kDiffMax =
590  std::numeric_limits<absl::civil_diff_t>::max();
591  const absl::civil_diff_t kDiffMin =
592  std::numeric_limits<absl::civil_diff_t>::min();
593 
594  // Check day arithmetic at the end of the year range.
595  const absl::CivilDay max_day(kDiffMax, 12, 31);
596  EXPECT_EQ(1, max_day - (max_day - 1));
597  EXPECT_EQ(-1, (max_day - 1) - max_day);
598 
599  // Check day arithmetic at the start of the year range.
600  const absl::CivilDay min_day(kDiffMin, 1, 1);
601  EXPECT_EQ(1, (min_day + 1) - min_day);
602  EXPECT_EQ(-1, min_day - (min_day + 1));
603 
604  // Check the limits of the return value.
605  const absl::CivilDay d1(1970, 1, 1);
606  const absl::CivilDay d2(25252734927768524, 7, 27);
607  EXPECT_EQ(kDiffMax, d2 - d1);
608  EXPECT_EQ(kDiffMin, d1 - (d2 + 1));
609 }
610 
611 TEST(CivilTime, Properties) {
612  absl::CivilSecond ss(2015, 2, 3, 4, 5, 6);
613  EXPECT_EQ(2015, ss.year());
614  EXPECT_EQ(2, ss.month());
615  EXPECT_EQ(3, ss.day());
616  EXPECT_EQ(4, ss.hour());
617  EXPECT_EQ(5, ss.minute());
618  EXPECT_EQ(6, ss.second());
619 
620  absl::CivilMinute mm(2015, 2, 3, 4, 5, 6);
621  EXPECT_EQ(2015, mm.year());
622  EXPECT_EQ(2, mm.month());
623  EXPECT_EQ(3, mm.day());
624  EXPECT_EQ(4, mm.hour());
625  EXPECT_EQ(5, mm.minute());
626  EXPECT_EQ(0, mm.second());
627 
628  absl::CivilHour hh(2015, 2, 3, 4, 5, 6);
629  EXPECT_EQ(2015, hh.year());
630  EXPECT_EQ(2, hh.month());
631  EXPECT_EQ(3, hh.day());
632  EXPECT_EQ(4, hh.hour());
633  EXPECT_EQ(0, hh.minute());
634  EXPECT_EQ(0, hh.second());
635 
636  absl::CivilDay d(2015, 2, 3, 4, 5, 6);
637  EXPECT_EQ(2015, d.year());
638  EXPECT_EQ(2, d.month());
639  EXPECT_EQ(3, d.day());
640  EXPECT_EQ(0, d.hour());
641  EXPECT_EQ(0, d.minute());
642  EXPECT_EQ(0, d.second());
643 
644  absl::CivilMonth m(2015, 2, 3, 4, 5, 6);
645  EXPECT_EQ(2015, m.year());
646  EXPECT_EQ(2, m.month());
647  EXPECT_EQ(1, m.day());
648  EXPECT_EQ(0, m.hour());
649  EXPECT_EQ(0, m.minute());
650  EXPECT_EQ(0, m.second());
651 
652  absl::CivilYear y(2015, 2, 3, 4, 5, 6);
653  EXPECT_EQ(2015, y.year());
654  EXPECT_EQ(1, y.month());
655  EXPECT_EQ(1, y.day());
656  EXPECT_EQ(0, y.hour());
657  EXPECT_EQ(0, y.minute());
658  EXPECT_EQ(0, y.second());
659 }
660 
661 TEST(CivilTime, Format) {
663  EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
664 
666  EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
667 
668  absl::CivilHour hh;
669  EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
670 
671  absl::CivilDay d;
672  EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
673 
675  EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
676 
677  absl::CivilYear y;
678  EXPECT_EQ("1970", absl::FormatCivilTime(y));
679 }
680 
681 TEST(CivilTime, FormatAndParseLenient) {
683  EXPECT_EQ("1970-01-01T00:00:00", absl::FormatCivilTime(ss));
684 
686  EXPECT_EQ("1970-01-01T00:00", absl::FormatCivilTime(mm));
687 
688  absl::CivilHour hh;
689  EXPECT_EQ("1970-01-01T00", absl::FormatCivilTime(hh));
690 
691  absl::CivilDay d;
692  EXPECT_EQ("1970-01-01", absl::FormatCivilTime(d));
693 
695  EXPECT_EQ("1970-01", absl::FormatCivilTime(m));
696 
697  absl::CivilYear y;
698  EXPECT_EQ("1970", absl::FormatCivilTime(y));
699 }
700 
701 TEST(CivilTime, OutputStream) {
702  absl::CivilSecond cs(2016, 2, 3, 4, 5, 6);
703  {
704  std::stringstream ss;
705  ss << std::left << std::setfill('.');
706  ss << std::setw(3) << 'X';
707  ss << std::setw(21) << absl::CivilYear(cs);
708  ss << std::setw(3) << 'X';
709  EXPECT_EQ("X..2016.................X..", ss.str());
710  }
711  {
712  std::stringstream ss;
713  ss << std::left << std::setfill('.');
714  ss << std::setw(3) << 'X';
715  ss << std::setw(21) << absl::CivilMonth(cs);
716  ss << std::setw(3) << 'X';
717  EXPECT_EQ("X..2016-02..............X..", ss.str());
718  }
719  {
720  std::stringstream ss;
721  ss << std::left << std::setfill('.');
722  ss << std::setw(3) << 'X';
723  ss << std::setw(21) << absl::CivilDay(cs);
724  ss << std::setw(3) << 'X';
725  EXPECT_EQ("X..2016-02-03...........X..", ss.str());
726  }
727  {
728  std::stringstream ss;
729  ss << std::left << std::setfill('.');
730  ss << std::setw(3) << 'X';
731  ss << std::setw(21) << absl::CivilHour(cs);
732  ss << std::setw(3) << 'X';
733  EXPECT_EQ("X..2016-02-03T04........X..", ss.str());
734  }
735  {
736  std::stringstream ss;
737  ss << std::left << std::setfill('.');
738  ss << std::setw(3) << 'X';
739  ss << std::setw(21) << absl::CivilMinute(cs);
740  ss << std::setw(3) << 'X';
741  EXPECT_EQ("X..2016-02-03T04:05.....X..", ss.str());
742  }
743  {
744  std::stringstream ss;
745  ss << std::left << std::setfill('.');
746  ss << std::setw(3) << 'X';
747  ss << std::setw(21) << absl::CivilSecond(cs);
748  ss << std::setw(3) << 'X';
749  EXPECT_EQ("X..2016-02-03T04:05:06..X..", ss.str());
750  }
751  {
752  std::stringstream ss;
753  ss << std::left << std::setfill('.');
754  ss << std::setw(3) << 'X';
755  ss << std::setw(21) << absl::Weekday::wednesday;
756  ss << std::setw(3) << 'X';
757  EXPECT_EQ("X..Wednesday............X..", ss.str());
758  }
759 }
760 
761 TEST(CivilTime, Weekday) {
762  absl::CivilDay d(1970, 1, 1);
763  EXPECT_EQ(absl::Weekday::thursday, absl::GetWeekday(d)) << d;
764 
765  // We used to get this wrong for years < -30.
766  d = absl::CivilDay(-31, 12, 24);
767  EXPECT_EQ(absl::Weekday::wednesday, absl::GetWeekday(d)) << d;
768 }
769 
770 TEST(CivilTime, NextPrevWeekday) {
771  // Jan 1, 1970 was a Thursday.
772  const absl::CivilDay thursday(1970, 1, 1);
773 
774  // Thursday -> Thursday
775  absl::CivilDay d = absl::NextWeekday(thursday, absl::Weekday::thursday);
776  EXPECT_EQ(7, d - thursday) << d;
777  EXPECT_EQ(d - 14, absl::PrevWeekday(thursday, absl::Weekday::thursday));
778 
779  // Thursday -> Friday
780  d = absl::NextWeekday(thursday, absl::Weekday::friday);
781  EXPECT_EQ(1, d - thursday) << d;
782  EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::friday));
783 
784  // Thursday -> Saturday
785  d = absl::NextWeekday(thursday, absl::Weekday::saturday);
786  EXPECT_EQ(2, d - thursday) << d;
787  EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::saturday));
788 
789  // Thursday -> Sunday
790  d = absl::NextWeekday(thursday, absl::Weekday::sunday);
791  EXPECT_EQ(3, d - thursday) << d;
792  EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::sunday));
793 
794  // Thursday -> Monday
795  d = absl::NextWeekday(thursday, absl::Weekday::monday);
796  EXPECT_EQ(4, d - thursday) << d;
797  EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::monday));
798 
799  // Thursday -> Tuesday
800  d = absl::NextWeekday(thursday, absl::Weekday::tuesday);
801  EXPECT_EQ(5, d - thursday) << d;
802  EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::tuesday));
803 
804  // Thursday -> Wednesday
805  d = absl::NextWeekday(thursday, absl::Weekday::wednesday);
806  EXPECT_EQ(6, d - thursday) << d;
807  EXPECT_EQ(d - 7, absl::PrevWeekday(thursday, absl::Weekday::wednesday));
808 }
809 
810 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
811 TEST(CivilTime, DifferenceWithHugeYear) {
812  absl::CivilDay d1(9223372036854775807, 1, 1);
813  absl::CivilDay d2(9223372036854775807, 12, 31);
814  EXPECT_EQ(364, d2 - d1);
815 
816  d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1);
817  d2 = absl::CivilDay(-9223372036854775807 - 1, 12, 31);
818  EXPECT_EQ(365, d2 - d1);
819 
820  // Check the limits of the return value at the end of the year range.
821  d1 = absl::CivilDay(9223372036854775807, 1, 1);
822  d2 = absl::CivilDay(9198119301927009252, 6, 6);
823  EXPECT_EQ(9223372036854775807, d1 - d2);
824  d2 = d2 - 1;
825  EXPECT_EQ(-9223372036854775807 - 1, d2 - d1);
826 
827  // Check the limits of the return value at the start of the year range.
828  d1 = absl::CivilDay(-9223372036854775807 - 1, 1, 1);
829  d2 = absl::CivilDay(-9198119301927009254, 7, 28);
830  EXPECT_EQ(9223372036854775807, d2 - d1);
831  d2 = d2 + 1;
832  EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
833 
834  // Check the limits of the return value from either side of year 0.
835  d1 = absl::CivilDay(-12626367463883278, 9, 3);
836  d2 = absl::CivilDay(12626367463883277, 3, 28);
837  EXPECT_EQ(9223372036854775807, d2 - d1);
838  d2 = d2 + 1;
839  EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
840 }
841 
842 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
843 TEST(CivilTime, DifferenceNoIntermediateOverflow) {
844  // The difference up to the minute field would be below the minimum
845  // int64_t, but the 52 extra seconds brings us back to the minimum.
846  absl::CivilSecond s1(-292277022657, 1, 27, 8, 29 - 1, 52);
847  absl::CivilSecond s2(1970, 1, 1, 0, 0 - 1, 0);
848  EXPECT_EQ(-9223372036854775807 - 1, s1 - s2);
849 
850  // The difference up to the minute field would be above the maximum
851  // int64_t, but the -53 extra seconds brings us back to the maximum.
852  s1 = absl::CivilSecond(292277026596, 12, 4, 15, 30, 7 - 7);
853  s2 = absl::CivilSecond(1970, 1, 1, 0, 0, 0 - 7);
854  EXPECT_EQ(9223372036854775807, s1 - s2);
855 }
856 
857 TEST(CivilTime, NormalizeSimpleOverflow) {
859  cs = absl::CivilSecond(2013, 11, 15, 16, 32, 59 + 1);
860  EXPECT_EQ("2013-11-15T16:33:00", absl::FormatCivilTime(cs));
861  cs = absl::CivilSecond(2013, 11, 15, 16, 59 + 1, 14);
862  EXPECT_EQ("2013-11-15T17:00:14", absl::FormatCivilTime(cs));
863  cs = absl::CivilSecond(2013, 11, 15, 23 + 1, 32, 14);
864  EXPECT_EQ("2013-11-16T00:32:14", absl::FormatCivilTime(cs));
865  cs = absl::CivilSecond(2013, 11, 30 + 1, 16, 32, 14);
866  EXPECT_EQ("2013-12-01T16:32:14", absl::FormatCivilTime(cs));
867  cs = absl::CivilSecond(2013, 12 + 1, 15, 16, 32, 14);
868  EXPECT_EQ("2014-01-15T16:32:14", absl::FormatCivilTime(cs));
869 }
870 
871 TEST(CivilTime, NormalizeSimpleUnderflow) {
873  cs = absl::CivilSecond(2013, 11, 15, 16, 32, 0 - 1);
874  EXPECT_EQ("2013-11-15T16:31:59", absl::FormatCivilTime(cs));
875  cs = absl::CivilSecond(2013, 11, 15, 16, 0 - 1, 14);
876  EXPECT_EQ("2013-11-15T15:59:14", absl::FormatCivilTime(cs));
877  cs = absl::CivilSecond(2013, 11, 15, 0 - 1, 32, 14);
878  EXPECT_EQ("2013-11-14T23:32:14", absl::FormatCivilTime(cs));
879  cs = absl::CivilSecond(2013, 11, 1 - 1, 16, 32, 14);
880  EXPECT_EQ("2013-10-31T16:32:14", absl::FormatCivilTime(cs));
881  cs = absl::CivilSecond(2013, 1 - 1, 15, 16, 32, 14);
882  EXPECT_EQ("2012-12-15T16:32:14", absl::FormatCivilTime(cs));
883 }
884 
885 TEST(CivilTime, NormalizeMultipleOverflow) {
886  absl::CivilSecond cs(2013, 12, 31, 23, 59, 59 + 1);
887  EXPECT_EQ("2014-01-01T00:00:00", absl::FormatCivilTime(cs));
888 }
889 
890 TEST(CivilTime, NormalizeMultipleUnderflow) {
891  absl::CivilSecond cs(2014, 1, 1, 0, 0, 0 - 1);
892  EXPECT_EQ("2013-12-31T23:59:59", absl::FormatCivilTime(cs));
893 }
894 
895 TEST(CivilTime, NormalizeOverflowLimits) {
897 
898  const int kintmax = std::numeric_limits<int>::max();
899  cs = absl::CivilSecond(0, kintmax, kintmax, kintmax, kintmax, kintmax);
900  EXPECT_EQ("185085715-11-27T12:21:07", absl::FormatCivilTime(cs));
901 
902  const int kintmin = std::numeric_limits<int>::min();
903  cs = absl::CivilSecond(0, kintmin, kintmin, kintmin, kintmin, kintmin);
904  EXPECT_EQ("-185085717-10-31T10:37:52", absl::FormatCivilTime(cs));
905 }
906 
907 TEST(CivilTime, NormalizeComplexOverflow) {
909  cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 + 123456789);
910  EXPECT_EQ("2017-10-14T14:05:23", absl::FormatCivilTime(cs));
911  cs = absl::CivilSecond(2013, 11, 15, 16, 32 + 1234567, 14);
912  EXPECT_EQ("2016-03-22T00:39:14", absl::FormatCivilTime(cs));
913  cs = absl::CivilSecond(2013, 11, 15, 16 + 123456, 32, 14);
914  EXPECT_EQ("2027-12-16T16:32:14", absl::FormatCivilTime(cs));
915  cs = absl::CivilSecond(2013, 11, 15 + 1234, 16, 32, 14);
916  EXPECT_EQ("2017-04-02T16:32:14", absl::FormatCivilTime(cs));
917  cs = absl::CivilSecond(2013, 11 + 123, 15, 16, 32, 14);
918  EXPECT_EQ("2024-02-15T16:32:14", absl::FormatCivilTime(cs));
919 }
920 
921 TEST(CivilTime, NormalizeComplexUnderflow) {
923  cs = absl::CivilSecond(1999, 3, 0, 0, 0, 0); // year 400
924  EXPECT_EQ("1999-02-28T00:00:00", absl::FormatCivilTime(cs));
925  cs = absl::CivilSecond(2013, 11, 15, 16, 32, 14 - 123456789);
926  EXPECT_EQ("2009-12-17T18:59:05", absl::FormatCivilTime(cs));
927  cs = absl::CivilSecond(2013, 11, 15, 16, 32 - 1234567, 14);
928  EXPECT_EQ("2011-07-12T08:25:14", absl::FormatCivilTime(cs));
929  cs = absl::CivilSecond(2013, 11, 15, 16 - 123456, 32, 14);
930  EXPECT_EQ("1999-10-16T16:32:14", absl::FormatCivilTime(cs));
931  cs = absl::CivilSecond(2013, 11, 15 - 1234, 16, 32, 14);
932  EXPECT_EQ("2010-06-30T16:32:14", absl::FormatCivilTime(cs));
933  cs = absl::CivilSecond(2013, 11 - 123, 15, 16, 32, 14);
934  EXPECT_EQ("2003-08-15T16:32:14", absl::FormatCivilTime(cs));
935 }
936 
937 TEST(CivilTime, NormalizeMishmash) {
939  cs = absl::CivilSecond(2013, 11 - 123, 15 + 1234, 16 - 123456, 32 + 1234567,
940  14 - 123456789);
941  EXPECT_EQ("1991-05-09T03:06:05", absl::FormatCivilTime(cs));
942  cs = absl::CivilSecond(2013, 11 + 123, 15 - 1234, 16 + 123456, 32 - 1234567,
943  14 + 123456789);
944  EXPECT_EQ("2036-05-24T05:58:23", absl::FormatCivilTime(cs));
945 
946  cs = absl::CivilSecond(2013, 11, -146097 + 1, 16, 32, 14);
947  EXPECT_EQ("1613-11-01T16:32:14", absl::FormatCivilTime(cs));
948  cs = absl::CivilSecond(2013, 11 + 400 * 12, -146097 + 1, 16, 32, 14);
949  EXPECT_EQ("2013-11-01T16:32:14", absl::FormatCivilTime(cs));
950 }
951 
952 // Convert all the days from 1970-1-1 to 1970-1-146097 (aka 2369-12-31)
953 // and check that they normalize to the expected time. 146097 days span
954 // the 400-year Gregorian cycle used during normalization.
955 TEST(CivilTime, NormalizeAllTheDays) {
956  absl::CivilDay expected(1970, 1, 1);
957  for (int day = 1; day <= 146097; ++day) {
958  absl::CivilSecond cs(1970, 1, day, 0, 0, 0);
959  EXPECT_EQ(expected, cs);
960  ++expected;
961  }
962 }
963 
964 TEST(CivilTime, NormalizeWithHugeYear) {
965  absl::CivilMonth c(9223372036854775807, 1);
966  EXPECT_EQ("9223372036854775807-01", absl::FormatCivilTime(c));
967  c = c - 1; // Causes normalization
968  EXPECT_EQ("9223372036854775806-12", absl::FormatCivilTime(c));
969 
970  c = absl::CivilMonth(-9223372036854775807 - 1, 1);
971  EXPECT_EQ("-9223372036854775808-01", absl::FormatCivilTime(c));
972  c = c + 12; // Causes normalization
973  EXPECT_EQ("-9223372036854775807-01", absl::FormatCivilTime(c));
974 }
975 
976 TEST(CivilTime, LeapYears) {
977  const absl::CivilSecond s1(2013, 2, 28 + 1, 0, 0, 0);
978  EXPECT_EQ("2013-03-01T00:00:00", absl::FormatCivilTime(s1));
979 
980  const absl::CivilSecond s2(2012, 2, 28 + 1, 0, 0, 0);
981  EXPECT_EQ("2012-02-29T00:00:00", absl::FormatCivilTime(s2));
982 
983  const absl::CivilSecond s3(1900, 2, 28 + 1, 0, 0, 0);
984  EXPECT_EQ("1900-03-01T00:00:00", absl::FormatCivilTime(s3));
985 
986  const struct {
987  int year;
988  int days;
989  struct {
990  int month;
991  int day;
992  } leap_day; // The date of the day after Feb 28.
993  } kLeapYearTable[]{
994  {1900, 365, {3, 1}},
995  {1999, 365, {3, 1}},
996  {2000, 366, {2, 29}}, // leap year
997  {2001, 365, {3, 1}},
998  {2002, 365, {3, 1}},
999  {2003, 365, {3, 1}},
1000  {2004, 366, {2, 29}}, // leap year
1001  {2005, 365, {3, 1}},
1002  {2006, 365, {3, 1}},
1003  {2007, 365, {3, 1}},
1004  {2008, 366, {2, 29}}, // leap year
1005  {2009, 365, {3, 1}},
1006  {2100, 365, {3, 1}},
1007  };
1008 
1009  for (int i = 0; i < ABSL_ARRAYSIZE(kLeapYearTable); ++i) {
1010  const int y = kLeapYearTable[i].year;
1011  const int m = kLeapYearTable[i].leap_day.month;
1012  const int d = kLeapYearTable[i].leap_day.day;
1013  const int n = kLeapYearTable[i].days;
1014 
1015  // Tests incrementing through the leap day.
1016  const absl::CivilDay feb28(y, 2, 28);
1017  const absl::CivilDay next_day = feb28 + 1;
1018  EXPECT_EQ(m, next_day.month());
1019  EXPECT_EQ(d, next_day.day());
1020 
1021  // Tests difference in days of leap years.
1022  const absl::CivilYear year(feb28);
1023  const absl::CivilYear next_year = year + 1;
1024  EXPECT_EQ(n, absl::CivilDay(next_year) - absl::CivilDay(year));
1025  }
1026 }
1027 
1028 TEST(CivilTime, FirstThursdayInMonth) {
1029  const absl::CivilDay nov1(2014, 11, 1);
1030  const absl::CivilDay thursday =
1031  absl::PrevWeekday(nov1, absl::Weekday::thursday) + 7;
1032  EXPECT_EQ("2014-11-06", absl::FormatCivilTime(thursday));
1033 
1034  // Bonus: Date of Thanksgiving in the United States
1035  // Rule: Fourth Thursday of November
1036  const absl::CivilDay thanksgiving = thursday + 7 * 3;
1037  EXPECT_EQ("2014-11-27", absl::FormatCivilTime(thanksgiving));
1038 }
1039 
1040 TEST(CivilTime, DocumentationExample) {
1041  absl::CivilSecond second(2015, 6, 28, 1, 2, 3); // 2015-06-28 01:02:03
1042  absl::CivilMinute minute(second); // 2015-06-28 01:02:00
1043  absl::CivilDay day(minute); // 2015-06-28 00:00:00
1044 
1045  second -= 1; // 2015-06-28 01:02:02
1046  --second; // 2015-06-28 01:02:01
1047  EXPECT_EQ(minute, second - 1); // Comparison between types
1048  EXPECT_LT(minute, second);
1049 
1050  // int diff = second - minute; // ERROR: Mixed types, won't compile
1051 
1052  absl::CivilDay june_1(2015, 6, 1); // Pass fields to c'tor.
1053  int diff = day - june_1; // Num days between 'day' and June 1
1054  EXPECT_EQ(27, diff);
1055 
1056  // Fields smaller than alignment are floored to their minimum value.
1057  absl::CivilDay day_floor(2015, 1, 2, 9, 9, 9);
1058  EXPECT_EQ(0, day_floor.hour()); // 09:09:09 is floored
1059  EXPECT_EQ(absl::CivilDay(2015, 1, 2), day_floor);
1060 
1061  // Unspecified fields default to their minium value
1062  absl::CivilDay day_default(2015); // Defaults to Jan 1
1063  EXPECT_EQ(absl::CivilDay(2015, 1, 1), day_default);
1064 
1065  // Iterates all the days of June.
1066  absl::CivilMonth june(day); // CivilDay -> CivilMonth
1067  absl::CivilMonth july = june + 1;
1068  for (absl::CivilDay day = june_1; day < july; ++day) {
1069  // ...
1070  }
1071 }
1072 
1073 } // namespace
Weekday GetWeekday(CivilDay cd)
Definition: civil_time.h:380
CONSTEXPR_M int minute() const noexcept
time_internal::cctz::year_t civil_year_t
Definition: civil_time.h:346
time_internal::cctz::detail::civil_time< time_internal::year_tag > CivilYear
Definition: civil_time.h:333
time_internal::cctz::diff_t civil_diff_t
Definition: civil_time.h:359
CivilDay NextWeekday(CivilDay cd, Weekday wd)
Definition: civil_time.h:414
time_internal::cctz::detail::civil_time< time_internal::hour_tag > CivilHour
Definition: civil_time.h:327
static CONSTEXPR_F civil_time() max()
static CONSTEXPR_F civil_time() min()
CONSTEXPR_M year_t year() const noexcept
time_internal::cctz::detail::civil_time< time_internal::month_tag > CivilMonth
Definition: civil_time.h:331
size_t value
time_internal::cctz::weekday Weekday
Definition: civil_time.h:369
time_internal::cctz::detail::civil_time< time_internal::second_tag > CivilSecond
Definition: civil_time.h:323
CONSTEXPR_M int month() const noexcept
#define TEST_RELATIONAL(OLDER, YOUNGER)
time_internal::cctz::detail::civil_time< time_internal::minute_tag > CivilMinute
Definition: civil_time.h:325
time_internal::cctz::detail::civil_time< time_internal::day_tag > CivilDay
Definition: civil_time.h:329
#define ABSL_ARRAYSIZE(array)
Definition: macros.h:42
CONSTEXPR_M int second() const noexcept
bool Format(FormatRawSink raw_sink, const FormatSpec< Args... > &format, const Args &...args)
Definition: str_format.h:454
TEST(Symbolize, Unimplemented)
uint64_t b
Definition: layout_test.cc:50
std::string FormatCivilTime(CivilSecond c)
Definition: civil_time.cc:47
CivilDay PrevWeekday(CivilDay cd, Weekday wd)
Definition: civil_time.h:417


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