24 #include "gmock/gmock.h" 25 #include "gtest/gtest.h" 31 #if defined(GTEST_USES_SIMPLE_RE) && GTEST_USES_SIMPLE_RE 32 const char kZoneAbbrRE[] =
".*";
34 const char kZoneAbbrRE[] =
"[A-Za-z]{3,4}|[-+][0-9]{2}([0-9]{2})?";
39 #define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst) \ 41 EXPECT_EQ(y, ci.cs.year()); \ 42 EXPECT_EQ(m, ci.cs.month()); \ 43 EXPECT_EQ(d, ci.cs.day()); \ 44 EXPECT_EQ(h, ci.cs.hour()); \ 45 EXPECT_EQ(min, ci.cs.minute()); \ 46 EXPECT_EQ(s, ci.cs.second()); \ 47 EXPECT_EQ(off, ci.offset); \ 48 EXPECT_EQ(isdst, ci.is_dst); \ 49 EXPECT_THAT(ci.zone_abbr, testing::MatchesRegex(kZoneAbbrRE)); \ 55 MATCHER_P(TimespecMatcher, ts,
"") {
56 if (ts.tv_sec ==
arg.tv_sec && ts.tv_nsec ==
arg.tv_nsec)
58 *result_listener <<
"expected: {" << ts.tv_sec <<
", " << ts.tv_nsec <<
"} ";
59 *result_listener <<
"actual: {" <<
arg.tv_sec <<
", " <<
arg.tv_nsec <<
"}";
66 MATCHER_P(TimevalMatcher, tv,
"") {
67 if (tv.tv_sec ==
arg.tv_sec && tv.tv_usec ==
arg.tv_usec)
69 *result_listener <<
"expected: {" << tv.tv_sec <<
", " << tv.tv_usec <<
"} ";
70 *result_listener <<
"actual: {" <<
arg.tv_sec <<
", " <<
arg.tv_usec <<
"}";
74 TEST(Time, ConstExpr) {
76 static_assert(t0 ==
absl::Time(),
"UnixEpoch");
78 static_assert(t1 !=
absl::Time(),
"InfiniteFuture");
80 static_assert(t2 !=
absl::Time(),
"InfinitePast");
82 static_assert(t3 ==
absl::Time(),
"FromUnixNanos");
84 static_assert(t4 ==
absl::Time(),
"FromUnixMicros");
86 static_assert(t5 ==
absl::Time(),
"FromUnixMillis");
88 static_assert(t6 ==
absl::Time(),
"FromUnixSeconds");
90 static_assert(t7 ==
absl::Time(),
"FromTimeT");
93 TEST(Time, ValueSemantics) {
114 TEST(Time, Breakdown) {
141 TEST(Time, AdditiveOperators) {
146 EXPECT_EQ(d, t1 - t0);
147 EXPECT_EQ(-d, t0 - t1);
148 EXPECT_EQ(t0, t1 - d);
153 EXPECT_EQ(t0 + d, t);
154 EXPECT_EQ(d, t - t0);
170 TEST(Time, RelationalOperators) {
176 static_assert(t1 == t1,
"");
177 static_assert(t2 == t2,
"");
178 static_assert(t3 == t3,
"");
180 static_assert(t1 < t2,
"");
181 static_assert(t2 < t3,
"");
182 static_assert(t1 < t3,
"");
184 static_assert(t1 <= t1,
"");
185 static_assert(t1 <= t2,
"");
186 static_assert(t2 <= t2,
"");
187 static_assert(t2 <= t3,
"");
188 static_assert(t3 <= t3,
"");
189 static_assert(t1 <= t3,
"");
191 static_assert(t2 > t1,
"");
192 static_assert(t3 > t2,
"");
193 static_assert(t3 > t1,
"");
195 static_assert(t2 >= t2,
"");
196 static_assert(t2 >= t1,
"");
197 static_assert(t3 >= t3,
"");
198 static_assert(t3 >= t2,
"");
199 static_assert(t1 >= t1,
"");
200 static_assert(t3 >= t1,
"");
203 TEST(Time, Infinity) {
207 static_assert(ifuture == ifuture,
"");
208 static_assert(ipast == ipast,
"");
209 static_assert(ipast < ifuture,
"");
210 static_assert(ifuture > ipast,
"");
224 static_assert(t < ifuture,
"");
225 static_assert(t > ipast,
"");
228 TEST(Time, FloorConversion) {
229 #define TEST_FLOOR_CONVERSION(TO, FROM) \ 230 EXPECT_EQ(1, TO(FROM(1001))); \ 231 EXPECT_EQ(1, TO(FROM(1000))); \ 232 EXPECT_EQ(0, TO(FROM(999))); \ 233 EXPECT_EQ(0, TO(FROM(1))); \ 234 EXPECT_EQ(0, TO(FROM(0))); \ 235 EXPECT_EQ(-1, TO(FROM(-1))); \ 236 EXPECT_EQ(-1, TO(FROM(-999))); \ 237 EXPECT_EQ(-1, TO(FROM(-1000))); \ 238 EXPECT_EQ(-2, TO(FROM(-1001))); 245 #undef TEST_FLOOR_CONVERSION 294 for (
const auto& test : to_ts) {
311 for (
const auto& test : from_ts) {
331 for (
const auto& test : to_tv) {
332 EXPECT_THAT(
ToTimeval(test.t), TimevalMatcher(test.tv));
348 for (
const auto& test : from_tv) {
353 const int64_t min_plus_1 = std::numeric_limits<int64_t>::min() + 1;
355 EXPECT_EQ(std::numeric_limits<int64_t>::min(),
360 EXPECT_EQ(std::numeric_limits<int64_t>::max(),
363 EXPECT_EQ(std::numeric_limits<int64_t>::max(),
366 EXPECT_EQ(std::numeric_limits<int64_t>::max() - 1,
371 TEST(Time, RoundtripConversion) {
372 #define TEST_CONVERSION_ROUND_TRIP(SOURCE, FROM, TO, MATCHER) \ 373 EXPECT_THAT(TO(FROM(SOURCE)), MATCHER(SOURCE)) 412 int64_t now_s = std::time(
nullptr);
424 time_t now_time_t = std::time(
nullptr);
462 ts.tv_nsec = 999999999;
496 << std::fixed << std::setprecision(17) << now_ud;
499 int64_t now_uni = ((719162LL * (24 * 60 * 60)) * (1000 * 1000 * 10)) +
511 #undef TEST_CONVERSION_ROUND_TRIP 514 template <
typename Duration>
516 return std::chrono::system_clock::from_time_t(0) + d;
537 const auto century_sec = 60 * 60 * 24 * 365 * int64_t{100};
539 const auto chrono_future = MakeChronoUnixTime(century);
540 const auto chrono_past = MakeChronoUnixTime(-century);
546 EXPECT_EQ(chrono_future,
548 EXPECT_EQ(chrono_past,
553 EXPECT_EQ(std::chrono::system_clock::from_time_t(-1),
555 EXPECT_EQ(std::chrono::system_clock::from_time_t(0),
557 EXPECT_EQ(std::chrono::system_clock::from_time_t(1),
560 EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(-1)),
562 EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(0)),
564 EXPECT_EQ(MakeChronoUnixTime(std::chrono::milliseconds(1)),
569 EXPECT_EQ(std::chrono::system_clock::from_time_t(0) -
570 std::chrono::system_clock::duration(1),
574 TEST(Time, TimeZoneAt) {
577 const std::string fmt =
"%a, %e %b %Y %H:%M:%S %z (%Z)";
581 const auto nov01_ci = nyc.
At(nov01);
583 EXPECT_EQ(
"Fri, 1 Nov 2013 08:30:00 -0400 (EDT)",
585 EXPECT_EQ(nov01_ci.pre, nov01_ci.trans);
586 EXPECT_EQ(nov01_ci.pre, nov01_ci.post);
593 const auto mar_ci = nyc.
At(mar13);
595 EXPECT_EQ(
"Sun, 13 Mar 2011 03:15:00 -0400 (EDT)",
597 EXPECT_EQ(
"Sun, 13 Mar 2011 03:00:00 -0400 (EDT)",
599 EXPECT_EQ(
"Sun, 13 Mar 2011 01:15:00 -0500 (EST)",
607 const auto nov06_ci = nyc.
At(nov06);
609 EXPECT_EQ(
"Sun, 6 Nov 2011 01:15:00 -0400 (EDT)",
611 EXPECT_EQ(
"Sun, 6 Nov 2011 01:00:00 -0500 (EST)",
613 EXPECT_EQ(
"Sun, 6 Nov 2011 01:15:00 -0500 (EST)",
619 const auto minus1_cl = nyc.
At(minus1);
622 EXPECT_EQ(
"Wed, 31 Dec 1969 18:59:59 -0500 (EST)",
624 EXPECT_EQ(
"Wed, 31 Dec 1969 23:59:59 +0000 (UTC)",
630 TEST(Time, FromCivilUTC) {
632 const std::string fmt =
"%a, %e %b %Y %H:%M:%S %z (%Z)";
633 const int kMax = std::numeric_limits<int>::max();
634 const int kMin = std::numeric_limits<int>::min();
640 EXPECT_EQ(
"Fri, 25 Nov 292277026596 12:21:07 +0000 (UTC)",
649 EXPECT_EQ(
"Fri, 1 Nov -292277022657 10:37:52 +0000 (UTC)",
657 EXPECT_EQ(
"Wed, 28 Feb 1900 23:59:59 +0000 (UTC)",
660 EXPECT_EQ(
"Thu, 1 Mar 1900 00:00:00 +0000 (UTC)",
663 EXPECT_EQ(
"Tue, 29 Feb 2000 23:59:59 +0000 (UTC)",
666 EXPECT_EQ(
"Wed, 1 Mar 2000 00:00:00 +0000 (UTC)",
680 const struct tm tm_bt =
ToTM(t, utc);
684 gmtime_s(&tm_lc, &tt);
686 gmtime_r(&tt, &tm_lc);
688 EXPECT_EQ(tm_lc.tm_year, tm_bt.tm_year);
689 EXPECT_EQ(tm_lc.tm_mon, tm_bt.tm_mon);
690 EXPECT_EQ(tm_lc.tm_mday, tm_bt.tm_mday);
691 EXPECT_EQ(tm_lc.tm_hour, tm_bt.tm_hour);
692 EXPECT_EQ(tm_lc.tm_min, tm_bt.tm_min);
693 EXPECT_EQ(tm_lc.tm_sec, tm_bt.tm_sec);
694 EXPECT_EQ(tm_lc.tm_wday, tm_bt.tm_wday);
695 EXPECT_EQ(tm_lc.tm_yday, tm_bt.tm_yday);
696 EXPECT_EQ(tm_lc.tm_isdst, tm_bt.tm_isdst);
698 ASSERT_FALSE(HasFailure());
705 struct tm tm =
ToTM(t, nyc);
706 EXPECT_FALSE(tm.tm_isdst);
711 EXPECT_TRUE(tm.tm_isdst);
715 EXPECT_EQ(std::numeric_limits<int>::max() - 1900, tm.tm_year);
716 EXPECT_EQ(11, tm.tm_mon);
717 EXPECT_EQ(31, tm.tm_mday);
718 EXPECT_EQ(23, tm.tm_hour);
719 EXPECT_EQ(59, tm.tm_min);
720 EXPECT_EQ(59, tm.tm_sec);
721 EXPECT_EQ(4, tm.tm_wday);
722 EXPECT_EQ(364, tm.tm_yday);
723 EXPECT_FALSE(tm.tm_isdst);
727 EXPECT_EQ(std::numeric_limits<int>::min(), tm.tm_year);
728 EXPECT_EQ(0, tm.tm_mon);
729 EXPECT_EQ(1, tm.tm_mday);
730 EXPECT_EQ(0, tm.tm_hour);
731 EXPECT_EQ(0, tm.tm_min);
732 EXPECT_EQ(0, tm.tm_sec);
733 EXPECT_EQ(0, tm.tm_wday);
734 EXPECT_EQ(0, tm.tm_yday);
735 EXPECT_FALSE(tm.tm_isdst);
744 std::memset(&tm, 0,
sizeof(tm));
745 tm.tm_year = 2014 - 1900;
762 tm.tm_year = 2014 - 1900;
779 tm.tm_year = 2014 - 1900;
796 TEST(Time, TMRoundTrip) {
804 struct tm tm =
ToTM(t, nyc);
813 struct tm tm =
ToTM(t, nyc);
822 struct tm tm =
ToTM(t, nyc);
834 for (
const auto base : bases) {
842 EXPECT_EQ(full_range, top - bottom);
843 EXPECT_EQ(-full_range, bottom - top);
860 EXPECT_LT(zero, max);
861 EXPECT_GT(zero, min);
875 TEST(Time, ConversionSaturation) {
879 const auto max_time_t = std::numeric_limits<time_t>::max();
880 const auto min_time_t = std::numeric_limits<time_t>::min();
881 time_t tt = max_time_t - 1;
884 EXPECT_EQ(max_time_t - 1, tt);
887 EXPECT_EQ(max_time_t, tt);
890 EXPECT_EQ(max_time_t, tt);
895 EXPECT_EQ(min_time_t + 1, tt);
898 EXPECT_EQ(min_time_t, tt);
901 EXPECT_EQ(min_time_t, tt);
903 const auto max_timeval_sec =
904 std::numeric_limits<decltype(timeval::tv_sec)>::max();
905 const auto min_timeval_sec =
906 std::numeric_limits<decltype(timeval::tv_sec)>::min();
908 tv.tv_sec = max_timeval_sec;
912 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
913 EXPECT_EQ(999998, tv.tv_usec);
916 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
917 EXPECT_EQ(999999, tv.tv_usec);
920 EXPECT_EQ(max_timeval_sec, tv.tv_sec);
921 EXPECT_EQ(999999, tv.tv_usec);
923 tv.tv_sec = min_timeval_sec;
927 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
928 EXPECT_EQ(1, tv.tv_usec);
931 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
932 EXPECT_EQ(0, tv.tv_usec);
935 EXPECT_EQ(min_timeval_sec, tv.tv_sec);
936 EXPECT_EQ(0, tv.tv_usec);
938 const auto max_timespec_sec =
939 std::numeric_limits<decltype(timespec::tv_sec)>::max();
940 const auto min_timespec_sec =
941 std::numeric_limits<decltype(timespec::tv_sec)>::min();
943 ts.tv_sec = max_timespec_sec;
944 ts.tv_nsec = 999999998;
947 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
948 EXPECT_EQ(999999998, ts.tv_nsec);
951 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
952 EXPECT_EQ(999999999, ts.tv_nsec);
955 EXPECT_EQ(max_timespec_sec, ts.tv_sec);
956 EXPECT_EQ(999999999, ts.tv_nsec);
958 ts.tv_sec = min_timespec_sec;
962 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
963 EXPECT_EQ(1, ts.tv_nsec);
966 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
967 EXPECT_EQ(0, ts.tv_nsec);
970 EXPECT_EQ(min_timespec_sec, ts.tv_sec);
971 EXPECT_EQ(0, ts.tv_nsec);
980 EXPECT_STREQ(
"-00", ci.zone_abbr);
987 EXPECT_STREQ(
"-00", ci.zone_abbr);
991 EXPECT_EQ(
"292277026596-12-04T15:30:06+00:00",
994 EXPECT_EQ(
"292277026596-12-04T15:30:07+00:00",
1002 EXPECT_EQ(
"292277026596-12-05T05:30:07+14:00",
1013 EXPECT_EQ(
"-292277022657-01-27T08:29:53+00:00",
1016 EXPECT_EQ(
"-292277022657-01-27T08:29:52+00:00",
1025 EXPECT_EQ(
"-292277022657-01-26T20:29:52-12:00",
1038 TEST(Time, ExtendedConversionSaturation) {
1077 TEST(Time, FromCivilAlignment) {
1094 TEST(Time, LegacyDateTime) {
1096 const std::string ymdhms =
"%Y-%m-%d %H:%M:%S";
1097 const int kMax = std::numeric_limits<int>::max();
1098 const int kMin = std::numeric_limits<int>::min();
1102 kMax, kMax, kMax, kMax, kMax, utc);
1103 EXPECT_EQ(
"infinite-future",
1106 kMin, kMin, kMin, kMin, kMin, utc);
1107 EXPECT_EQ(
"infinite-past",
1138 TEST(Time, NextTransitionUTC) {
1143 EXPECT_FALSE(tz.NextTransition(t, &trans));
1146 EXPECT_FALSE(tz.NextTransition(t, &trans));
1149 TEST(Time, PrevTransitionUTC) {
1154 EXPECT_FALSE(tz.PrevTransition(t, &trans));
1157 EXPECT_FALSE(tz.PrevTransition(t, &trans));
1160 TEST(Time, NextTransitionNYC) {
1165 EXPECT_TRUE(tz.NextTransition(t, &trans));
1170 EXPECT_FALSE(tz.NextTransition(t, &trans));
1173 EXPECT_TRUE(tz.NextTransition(t, &trans));
1184 TEST(Time, PrevTransitionNYC) {
1189 EXPECT_TRUE(tz.PrevTransition(t, &trans));
1194 EXPECT_FALSE(tz.PrevTransition(t, &trans));
1197 EXPECT_TRUE(tz.PrevTransition(t, &trans));
CivilInfo At(Time t) const
timespec ToTimespec(Duration d)
absl::TimeConversion ConvertDateTime(int64_t year, int mon, int day, int hour, int min, int sec, TimeZone tz)
Weekday GetWeekday(CivilDay cd)
constexpr Time InfiniteFuture()
constexpr Duration Hours(int64_t n)
#define EXPECT_CIVIL_INFO(ci, y, m, d, h, min, s, off, isdst)
std::string FormatTime(const std::string &format, absl::Time t, absl::TimeZone tz)
absl::Time FromUniversal(int64_t universal)
TimeZone FixedTimeZone(int seconds)
int64_t ToUnixMillis(Time t)
std::chrono::system_clock::time_point ToChronoTime(absl::Time t)
int64_t ToUniversal(absl::Time t)
const char RFC3339_full[]
#define TEST_CONVERSION_ROUND_TRIP(SOURCE, FROM, TO, MATCHER)
int GetYearDay(CivilDay cd)
constexpr Time FromUnixNanos(int64_t ns)
constexpr Time FromUnixMillis(int64_t ms)
constexpr Duration Microseconds(int64_t n)
int64_t ToUnixMicros(Time t)
std::chrono::duration< std::int_fast64_t > seconds
constexpr Duration Milliseconds(int64_t n)
static CONSTEXPR_F civil_time() max()
absl::Time FromTM(const struct tm &tm, absl::TimeZone tz)
constexpr Time FromUnixSeconds(int64_t s)
constexpr Time UnixEpoch()
timeval ToTimeval(Duration d)
constexpr Time FromUnixMicros(int64_t us)
int64_t GetCurrentTimeNanos()
absl::Time TimeFromTimespec(timespec ts)
constexpr Duration Minutes(int64_t n)
int64_t ToUnixSeconds(Time t)
#define TEST_FLOOR_CONVERSION(TO, FROM)
constexpr Time UniversalEpoch()
absl::Time FromUDate(double udate)
std::chrono::time_point< std::chrono::system_clock, D > time_point
struct tm ToTM(absl::Time t, absl::TimeZone tz)
constexpr Duration Seconds(int64_t n)
Time FromCivil(CivilSecond ct, TimeZone tz)
TEST(Symbolize, Unimplemented)
TimeZone LoadTimeZone(const std::string &name)
constexpr Duration Nanoseconds(int64_t n)
constexpr Duration InfiniteDuration()
Time FromDateTime(int64_t year, int mon, int day, int hour, int min, int sec, TimeZone tz)
constexpr Duration ZeroDuration()
int64_t ToUnixNanos(Time t)
constexpr Time FromTimeT(time_t t)
absl::Time TimeFromTimeval(timeval tv)
constexpr Time InfinitePast()
Time FromChrono(const std::chrono::system_clock::time_point &tp)