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,DifferenceWithHugeYear)238 TEST(CivilTime, DifferenceWithHugeYear) {
239 {
240 constexpr civil_day d1(9223372036854775807, 1, 1);
241 constexpr civil_day d2(9223372036854775807, 12, 31);
242 static_assert(d2 - d1 == 364, "DifferenceWithHugeYear");
243 }
244 {
245 constexpr civil_day d1(-9223372036854775807 - 1, 1, 1);
246 constexpr civil_day d2(-9223372036854775807 - 1, 12, 31);
247 static_assert(d2 - d1 == 365, "DifferenceWithHugeYear");
248 }
249 {
250 // Check the limits of the return value at the end of the year range.
251 constexpr civil_day d1(9223372036854775807, 1, 1);
252 constexpr civil_day d2(9198119301927009252, 6, 6);
253 static_assert(d1 - d2 == 9223372036854775807, "DifferenceWithHugeYear");
254 static_assert((d2 - 1) - d1 == -9223372036854775807 - 1,
255 "DifferenceWithHugeYear");
256 }
257 {
258 // Check the limits of the return value at the start of the year range.
259 constexpr civil_day d1(-9223372036854775807 - 1, 1, 1);
260 constexpr civil_day d2(-9198119301927009254, 7, 28);
261 static_assert(d2 - d1 == 9223372036854775807, "DifferenceWithHugeYear");
262 static_assert(d1 - (d2 + 1) == -9223372036854775807 - 1,
263 "DifferenceWithHugeYear");
264 }
265 {
266 // Check the limits of the return value from either side of year 0.
267 constexpr civil_day d1(-12626367463883278, 9, 3);
268 constexpr civil_day d2(12626367463883277, 3, 28);
269 static_assert(d2 - d1 == 9223372036854775807, "DifferenceWithHugeYear");
270 static_assert(d1 - (d2 + 1) == -9223372036854775807 - 1,
271 "DifferenceWithHugeYear");
272 }
273 }
274
275 // NOTE: Run this with --copt=-ftrapv to detect overflow problems.
TEST(CivilTime,DifferenceNoIntermediateOverflow)276 TEST(CivilTime, DifferenceNoIntermediateOverflow) {
277 {
278 // The difference up to the minute field would be below the minimum
279 // diff_t, but the 52 extra seconds brings us back to the minimum.
280 constexpr civil_second s1(-292277022657, 1, 27, 8, 29 - 1, 52);
281 constexpr civil_second s2(1970, 1, 1, 0, 0 - 1, 0);
282 static_assert(s1 - s2 == -9223372036854775807 - 1,
283 "DifferenceNoIntermediateOverflow");
284 }
285 {
286 // The difference up to the minute field would be above the maximum
287 // diff_t, but the -53 extra seconds brings us back to the maximum.
288 constexpr civil_second s1(292277026596, 12, 4, 15, 30, 7 - 7);
289 constexpr civil_second s2(1970, 1, 1, 0, 0, 0 - 7);
290 static_assert(s1 - s2 == 9223372036854775807,
291 "DifferenceNoIntermediateOverflow");
292 }
293 }
294
295 // Helper constexpr tests
296
TEST(CivilTime,WeekDay)297 TEST(CivilTime, WeekDay) {
298 constexpr civil_day cd(2016, 1, 28);
299 constexpr weekday wd = get_weekday(cd);
300 static_assert(wd == weekday::thursday, "Weekday");
301 }
302
TEST(CivilTime,NextWeekDay)303 TEST(CivilTime, NextWeekDay) {
304 constexpr civil_day cd(2016, 1, 28);
305 constexpr civil_day next = next_weekday(cd, weekday::thursday);
306 static_assert(next.year() == 2016, "NextWeekDay.year");
307 static_assert(next.month() == 2, "NextWeekDay.month");
308 static_assert(next.day() == 4, "NextWeekDay.day");
309 }
310
TEST(CivilTime,PrevWeekDay)311 TEST(CivilTime, PrevWeekDay) {
312 constexpr civil_day cd(2016, 1, 28);
313 constexpr civil_day prev = prev_weekday(cd, weekday::thursday);
314 static_assert(prev.year() == 2016, "PrevWeekDay.year");
315 static_assert(prev.month() == 1, "PrevWeekDay.month");
316 static_assert(prev.day() == 21, "PrevWeekDay.day");
317 }
318
TEST(CivilTime,YearDay)319 TEST(CivilTime, YearDay) {
320 constexpr civil_day cd(2016, 1, 28);
321 constexpr int yd = get_yearday(cd);
322 static_assert(yd == 28, "YearDay");
323 }
324 #endif // __cpp_constexpr >= 201304 || (defined(_MSC_VER) && _MSC_VER >= 1910)
325
326 // The remaining tests do not use constexpr.
327
TEST(CivilTime,DefaultConstruction)328 TEST(CivilTime, DefaultConstruction) {
329 civil_second ss;
330 EXPECT_EQ("1970-01-01T00:00:00", Format(ss));
331
332 civil_minute mm;
333 EXPECT_EQ("1970-01-01T00:00", Format(mm));
334
335 civil_hour hh;
336 EXPECT_EQ("1970-01-01T00", Format(hh));
337
338 civil_day d;
339 EXPECT_EQ("1970-01-01", Format(d));
340
341 civil_month m;
342 EXPECT_EQ("1970-01", Format(m));
343
344 civil_year y;
345 EXPECT_EQ("1970", Format(y));
346 }
347
TEST(CivilTime,StructMember)348 TEST(CivilTime, StructMember) {
349 struct S {
350 civil_day day;
351 };
352 S s = {};
353 EXPECT_EQ(civil_day{}, s.day);
354 }
355
TEST(CivilTime,FieldsConstruction)356 TEST(CivilTime, FieldsConstruction) {
357 EXPECT_EQ("2015-01-02T03:04:05", Format(civil_second(2015, 1, 2, 3, 4, 5)));
358 EXPECT_EQ("2015-01-02T03:04:00", Format(civil_second(2015, 1, 2, 3, 4)));
359 EXPECT_EQ("2015-01-02T03:00:00", Format(civil_second(2015, 1, 2, 3)));
360 EXPECT_EQ("2015-01-02T00:00:00", Format(civil_second(2015, 1, 2)));
361 EXPECT_EQ("2015-01-01T00:00:00", Format(civil_second(2015, 1)));
362 EXPECT_EQ("2015-01-01T00:00:00", Format(civil_second(2015)));
363
364 EXPECT_EQ("2015-01-02T03:04", Format(civil_minute(2015, 1, 2, 3, 4, 5)));
365 EXPECT_EQ("2015-01-02T03:04", Format(civil_minute(2015, 1, 2, 3, 4)));
366 EXPECT_EQ("2015-01-02T03:00", Format(civil_minute(2015, 1, 2, 3)));
367 EXPECT_EQ("2015-01-02T00:00", Format(civil_minute(2015, 1, 2)));
368 EXPECT_EQ("2015-01-01T00:00", Format(civil_minute(2015, 1)));
369 EXPECT_EQ("2015-01-01T00:00", Format(civil_minute(2015)));
370
371 EXPECT_EQ("2015-01-02T03", Format(civil_hour(2015, 1, 2, 3, 4, 5)));
372 EXPECT_EQ("2015-01-02T03", Format(civil_hour(2015, 1, 2, 3, 4)));
373 EXPECT_EQ("2015-01-02T03", Format(civil_hour(2015, 1, 2, 3)));
374 EXPECT_EQ("2015-01-02T00", Format(civil_hour(2015, 1, 2)));
375 EXPECT_EQ("2015-01-01T00", Format(civil_hour(2015, 1)));
376 EXPECT_EQ("2015-01-01T00", Format(civil_hour(2015)));
377
378 EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2, 3, 4, 5)));
379 EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2, 3, 4)));
380 EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2, 3)));
381 EXPECT_EQ("2015-01-02", Format(civil_day(2015, 1, 2)));
382 EXPECT_EQ("2015-01-01", Format(civil_day(2015, 1)));
383 EXPECT_EQ("2015-01-01", Format(civil_day(2015)));
384
385 EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2, 3, 4, 5)));
386 EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2, 3, 4)));
387 EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2, 3)));
388 EXPECT_EQ("2015-01", Format(civil_month(2015, 1, 2)));
389 EXPECT_EQ("2015-01", Format(civil_month(2015, 1)));
390 EXPECT_EQ("2015-01", Format(civil_month(2015)));
391
392 EXPECT_EQ("2015", Format(civil_year(2015, 1, 2, 3, 4, 5)));
393 EXPECT_EQ("2015", Format(civil_year(2015, 1, 2, 3, 4)));
394 EXPECT_EQ("2015", Format(civil_year(2015, 1, 2, 3)));
395 EXPECT_EQ("2015", Format(civil_year(2015, 1, 2)));
396 EXPECT_EQ("2015", Format(civil_year(2015, 1)));
397 EXPECT_EQ("2015", Format(civil_year(2015)));
398 }
399
TEST(CivilTime,FieldsConstructionLimits)400 TEST(CivilTime, FieldsConstructionLimits) {
401 const int kIntMax = std::numeric_limits<int>::max();
402 EXPECT_EQ("2038-01-19T03:14:07",
403 Format(civil_second(1970, 1, 1, 0, 0, kIntMax)));
404 EXPECT_EQ("6121-02-11T05:21:07",
405 Format(civil_second(1970, 1, 1, 0, kIntMax, kIntMax)));
406 EXPECT_EQ("251104-11-20T12:21:07",
407 Format(civil_second(1970, 1, 1, kIntMax, kIntMax, kIntMax)));
408 EXPECT_EQ("6130715-05-30T12:21:07",
409 Format(civil_second(1970, 1, kIntMax, kIntMax, kIntMax, kIntMax)));
410 EXPECT_EQ(
411 "185087685-11-26T12:21:07",
412 Format(civil_second(1970, kIntMax, kIntMax, kIntMax, kIntMax, kIntMax)));
413
414 const int kIntMin = std::numeric_limits<int>::min();
415 EXPECT_EQ("1901-12-13T20:45:52",
416 Format(civil_second(1970, 1, 1, 0, 0, kIntMin)));
417 EXPECT_EQ("-2182-11-20T18:37:52",
418 Format(civil_second(1970, 1, 1, 0, kIntMin, kIntMin)));
419 EXPECT_EQ("-247165-02-11T10:37:52",
420 Format(civil_second(1970, 1, 1, kIntMin, kIntMin, kIntMin)));
421 EXPECT_EQ("-6126776-08-01T10:37:52",
422 Format(civil_second(1970, 1, kIntMin, kIntMin, kIntMin, kIntMin)));
423 EXPECT_EQ(
424 "-185083747-10-31T10:37:52",
425 Format(civil_second(1970, kIntMin, kIntMin, kIntMin, kIntMin, kIntMin)));
426 }
427
TEST(CivilTime,ImplicitCrossAlignment)428 TEST(CivilTime, ImplicitCrossAlignment) {
429 civil_year year(2015);
430 civil_month month = year;
431 civil_day day = month;
432 civil_hour hour = day;
433 civil_minute minute = hour;
434 civil_second second = minute;
435
436 second = year;
437 EXPECT_EQ(second, year);
438 second = month;
439 EXPECT_EQ(second, month);
440 second = day;
441 EXPECT_EQ(second, day);
442 second = hour;
443 EXPECT_EQ(second, hour);
444 second = minute;
445 EXPECT_EQ(second, minute);
446
447 minute = year;
448 EXPECT_EQ(minute, year);
449 minute = month;
450 EXPECT_EQ(minute, month);
451 minute = day;
452 EXPECT_EQ(minute, day);
453 minute = hour;
454 EXPECT_EQ(minute, hour);
455
456 hour = year;
457 EXPECT_EQ(hour, year);
458 hour = month;
459 EXPECT_EQ(hour, month);
460 hour = day;
461 EXPECT_EQ(hour, day);
462
463 day = year;
464 EXPECT_EQ(day, year);
465 day = month;
466 EXPECT_EQ(day, month);
467
468 month = year;
469 EXPECT_EQ(month, year);
470
471 // Ensures unsafe conversions are not allowed.
472 EXPECT_FALSE((std::is_convertible<civil_second, civil_minute>::value));
473 EXPECT_FALSE((std::is_convertible<civil_second, civil_hour>::value));
474 EXPECT_FALSE((std::is_convertible<civil_second, civil_day>::value));
475 EXPECT_FALSE((std::is_convertible<civil_second, civil_month>::value));
476 EXPECT_FALSE((std::is_convertible<civil_second, civil_year>::value));
477
478 EXPECT_FALSE((std::is_convertible<civil_minute, civil_hour>::value));
479 EXPECT_FALSE((std::is_convertible<civil_minute, civil_day>::value));
480 EXPECT_FALSE((std::is_convertible<civil_minute, civil_month>::value));
481 EXPECT_FALSE((std::is_convertible<civil_minute, civil_year>::value));
482
483 EXPECT_FALSE((std::is_convertible<civil_hour, civil_day>::value));
484 EXPECT_FALSE((std::is_convertible<civil_hour, civil_month>::value));
485 EXPECT_FALSE((std::is_convertible<civil_hour, civil_year>::value));
486
487 EXPECT_FALSE((std::is_convertible<civil_day, civil_month>::value));
488 EXPECT_FALSE((std::is_convertible<civil_day, civil_year>::value));
489
490 EXPECT_FALSE((std::is_convertible<civil_month, civil_year>::value));
491 }
492
TEST(CivilTime,ExplicitCrossAlignment)493 TEST(CivilTime, ExplicitCrossAlignment) {
494 //
495 // Assign from smaller units -> larger units
496 //
497
498 civil_second second(2015, 1, 2, 3, 4, 5);
499 EXPECT_EQ("2015-01-02T03:04:05", Format(second));
500
501 civil_minute minute(second);
502 EXPECT_EQ("2015-01-02T03:04", Format(minute));
503
504 civil_hour hour(minute);
505 EXPECT_EQ("2015-01-02T03", Format(hour));
506
507 civil_day day(hour);
508 EXPECT_EQ("2015-01-02", Format(day));
509
510 civil_month month(day);
511 EXPECT_EQ("2015-01", Format(month));
512
513 civil_year year(month);
514 EXPECT_EQ("2015", Format(year));
515
516 //
517 // Now assign from larger units -> smaller units
518 //
519
520 month = civil_month(year);
521 EXPECT_EQ("2015-01", Format(month));
522
523 day = civil_day(month);
524 EXPECT_EQ("2015-01-01", Format(day));
525
526 hour = civil_hour(day);
527 EXPECT_EQ("2015-01-01T00", Format(hour));
528
529 minute = civil_minute(hour);
530 EXPECT_EQ("2015-01-01T00:00", Format(minute));
531
532 second = civil_second(minute);
533 EXPECT_EQ("2015-01-01T00:00:00", Format(second));
534 }
535
536 // Metafunction to test whether difference is allowed between two types.
537 template <typename T1, typename T2>
538 struct HasDifference {
539 template <typename U1, typename U2>
540 static std::false_type test(...);
541 template <typename U1, typename U2>
542 static std::true_type test(decltype(std::declval<U1>() - std::declval<U2>()));
543 static constexpr bool value = decltype(test<T1, T2>(0))::value;
544 };
545
TEST(CivilTime,DisallowCrossAlignedDifference)546 TEST(CivilTime, DisallowCrossAlignedDifference) {
547 // Difference is allowed between types with the same alignment.
548 static_assert(HasDifference<civil_second, civil_second>::value, "");
549 static_assert(HasDifference<civil_minute, civil_minute>::value, "");
550 static_assert(HasDifference<civil_hour, civil_hour>::value, "");
551 static_assert(HasDifference<civil_day, civil_day>::value, "");
552 static_assert(HasDifference<civil_month, civil_month>::value, "");
553 static_assert(HasDifference<civil_year, civil_year>::value, "");
554
555 // Difference is disallowed between types with different alignments.
556 static_assert(!HasDifference<civil_second, civil_minute>::value, "");
557 static_assert(!HasDifference<civil_second, civil_hour>::value, "");
558 static_assert(!HasDifference<civil_second, civil_day>::value, "");
559 static_assert(!HasDifference<civil_second, civil_month>::value, "");
560 static_assert(!HasDifference<civil_second, civil_year>::value, "");
561
562 static_assert(!HasDifference<civil_minute, civil_hour>::value, "");
563 static_assert(!HasDifference<civil_minute, civil_day>::value, "");
564 static_assert(!HasDifference<civil_minute, civil_month>::value, "");
565 static_assert(!HasDifference<civil_minute, civil_year>::value, "");
566
567 static_assert(!HasDifference<civil_hour, civil_day>::value, "");
568 static_assert(!HasDifference<civil_hour, civil_month>::value, "");
569 static_assert(!HasDifference<civil_hour, civil_year>::value, "");
570
571 static_assert(!HasDifference<civil_day, civil_month>::value, "");
572 static_assert(!HasDifference<civil_day, civil_year>::value, "");
573
574 static_assert(!HasDifference<civil_month, civil_year>::value, "");
575 }
576
TEST(CivilTime,ValueSemantics)577 TEST(CivilTime, ValueSemantics) {
578 const civil_hour a(2015, 1, 2, 3);
579 const civil_hour b = a;
580 const civil_hour c(b);
581 civil_hour d;
582 d = c;
583 EXPECT_EQ("2015-01-02T03", Format(d));
584 }
585
TEST(CivilTime,Relational)586 TEST(CivilTime, Relational) {
587 // Tests that the alignment unit is ignored in comparison.
588 const civil_year year(2014);
589 const civil_month month(year);
590 EXPECT_EQ(year, month);
591
592 #define TEST_RELATIONAL(OLDER, YOUNGER) \
593 do { \
594 EXPECT_FALSE(OLDER < OLDER); \
595 EXPECT_FALSE(OLDER > OLDER); \
596 EXPECT_TRUE(OLDER >= OLDER); \
597 EXPECT_TRUE(OLDER <= OLDER); \
598 EXPECT_FALSE(YOUNGER < YOUNGER); \
599 EXPECT_FALSE(YOUNGER > YOUNGER); \
600 EXPECT_TRUE(YOUNGER >= YOUNGER); \
601 EXPECT_TRUE(YOUNGER <= YOUNGER); \
602 EXPECT_EQ(OLDER, OLDER); \
603 EXPECT_NE(OLDER, YOUNGER); \
604 EXPECT_LT(OLDER, YOUNGER); \
605 EXPECT_LE(OLDER, YOUNGER); \
606 EXPECT_GT(YOUNGER, OLDER); \
607 EXPECT_GE(YOUNGER, OLDER); \
608 } while (0)
609
610 // Alignment is ignored in comparison (verified above), so kSecond is used
611 // to test comparison in all field positions.
612 TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
613 civil_second(2015, 1, 1, 0, 0, 0));
614 TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
615 civil_second(2014, 2, 1, 0, 0, 0));
616 TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
617 civil_second(2014, 1, 2, 0, 0, 0));
618 TEST_RELATIONAL(civil_second(2014, 1, 1, 0, 0, 0),
619 civil_second(2014, 1, 1, 1, 0, 0));
620 TEST_RELATIONAL(civil_second(2014, 1, 1, 1, 0, 0),
621 civil_second(2014, 1, 1, 1, 1, 0));
622 TEST_RELATIONAL(civil_second(2014, 1, 1, 1, 1, 0),
623 civil_second(2014, 1, 1, 1, 1, 1));
624
625 // Tests the relational operators of two different civil-time types.
626 TEST_RELATIONAL(civil_day(2014, 1, 1), civil_minute(2014, 1, 1, 1, 1));
627 TEST_RELATIONAL(civil_day(2014, 1, 1), civil_month(2014, 2));
628
629 #undef TEST_RELATIONAL
630 }
631
TEST(CivilTime,Arithmetic)632 TEST(CivilTime, Arithmetic) {
633 civil_second second(2015, 1, 2, 3, 4, 5);
634 EXPECT_EQ("2015-01-02T03:04:06", Format(second += 1));
635 EXPECT_EQ("2015-01-02T03:04:07", Format(second + 1));
636 EXPECT_EQ("2015-01-02T03:04:08", Format(2 + second));
637 EXPECT_EQ("2015-01-02T03:04:05", Format(second - 1));
638 EXPECT_EQ("2015-01-02T03:04:05", Format(second -= 1));
639 EXPECT_EQ("2015-01-02T03:04:05", Format(second++));
640 EXPECT_EQ("2015-01-02T03:04:07", Format(++second));
641 EXPECT_EQ("2015-01-02T03:04:07", Format(second--));
642 EXPECT_EQ("2015-01-02T03:04:05", Format(--second));
643
644 civil_minute minute(2015, 1, 2, 3, 4);
645 EXPECT_EQ("2015-01-02T03:05", Format(minute += 1));
646 EXPECT_EQ("2015-01-02T03:06", Format(minute + 1));
647 EXPECT_EQ("2015-01-02T03:07", Format(2 + minute));
648 EXPECT_EQ("2015-01-02T03:04", Format(minute - 1));
649 EXPECT_EQ("2015-01-02T03:04", Format(minute -= 1));
650 EXPECT_EQ("2015-01-02T03:04", Format(minute++));
651 EXPECT_EQ("2015-01-02T03:06", Format(++minute));
652 EXPECT_EQ("2015-01-02T03:06", Format(minute--));
653 EXPECT_EQ("2015-01-02T03:04", Format(--minute));
654
655 civil_hour hour(2015, 1, 2, 3);
656 EXPECT_EQ("2015-01-02T04", Format(hour += 1));
657 EXPECT_EQ("2015-01-02T05", Format(hour + 1));
658 EXPECT_EQ("2015-01-02T06", Format(2 + hour));
659 EXPECT_EQ("2015-01-02T03", Format(hour - 1));
660 EXPECT_EQ("2015-01-02T03", Format(hour -= 1));
661 EXPECT_EQ("2015-01-02T03", Format(hour++));
662 EXPECT_EQ("2015-01-02T05", Format(++hour));
663 EXPECT_EQ("2015-01-02T05", Format(hour--));
664 EXPECT_EQ("2015-01-02T03", Format(--hour));
665
666 civil_day day(2015, 1, 2);
667 EXPECT_EQ("2015-01-03", Format(day += 1));
668 EXPECT_EQ("2015-01-04", Format(day + 1));
669 EXPECT_EQ("2015-01-05", Format(2 + day));
670 EXPECT_EQ("2015-01-02", Format(day - 1));
671 EXPECT_EQ("2015-01-02", Format(day -= 1));
672 EXPECT_EQ("2015-01-02", Format(day++));
673 EXPECT_EQ("2015-01-04", Format(++day));
674 EXPECT_EQ("2015-01-04", Format(day--));
675 EXPECT_EQ("2015-01-02", Format(--day));
676
677 civil_month month(2015, 1);
678 EXPECT_EQ("2015-02", Format(month += 1));
679 EXPECT_EQ("2015-03", Format(month + 1));
680 EXPECT_EQ("2015-04", Format(2 + month));
681 EXPECT_EQ("2015-01", Format(month - 1));
682 EXPECT_EQ("2015-01", Format(month -= 1));
683 EXPECT_EQ("2015-01", Format(month++));
684 EXPECT_EQ("2015-03", Format(++month));
685 EXPECT_EQ("2015-03", Format(month--));
686 EXPECT_EQ("2015-01", Format(--month));
687
688 civil_year year(2015);
689 EXPECT_EQ("2016", Format(year += 1));
690 EXPECT_EQ("2017", Format(year + 1));
691 EXPECT_EQ("2018", Format(2 + year));
692 EXPECT_EQ("2015", Format(year - 1));
693 EXPECT_EQ("2015", Format(year -= 1));
694 EXPECT_EQ("2015", Format(year++));
695 EXPECT_EQ("2017", Format(++year));
696 EXPECT_EQ("2017", Format(year--));
697 EXPECT_EQ("2015", Format(--year));
698 }
699
TEST(CivilTime,ArithmeticLimits)700 TEST(CivilTime, ArithmeticLimits) {
701 const int kIntMax = std::numeric_limits<int>::max();
702 const int kIntMin = std::numeric_limits<int>::min();
703
704 civil_second second(1970, 1, 1, 0, 0, 0);
705 second += kIntMax;
706 EXPECT_EQ("2038-01-19T03:14:07", Format(second));
707 second -= kIntMax;
708 EXPECT_EQ("1970-01-01T00:00:00", Format(second));
709 second += kIntMin;
710 EXPECT_EQ("1901-12-13T20:45:52", Format(second));
711 second -= kIntMin;
712 EXPECT_EQ("1970-01-01T00:00:00", Format(second));
713
714 civil_minute minute(1970, 1, 1, 0, 0);
715 minute += kIntMax;
716 EXPECT_EQ("6053-01-23T02:07", Format(minute));
717 minute -= kIntMax;
718 EXPECT_EQ("1970-01-01T00:00", Format(minute));
719 minute += kIntMin;
720 EXPECT_EQ("-2114-12-08T21:52", Format(minute));
721 minute -= kIntMin;
722 EXPECT_EQ("1970-01-01T00:00", Format(minute));
723
724 civil_hour hour(1970, 1, 1, 0);
725 hour += kIntMax;
726 EXPECT_EQ("246953-10-09T07", Format(hour));
727 hour -= kIntMax;
728 EXPECT_EQ("1970-01-01T00", Format(hour));
729 hour += kIntMin;
730 EXPECT_EQ("-243014-03-24T16", Format(hour));
731 hour -= kIntMin;
732 EXPECT_EQ("1970-01-01T00", Format(hour));
733
734 civil_day day(1970, 1, 1);
735 day += kIntMax;
736 EXPECT_EQ("5881580-07-11", Format(day));
737 day -= kIntMax;
738 EXPECT_EQ("1970-01-01", Format(day));
739 day += kIntMin;
740 EXPECT_EQ("-5877641-06-23", Format(day));
741 day -= kIntMin;
742 EXPECT_EQ("1970-01-01", Format(day));
743
744 civil_month month(1970, 1);
745 month += kIntMax;
746 EXPECT_EQ("178958940-08", Format(month));
747 month -= kIntMax;
748 EXPECT_EQ("1970-01", Format(month));
749 month += kIntMin;
750 EXPECT_EQ("-178955001-05", Format(month));
751 month -= kIntMin;
752 EXPECT_EQ("1970-01", Format(month));
753
754 civil_year year(0);
755 year += kIntMax;
756 EXPECT_EQ("2147483647", Format(year));
757 year -= kIntMax;
758 EXPECT_EQ("0", Format(year));
759 year += kIntMin;
760 EXPECT_EQ("-2147483648", Format(year));
761 year -= kIntMin;
762 EXPECT_EQ("0", Format(year));
763 }
764
TEST(CivilTime,ArithmeticDifference)765 TEST(CivilTime, ArithmeticDifference) {
766 civil_second second(2015, 1, 2, 3, 4, 5);
767 EXPECT_EQ(0, second - second);
768 EXPECT_EQ(10, (second + 10) - second);
769 EXPECT_EQ(-10, (second - 10) - second);
770
771 civil_minute minute(2015, 1, 2, 3, 4);
772 EXPECT_EQ(0, minute - minute);
773 EXPECT_EQ(10, (minute + 10) - minute);
774 EXPECT_EQ(-10, (minute - 10) - minute);
775
776 civil_hour hour(2015, 1, 2, 3);
777 EXPECT_EQ(0, hour - hour);
778 EXPECT_EQ(10, (hour + 10) - hour);
779 EXPECT_EQ(-10, (hour - 10) - hour);
780
781 civil_day day(2015, 1, 2);
782 EXPECT_EQ(0, day - day);
783 EXPECT_EQ(10, (day + 10) - day);
784 EXPECT_EQ(-10, (day - 10) - day);
785
786 civil_month month(2015, 1);
787 EXPECT_EQ(0, month - month);
788 EXPECT_EQ(10, (month + 10) - month);
789 EXPECT_EQ(-10, (month - 10) - month);
790
791 civil_year year(2015);
792 EXPECT_EQ(0, year - year);
793 EXPECT_EQ(10, (year + 10) - year);
794 EXPECT_EQ(-10, (year - 10) - year);
795 }
796
TEST(CivilTime,DifferenceLimits)797 TEST(CivilTime, DifferenceLimits) {
798 const int kIntMax = std::numeric_limits<int>::max();
799 const int kIntMin = std::numeric_limits<int>::min();
800
801 // Check day arithmetic at the end of the year range.
802 const civil_day max_day(kIntMax, 12, 31);
803 EXPECT_EQ(1, max_day - (max_day - 1));
804 EXPECT_EQ(-1, (max_day - 1) - max_day);
805
806 // Check day arithmetic at the end of the year range.
807 const civil_day min_day(kIntMin, 1, 1);
808 EXPECT_EQ(1, (min_day + 1) - min_day);
809 EXPECT_EQ(-1, min_day - (min_day + 1));
810
811 // Check the limits of the return value.
812 const civil_day d1(1970, 1, 1);
813 const civil_day d2(5881580, 7, 11);
814 EXPECT_EQ(kIntMax, d2 - d1);
815 EXPECT_EQ(kIntMin, d1 - (d2 + 1));
816 }
817
TEST(CivilTime,Properties)818 TEST(CivilTime, Properties) {
819 civil_second ss(2015, 2, 3, 4, 5, 6);
820 EXPECT_EQ(2015, ss.year());
821 EXPECT_EQ(2, ss.month());
822 EXPECT_EQ(3, ss.day());
823 EXPECT_EQ(4, ss.hour());
824 EXPECT_EQ(5, ss.minute());
825 EXPECT_EQ(6, ss.second());
826 EXPECT_EQ(weekday::tuesday, get_weekday(ss));
827 EXPECT_EQ(34, get_yearday(ss));
828
829 civil_minute mm(2015, 2, 3, 4, 5, 6);
830 EXPECT_EQ(2015, mm.year());
831 EXPECT_EQ(2, mm.month());
832 EXPECT_EQ(3, mm.day());
833 EXPECT_EQ(4, mm.hour());
834 EXPECT_EQ(5, mm.minute());
835 EXPECT_EQ(0, mm.second());
836 EXPECT_EQ(weekday::tuesday, get_weekday(mm));
837 EXPECT_EQ(34, get_yearday(mm));
838
839 civil_hour hh(2015, 2, 3, 4, 5, 6);
840 EXPECT_EQ(2015, hh.year());
841 EXPECT_EQ(2, hh.month());
842 EXPECT_EQ(3, hh.day());
843 EXPECT_EQ(4, hh.hour());
844 EXPECT_EQ(0, hh.minute());
845 EXPECT_EQ(0, hh.second());
846 EXPECT_EQ(weekday::tuesday, get_weekday(hh));
847 EXPECT_EQ(34, get_yearday(hh));
848
849 civil_day d(2015, 2, 3, 4, 5, 6);
850 EXPECT_EQ(2015, d.year());
851 EXPECT_EQ(2, d.month());
852 EXPECT_EQ(3, d.day());
853 EXPECT_EQ(0, d.hour());
854 EXPECT_EQ(0, d.minute());
855 EXPECT_EQ(0, d.second());
856 EXPECT_EQ(weekday::tuesday, get_weekday(d));
857 EXPECT_EQ(34, get_yearday(d));
858
859 civil_month m(2015, 2, 3, 4, 5, 6);
860 EXPECT_EQ(2015, m.year());
861 EXPECT_EQ(2, m.month());
862 EXPECT_EQ(1, m.day());
863 EXPECT_EQ(0, m.hour());
864 EXPECT_EQ(0, m.minute());
865 EXPECT_EQ(0, m.second());
866 EXPECT_EQ(weekday::sunday, get_weekday(m));
867 EXPECT_EQ(32, get_yearday(m));
868
869 civil_year y(2015, 2, 3, 4, 5, 6);
870 EXPECT_EQ(2015, y.year());
871 EXPECT_EQ(1, y.month());
872 EXPECT_EQ(1, y.day());
873 EXPECT_EQ(0, y.hour());
874 EXPECT_EQ(0, y.minute());
875 EXPECT_EQ(0, y.second());
876 EXPECT_EQ(weekday::thursday, get_weekday(y));
877 EXPECT_EQ(1, get_yearday(y));
878 }
879
TEST(CivilTime,OutputStream)880 TEST(CivilTime, OutputStream) {
881 // Tests formatting of civil_year, which does not pad.
882 EXPECT_EQ("2016", Format(civil_year(2016)));
883 EXPECT_EQ("123", Format(civil_year(123)));
884 EXPECT_EQ("0", Format(civil_year(0)));
885 EXPECT_EQ("-1", Format(civil_year(-1)));
886
887 // Tests formatting of sub-year types, which pad to 2 digits
888 EXPECT_EQ("2016-02", Format(civil_month(2016, 2)));
889 EXPECT_EQ("2016-02-03", Format(civil_day(2016, 2, 3)));
890 EXPECT_EQ("2016-02-03T04", Format(civil_hour(2016, 2, 3, 4)));
891 EXPECT_EQ("2016-02-03T04:05", Format(civil_minute(2016, 2, 3, 4, 5)));
892 EXPECT_EQ("2016-02-03T04:05:06", Format(civil_second(2016, 2, 3, 4, 5, 6)));
893
894 // Tests formatting of weekday.
895 EXPECT_EQ("Monday", Format(weekday::monday));
896 EXPECT_EQ("Tuesday", Format(weekday::tuesday));
897 EXPECT_EQ("Wednesday", Format(weekday::wednesday));
898 EXPECT_EQ("Thursday", Format(weekday::thursday));
899 EXPECT_EQ("Friday", Format(weekday::friday));
900 EXPECT_EQ("Saturday", Format(weekday::saturday));
901 EXPECT_EQ("Sunday", Format(weekday::sunday));
902 }
903
TEST(CivilTime,OutputStreamLeftFillWidth)904 TEST(CivilTime, OutputStreamLeftFillWidth) {
905 civil_second cs(2016, 2, 3, 4, 5, 6);
906 {
907 std::stringstream ss;
908 ss << std::left << std::setfill('.');
909 ss << std::setw(3) << 'X';
910 ss << std::setw(21) << civil_year(cs);
911 ss << std::setw(3) << 'X';
912 EXPECT_EQ("X..2016.................X..", ss.str());
913 }
914 {
915 std::stringstream ss;
916 ss << std::left << std::setfill('.');
917 ss << std::setw(3) << 'X';
918 ss << std::setw(21) << civil_month(cs);
919 ss << std::setw(3) << 'X';
920 EXPECT_EQ("X..2016-02..............X..", ss.str());
921 }
922 {
923 std::stringstream ss;
924 ss << std::left << std::setfill('.');
925 ss << std::setw(3) << 'X';
926 ss << std::setw(21) << civil_day(cs);
927 ss << std::setw(3) << 'X';
928 EXPECT_EQ("X..2016-02-03...........X..", ss.str());
929 }
930 {
931 std::stringstream ss;
932 ss << std::left << std::setfill('.');
933 ss << std::setw(3) << 'X';
934 ss << std::setw(21) << civil_hour(cs);
935 ss << std::setw(3) << 'X';
936 EXPECT_EQ("X..2016-02-03T04........X..", ss.str());
937 }
938 {
939 std::stringstream ss;
940 ss << std::left << std::setfill('.');
941 ss << std::setw(3) << 'X';
942 ss << std::setw(21) << civil_minute(cs);
943 ss << std::setw(3) << 'X';
944 EXPECT_EQ("X..2016-02-03T04:05.....X..", ss.str());
945 }
946 {
947 std::stringstream ss;
948 ss << std::left << std::setfill('.');
949 ss << std::setw(3) << 'X';
950 ss << std::setw(21) << civil_second(cs);
951 ss << std::setw(3) << 'X';
952 EXPECT_EQ("X..2016-02-03T04:05:06..X..", ss.str());
953 }
954 }
955
TEST(CivilTime,NextPrevWeekday)956 TEST(CivilTime, NextPrevWeekday) {
957 // Jan 1, 1970 was a Thursday.
958 const civil_day thursday(1970, 1, 1);
959 EXPECT_EQ(weekday::thursday, get_weekday(thursday));
960
961 // Thursday -> Thursday
962 civil_day d = next_weekday(thursday, weekday::thursday);
963 EXPECT_EQ(7, d - thursday) << Format(d);
964 EXPECT_EQ(d - 14, prev_weekday(thursday, weekday::thursday));
965
966 // Thursday -> Friday
967 d = next_weekday(thursday, weekday::friday);
968 EXPECT_EQ(1, d - thursday) << Format(d);
969 EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::friday));
970
971 // Thursday -> Saturday
972 d = next_weekday(thursday, weekday::saturday);
973 EXPECT_EQ(2, d - thursday) << Format(d);
974 EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::saturday));
975
976 // Thursday -> Sunday
977 d = next_weekday(thursday, weekday::sunday);
978 EXPECT_EQ(3, d - thursday) << Format(d);
979 EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::sunday));
980
981 // Thursday -> Monday
982 d = next_weekday(thursday, weekday::monday);
983 EXPECT_EQ(4, d - thursday) << Format(d);
984 EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::monday));
985
986 // Thursday -> Tuesday
987 d = next_weekday(thursday, weekday::tuesday);
988 EXPECT_EQ(5, d - thursday) << Format(d);
989 EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::tuesday));
990
991 // Thursday -> Wednesday
992 d = next_weekday(thursday, weekday::wednesday);
993 EXPECT_EQ(6, d - thursday) << Format(d);
994 EXPECT_EQ(d - 7, prev_weekday(thursday, weekday::wednesday));
995 }
996
TEST(CivilTime,NormalizeWithHugeYear)997 TEST(CivilTime, NormalizeWithHugeYear) {
998 civil_month c(9223372036854775807, 1);
999 EXPECT_EQ("9223372036854775807-01", Format(c));
1000 c = c - 1; // Causes normalization
1001 EXPECT_EQ("9223372036854775806-12", Format(c));
1002
1003 c = civil_month(-9223372036854775807 - 1, 1);
1004 EXPECT_EQ("-9223372036854775808-01", Format(c));
1005 c = c + 12; // Causes normalization
1006 EXPECT_EQ("-9223372036854775807-01", Format(c));
1007 }
1008
TEST(CivilTime,LeapYears)1009 TEST(CivilTime, LeapYears) {
1010 // Test data for leap years.
1011 const struct {
1012 int year;
1013 int days;
1014 struct {
1015 int month;
1016 int day;
1017 } leap_day; // The date of the day after Feb 28.
1018 } kLeapYearTable[]{
1019 {1900, 365, {3, 1}}, {1999, 365, {3, 1}},
1020 {2000, 366, {2, 29}}, // leap year
1021 {2001, 365, {3, 1}}, {2002, 365, {3, 1}},
1022 {2003, 365, {3, 1}}, {2004, 366, {2, 29}}, // leap year
1023 {2005, 365, {3, 1}}, {2006, 365, {3, 1}},
1024 {2007, 365, {3, 1}}, {2008, 366, {2, 29}}, // leap year
1025 {2009, 365, {3, 1}}, {2100, 365, {3, 1}},
1026 };
1027
1028 for (const auto& e : kLeapYearTable) {
1029 // Tests incrementing through the leap day.
1030 const civil_day feb28(e.year, 2, 28);
1031 const civil_day next_day = feb28 + 1;
1032 EXPECT_EQ(e.leap_day.month, next_day.month());
1033 EXPECT_EQ(e.leap_day.day, next_day.day());
1034
1035 // Tests difference in days of leap years.
1036 const civil_year year(feb28);
1037 const civil_year next_year = year + 1;
1038 EXPECT_EQ(e.days, civil_day(next_year) - civil_day(year));
1039 }
1040 }
1041
TEST(CivilTime,FirstThursdayInMonth)1042 TEST(CivilTime, FirstThursdayInMonth) {
1043 const civil_day nov1(2014, 11, 1);
1044 const civil_day thursday = next_weekday(nov1 - 1, weekday::thursday);
1045 EXPECT_EQ("2014-11-06", Format(thursday));
1046
1047 // Bonus: Date of Thanksgiving in the United States
1048 // Rule: Fourth Thursday of November
1049 const civil_day thanksgiving = thursday + 7 * 3;
1050 EXPECT_EQ("2014-11-27", Format(thanksgiving));
1051 }
1052
1053 } // namespace cctz
1054 } // namespace time_internal
1055 ABSL_NAMESPACE_END
1056 } // namespace absl
1057