19 #include <type_traits> 22 #include "gtest/gtest.h" 26 TEST(CivilTime, DefaultConstruction) {
46 TEST(CivilTime, StructMember) {
54 TEST(CivilTime, FieldsConstruction) {
55 EXPECT_EQ(
"2015-01-02T03:04:05",
57 EXPECT_EQ(
"2015-01-02T03:04:00",
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",
68 EXPECT_EQ(
"2015-01-02T03:04",
70 EXPECT_EQ(
"2015-01-02T03:04",
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",
81 EXPECT_EQ(
"2015-01-02T03",
83 EXPECT_EQ(
"2015-01-02T03",
85 EXPECT_EQ(
"2015-01-02T03",
87 EXPECT_EQ(
"2015-01-02T00",
89 EXPECT_EQ(
"2015-01-01T00",
91 EXPECT_EQ(
"2015-01-01T00",
94 EXPECT_EQ(
"2015-01-02",
96 EXPECT_EQ(
"2015-01-02",
98 EXPECT_EQ(
"2015-01-02",
100 EXPECT_EQ(
"2015-01-02",
102 EXPECT_EQ(
"2015-01-01",
104 EXPECT_EQ(
"2015-01-01",
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)));
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)));
170 TEST(CivilTime, RangeLimits) {
172 std::numeric_limits<absl::civil_year_t>::max();
187 std::numeric_limits<absl::civil_year_t>::min();
202 TEST(CivilTime, ImplicitCrossAlignment) {
211 EXPECT_EQ(second, year);
213 EXPECT_EQ(second, month);
215 EXPECT_EQ(second, day);
217 EXPECT_EQ(second, hour);
219 EXPECT_EQ(second, minute);
222 EXPECT_EQ(minute, year);
224 EXPECT_EQ(minute, month);
226 EXPECT_EQ(minute, day);
228 EXPECT_EQ(minute, hour);
231 EXPECT_EQ(hour, year);
233 EXPECT_EQ(hour, month);
235 EXPECT_EQ(hour, day);
238 EXPECT_EQ(day, year);
240 EXPECT_EQ(day, month);
243 EXPECT_EQ(month, year);
282 TEST(CivilTime, ExplicitCrossAlignment) {
326 template <
typename T1,
typename T2>
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;
335 TEST(CivilTime, DisallowCrossAlignedDifference) {
366 TEST(CivilTime, ValueSemantics) {
375 TEST(CivilTime, Relational) {
379 EXPECT_EQ(year, month);
381 #define TEST_RELATIONAL(OLDER, YOUNGER) \ 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); \ 420 #undef TEST_RELATIONAL 423 TEST(CivilTime, Arithmetic) {
491 TEST(CivilTime, ArithmeticLimits) {
492 const int kIntMax = std::numeric_limits<int>::max();
493 const int kIntMin = std::numeric_limits<int>::min();
556 TEST(CivilTime, Difference) {
558 EXPECT_EQ(0, second - second);
559 EXPECT_EQ(10, (second + 10) - second);
560 EXPECT_EQ(-10, (second - 10) - second);
563 EXPECT_EQ(0, minute - minute);
564 EXPECT_EQ(10, (minute + 10) - minute);
565 EXPECT_EQ(-10, (minute - 10) - minute);
568 EXPECT_EQ(0, hour - hour);
569 EXPECT_EQ(10, (hour + 10) - hour);
570 EXPECT_EQ(-10, (hour - 10) - hour);
573 EXPECT_EQ(0, day - day);
574 EXPECT_EQ(10, (day + 10) - day);
575 EXPECT_EQ(-10, (day - 10) - day);
578 EXPECT_EQ(0, month - month);
579 EXPECT_EQ(10, (month + 10) - month);
580 EXPECT_EQ(-10, (month - 10) - month);
583 EXPECT_EQ(0, year - year);
584 EXPECT_EQ(10, (year + 10) - year);
585 EXPECT_EQ(-10, (year - 10) - year);
588 TEST(CivilTime, DifferenceLimits) {
590 std::numeric_limits<absl::civil_diff_t>::max();
592 std::numeric_limits<absl::civil_diff_t>::min();
596 EXPECT_EQ(1, max_day - (max_day - 1));
597 EXPECT_EQ(-1, (max_day - 1) - max_day);
601 EXPECT_EQ(1, (min_day + 1) - min_day);
602 EXPECT_EQ(-1, min_day - (min_day + 1));
607 EXPECT_EQ(kDiffMax, d2 - d1);
608 EXPECT_EQ(kDiffMin, d1 - (d2 + 1));
611 TEST(CivilTime, Properties) {
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());
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());
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());
637 EXPECT_EQ(2015, d.
year());
638 EXPECT_EQ(2, d.
month());
639 EXPECT_EQ(3, d.
day());
640 EXPECT_EQ(0, d.
hour());
645 EXPECT_EQ(2015, m.
year());
646 EXPECT_EQ(2, m.
month());
647 EXPECT_EQ(1, m.
day());
648 EXPECT_EQ(0, m.
hour());
653 EXPECT_EQ(2015, y.
year());
654 EXPECT_EQ(1, y.
month());
655 EXPECT_EQ(1, y.
day());
656 EXPECT_EQ(0, y.
hour());
681 TEST(CivilTime, FormatAndParseLenient) {
701 TEST(CivilTime, OutputStream) {
704 std::stringstream ss;
705 ss << std::left << std::setfill(
'.');
706 ss << std::setw(3) <<
'X';
708 ss << std::setw(3) <<
'X';
709 EXPECT_EQ(
"X..2016.................X..", ss.str());
712 std::stringstream ss;
713 ss << std::left << std::setfill(
'.');
714 ss << std::setw(3) <<
'X';
716 ss << std::setw(3) <<
'X';
717 EXPECT_EQ(
"X..2016-02..............X..", ss.str());
720 std::stringstream ss;
721 ss << std::left << std::setfill(
'.');
722 ss << std::setw(3) <<
'X';
724 ss << std::setw(3) <<
'X';
725 EXPECT_EQ(
"X..2016-02-03...........X..", ss.str());
728 std::stringstream ss;
729 ss << std::left << std::setfill(
'.');
730 ss << std::setw(3) <<
'X';
732 ss << std::setw(3) <<
'X';
733 EXPECT_EQ(
"X..2016-02-03T04........X..", ss.str());
736 std::stringstream ss;
737 ss << std::left << std::setfill(
'.');
738 ss << std::setw(3) <<
'X';
740 ss << std::setw(3) <<
'X';
741 EXPECT_EQ(
"X..2016-02-03T04:05.....X..", ss.str());
744 std::stringstream ss;
745 ss << std::left << std::setfill(
'.');
746 ss << std::setw(3) <<
'X';
748 ss << std::setw(3) <<
'X';
749 EXPECT_EQ(
"X..2016-02-03T04:05:06..X..", ss.str());
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());
770 TEST(CivilTime, NextPrevWeekday) {
776 EXPECT_EQ(7, d - thursday) << d;
781 EXPECT_EQ(1, d - thursday) << d;
786 EXPECT_EQ(2, d - thursday) << d;
791 EXPECT_EQ(3, d - thursday) << d;
796 EXPECT_EQ(4, d - thursday) << d;
801 EXPECT_EQ(5, d - thursday) << d;
806 EXPECT_EQ(6, d - thursday) << d;
811 TEST(CivilTime, DifferenceWithHugeYear) {
814 EXPECT_EQ(364, d2 - d1);
818 EXPECT_EQ(365, d2 - d1);
823 EXPECT_EQ(9223372036854775807, d1 - d2);
825 EXPECT_EQ(-9223372036854775807 - 1, d2 - d1);
830 EXPECT_EQ(9223372036854775807, d2 - d1);
832 EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
837 EXPECT_EQ(9223372036854775807, d2 - d1);
839 EXPECT_EQ(-9223372036854775807 - 1, d1 - d2);
843 TEST(CivilTime, DifferenceNoIntermediateOverflow) {
848 EXPECT_EQ(-9223372036854775807 - 1, s1 - s2);
854 EXPECT_EQ(9223372036854775807, s1 - s2);
857 TEST(CivilTime, NormalizeSimpleOverflow) {
871 TEST(CivilTime, NormalizeSimpleUnderflow) {
885 TEST(CivilTime, NormalizeMultipleOverflow) {
890 TEST(CivilTime, NormalizeMultipleUnderflow) {
895 TEST(CivilTime, NormalizeOverflowLimits) {
898 const int kintmax = std::numeric_limits<int>::max();
902 const int kintmin = std::numeric_limits<int>::min();
907 TEST(CivilTime, NormalizeComplexOverflow) {
921 TEST(CivilTime, NormalizeComplexUnderflow) {
937 TEST(CivilTime, NormalizeMishmash) {
955 TEST(CivilTime, NormalizeAllTheDays) {
957 for (
int day = 1; day <= 146097; ++day) {
959 EXPECT_EQ(expected, cs);
964 TEST(CivilTime, NormalizeWithHugeYear) {
976 TEST(CivilTime, LeapYears) {
996 {2000, 366, {2, 29}},
1000 {2004, 366, {2, 29}},
1001 {2005, 365, {3, 1}},
1002 {2006, 365, {3, 1}},
1003 {2007, 365, {3, 1}},
1004 {2008, 366, {2, 29}},
1005 {2009, 365, {3, 1}},
1006 {2100, 365, {3, 1}},
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;
1018 EXPECT_EQ(m, next_day.
month());
1019 EXPECT_EQ(d, next_day.
day());
1028 TEST(CivilTime, FirstThursdayInMonth) {
1040 TEST(CivilTime, DocumentationExample) {
1047 EXPECT_EQ(minute, second - 1);
1048 EXPECT_LT(minute, second);
1053 int diff = day - june_1;
1054 EXPECT_EQ(27, diff);
1058 EXPECT_EQ(0, day_floor.hour());
Weekday GetWeekday(CivilDay cd)
CONSTEXPR_M int minute() const noexcept
time_internal::cctz::year_t civil_year_t
time_internal::cctz::detail::civil_time< time_internal::year_tag > CivilYear
time_internal::cctz::diff_t civil_diff_t
CONSTEXPR_M int day() const noexcept
CivilDay NextWeekday(CivilDay cd, Weekday wd)
time_internal::cctz::detail::civil_time< time_internal::hour_tag > CivilHour
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
time_internal::cctz::weekday Weekday
time_internal::cctz::detail::civil_time< time_internal::second_tag > CivilSecond
CONSTEXPR_M int month() const noexcept
#define TEST_RELATIONAL(OLDER, YOUNGER)
time_internal::cctz::detail::civil_time< time_internal::minute_tag > CivilMinute
time_internal::cctz::detail::civil_time< time_internal::day_tag > CivilDay
#define ABSL_ARRAYSIZE(array)
CONSTEXPR_M int second() const noexcept
bool Format(FormatRawSink raw_sink, const FormatSpec< Args... > &format, const Args &...args)
TEST(Symbolize, Unimplemented)
CONSTEXPR_M int hour() const noexcept
std::string FormatCivilTime(CivilSecond c)
CivilDay PrevWeekday(CivilDay cd, Weekday wd)