summaryrefslogtreecommitdiff
path: root/absl
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2024-03-12 08:19:32 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2024-03-12 08:20:25 -0700
commit28e40003f899553197eacc81fdcd73b90b2d41c7 (patch)
treefbd327465df644c728774b889c515d1a2b71fc14 /absl
parent6cd8cf090d1ee521ad1aadeb961601d036f00cae (diff)
Use UnixEpoch and ZeroDuration
PiperOrigin-RevId: 615047295 Change-Id: I86e5de8543dc1ebee6d5a526846e9121b241b0fa
Diffstat (limited to 'absl')
-rw-r--r--absl/time/time_test.cc117
1 files changed, 61 insertions, 56 deletions
diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc
index bcf4f2ad..052d8806 100644
--- a/absl/time/time_test.cc
+++ b/absl/time/time_test.cc
@@ -14,6 +14,11 @@
#include "absl/time/time.h"
+#include <cstdint>
+#include <ios>
+
+#include "absl/time/civil_time.h"
+
#if defined(_MSC_VER)
#include <winsock2.h> // for timeval
#endif
@@ -77,21 +82,21 @@ MATCHER_P(TimevalMatcher, tv, "") {
TEST(Time, ConstExpr) {
constexpr absl::Time t0 = absl::UnixEpoch();
- static_assert(t0 == absl::Time(), "UnixEpoch");
+ static_assert(t0 == absl::UnixEpoch(), "UnixEpoch");
constexpr absl::Time t1 = absl::InfiniteFuture();
- static_assert(t1 != absl::Time(), "InfiniteFuture");
+ static_assert(t1 != absl::UnixEpoch(), "InfiniteFuture");
constexpr absl::Time t2 = absl::InfinitePast();
- static_assert(t2 != absl::Time(), "InfinitePast");
+ static_assert(t2 != absl::UnixEpoch(), "InfinitePast");
constexpr absl::Time t3 = absl::FromUnixNanos(0);
- static_assert(t3 == absl::Time(), "FromUnixNanos");
+ static_assert(t3 == absl::UnixEpoch(), "FromUnixNanos");
constexpr absl::Time t4 = absl::FromUnixMicros(0);
- static_assert(t4 == absl::Time(), "FromUnixMicros");
+ static_assert(t4 == absl::UnixEpoch(), "FromUnixMicros");
constexpr absl::Time t5 = absl::FromUnixMillis(0);
- static_assert(t5 == absl::Time(), "FromUnixMillis");
+ static_assert(t5 == absl::UnixEpoch(), "FromUnixMillis");
constexpr absl::Time t6 = absl::FromUnixSeconds(0);
- static_assert(t6 == absl::Time(), "FromUnixSeconds");
+ static_assert(t6 == absl::UnixEpoch(), "FromUnixSeconds");
constexpr absl::Time t7 = absl::FromTimeT(0);
- static_assert(t7 == absl::Time(), "FromTimeT");
+ static_assert(t7 == absl::UnixEpoch(), "FromTimeT");
}
TEST(Time, ValueSemantics) {
@@ -176,7 +181,7 @@ TEST(Time, RelationalOperators) {
constexpr absl::Time t2 = absl::FromUnixNanos(1);
constexpr absl::Time t3 = absl::FromUnixNanos(2);
- static_assert(absl::Time() == t1, "");
+ static_assert(absl::UnixEpoch() == t1, "");
static_assert(t1 == t1, "");
static_assert(t2 == t2, "");
static_assert(t3 == t3, "");
@@ -255,7 +260,7 @@ TEST(Time, FloorConversion) {
EXPECT_EQ(1, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(3) / 2));
EXPECT_EQ(1, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(1)));
EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(1) / 2));
- EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::Nanoseconds(0)));
+ EXPECT_EQ(0, absl::ToUnixNanos(absl::UnixEpoch() + absl::ZeroDuration()));
EXPECT_EQ(-1,
absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(1) / 2));
EXPECT_EQ(-1, absl::ToUnixNanos(absl::UnixEpoch() - absl::Nanoseconds(1)));
@@ -272,7 +277,7 @@ TEST(Time, FloorConversion) {
EXPECT_EQ(0,
absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(1)));
EXPECT_EQ(0,
- absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(0)));
+ absl::ToUniversal(absl::UniversalEpoch() + absl::ZeroDuration()));
EXPECT_EQ(-1,
absl::ToUniversal(absl::UniversalEpoch() + absl::Nanoseconds(-1)));
EXPECT_EQ(-1,
@@ -289,13 +294,13 @@ TEST(Time, FloorConversion) {
} to_ts[] = {
{absl::FromUnixSeconds(1) + absl::Nanoseconds(1), {1, 1}},
{absl::FromUnixSeconds(1) + absl::Nanoseconds(1) / 2, {1, 0}},
- {absl::FromUnixSeconds(1) + absl::Nanoseconds(0), {1, 0}},
- {absl::FromUnixSeconds(0) + absl::Nanoseconds(0), {0, 0}},
+ {absl::FromUnixSeconds(1) + absl::ZeroDuration(), {1, 0}},
+ {absl::FromUnixSeconds(0) + absl::ZeroDuration(), {0, 0}},
{absl::FromUnixSeconds(0) - absl::Nanoseconds(1) / 2, {-1, 999999999}},
{absl::FromUnixSeconds(0) - absl::Nanoseconds(1), {-1, 999999999}},
{absl::FromUnixSeconds(-1) + absl::Nanoseconds(1), {-1, 1}},
{absl::FromUnixSeconds(-1) + absl::Nanoseconds(1) / 2, {-1, 0}},
- {absl::FromUnixSeconds(-1) + absl::Nanoseconds(0), {-1, 0}},
+ {absl::FromUnixSeconds(-1) + absl::ZeroDuration(), {-1, 0}},
{absl::FromUnixSeconds(-1) - absl::Nanoseconds(1) / 2, {-2, 999999999}},
};
for (const auto& test : to_ts) {
@@ -306,12 +311,12 @@ TEST(Time, FloorConversion) {
absl::Time t;
} from_ts[] = {
{{1, 1}, absl::FromUnixSeconds(1) + absl::Nanoseconds(1)},
- {{1, 0}, absl::FromUnixSeconds(1) + absl::Nanoseconds(0)},
- {{0, 0}, absl::FromUnixSeconds(0) + absl::Nanoseconds(0)},
+ {{1, 0}, absl::FromUnixSeconds(1) + absl::ZeroDuration()},
+ {{0, 0}, absl::FromUnixSeconds(0) + absl::ZeroDuration()},
{{0, -1}, absl::FromUnixSeconds(0) - absl::Nanoseconds(1)},
{{-1, 999999999}, absl::FromUnixSeconds(0) - absl::Nanoseconds(1)},
{{-1, 1}, absl::FromUnixSeconds(-1) + absl::Nanoseconds(1)},
- {{-1, 0}, absl::FromUnixSeconds(-1) + absl::Nanoseconds(0)},
+ {{-1, 0}, absl::FromUnixSeconds(-1) + absl::ZeroDuration()},
{{-1, -1}, absl::FromUnixSeconds(-1) - absl::Nanoseconds(1)},
{{-2, 999999999}, absl::FromUnixSeconds(-1) - absl::Nanoseconds(1)},
};
@@ -319,36 +324,36 @@ TEST(Time, FloorConversion) {
EXPECT_EQ(test.t, absl::TimeFromTimespec(test.ts));
}
- // Tests ToTimeval()/TimeFromTimeval() (same as timespec above)
+ // Tests absl::ToTimeval()/TimeFromTimeval() (same as timespec above)
const struct {
absl::Time t;
timeval tv;
} to_tv[] = {
{absl::FromUnixSeconds(1) + absl::Microseconds(1), {1, 1}},
{absl::FromUnixSeconds(1) + absl::Microseconds(1) / 2, {1, 0}},
- {absl::FromUnixSeconds(1) + absl::Microseconds(0), {1, 0}},
- {absl::FromUnixSeconds(0) + absl::Microseconds(0), {0, 0}},
+ {absl::FromUnixSeconds(1) + absl::ZeroDuration(), {1, 0}},
+ {absl::FromUnixSeconds(0) + absl::ZeroDuration(), {0, 0}},
{absl::FromUnixSeconds(0) - absl::Microseconds(1) / 2, {-1, 999999}},
{absl::FromUnixSeconds(0) - absl::Microseconds(1), {-1, 999999}},
{absl::FromUnixSeconds(-1) + absl::Microseconds(1), {-1, 1}},
{absl::FromUnixSeconds(-1) + absl::Microseconds(1) / 2, {-1, 0}},
- {absl::FromUnixSeconds(-1) + absl::Microseconds(0), {-1, 0}},
+ {absl::FromUnixSeconds(-1) + absl::ZeroDuration(), {-1, 0}},
{absl::FromUnixSeconds(-1) - absl::Microseconds(1) / 2, {-2, 999999}},
};
for (const auto& test : to_tv) {
- EXPECT_THAT(ToTimeval(test.t), TimevalMatcher(test.tv));
+ EXPECT_THAT(absl::ToTimeval(test.t), TimevalMatcher(test.tv));
}
const struct {
timeval tv;
absl::Time t;
} from_tv[] = {
{{1, 1}, absl::FromUnixSeconds(1) + absl::Microseconds(1)},
- {{1, 0}, absl::FromUnixSeconds(1) + absl::Microseconds(0)},
- {{0, 0}, absl::FromUnixSeconds(0) + absl::Microseconds(0)},
+ {{1, 0}, absl::FromUnixSeconds(1) + absl::ZeroDuration()},
+ {{0, 0}, absl::FromUnixSeconds(0) + absl::ZeroDuration()},
{{0, -1}, absl::FromUnixSeconds(0) - absl::Microseconds(1)},
{{-1, 999999}, absl::FromUnixSeconds(0) - absl::Microseconds(1)},
{{-1, 1}, absl::FromUnixSeconds(-1) + absl::Microseconds(1)},
- {{-1, 0}, absl::FromUnixSeconds(-1) + absl::Microseconds(0)},
+ {{-1, 0}, absl::FromUnixSeconds(-1) + absl::ZeroDuration()},
{{-1, -1}, absl::FromUnixSeconds(-1) - absl::Microseconds(1)},
{{-2, 999999}, absl::FromUnixSeconds(-1) - absl::Microseconds(1)},
};
@@ -438,7 +443,7 @@ TEST(Time, RoundtripConversion) {
testing::Eq)
<< now_time_t;
- // TimeFromTimeval() and ToTimeval()
+ // TimeFromTimeval() and absl::ToTimeval()
timeval tv;
tv.tv_sec = -1;
tv.tv_usec = 0;
@@ -723,14 +728,14 @@ TEST(Time, FromCivilUTC) {
TEST(Time, ToTM) {
const absl::TimeZone utc = absl::UTCTimeZone();
- // Compares the results of ToTM() to gmtime_r() for lots of times over the
- // course of a few days.
+ // Compares the results of absl::ToTM() to gmtime_r() for lots of times over
+ // the course of a few days.
const absl::Time start =
absl::FromCivil(absl::CivilSecond(2014, 1, 2, 3, 4, 5), utc);
const absl::Time end =
absl::FromCivil(absl::CivilSecond(2014, 1, 5, 3, 4, 5), utc);
for (absl::Time t = start; t < end; t += absl::Seconds(30)) {
- const struct tm tm_bt = ToTM(t, utc);
+ const struct tm tm_bt = absl::ToTM(t, utc);
const time_t tt = absl::ToTimeT(t);
struct tm tm_lc;
#ifdef _WIN32
@@ -755,16 +760,16 @@ TEST(Time, ToTM) {
const absl::TimeZone nyc =
absl::time_internal::LoadTimeZone("America/New_York");
absl::Time t = absl::FromCivil(absl::CivilSecond(2014, 3, 1, 0, 0, 0), nyc);
- struct tm tm = ToTM(t, nyc);
+ struct tm tm = absl::ToTM(t, nyc);
EXPECT_FALSE(tm.tm_isdst);
// Checks that the tm_isdst field is correct when in daylight time.
t = absl::FromCivil(absl::CivilSecond(2014, 4, 1, 0, 0, 0), nyc);
- tm = ToTM(t, nyc);
+ tm = absl::ToTM(t, nyc);
EXPECT_TRUE(tm.tm_isdst);
// Checks overflow.
- tm = ToTM(absl::InfiniteFuture(), nyc);
+ tm = absl::ToTM(absl::InfiniteFuture(), nyc);
EXPECT_EQ(std::numeric_limits<int>::max() - 1900, tm.tm_year);
EXPECT_EQ(11, tm.tm_mon);
EXPECT_EQ(31, tm.tm_mday);
@@ -776,7 +781,7 @@ TEST(Time, ToTM) {
EXPECT_FALSE(tm.tm_isdst);
// Checks underflow.
- tm = ToTM(absl::InfinitePast(), nyc);
+ tm = absl::ToTM(absl::InfinitePast(), nyc);
EXPECT_EQ(std::numeric_limits<int>::min(), tm.tm_year);
EXPECT_EQ(0, tm.tm_mon);
EXPECT_EQ(1, tm.tm_mday);
@@ -802,13 +807,13 @@ TEST(Time, FromTM) {
tm.tm_min = 2;
tm.tm_sec = 3;
tm.tm_isdst = -1;
- absl::Time t = FromTM(tm, nyc);
+ absl::Time t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc)); // DST
tm.tm_isdst = 0;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc)); // DST
tm.tm_isdst = 1;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-06-28T01:02:03-04:00", absl::FormatTime(t, nyc)); // DST
// Adjusts tm to refer to an ambiguous time.
@@ -819,13 +824,13 @@ TEST(Time, FromTM) {
tm.tm_min = 30;
tm.tm_sec = 42;
tm.tm_isdst = -1;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-11-02T01:30:42-04:00", absl::FormatTime(t, nyc)); // DST
tm.tm_isdst = 0;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-11-02T01:30:42-05:00", absl::FormatTime(t, nyc)); // STD
tm.tm_isdst = 1;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-11-02T01:30:42-04:00", absl::FormatTime(t, nyc)); // DST
// Adjusts tm to refer to a skipped time.
@@ -836,13 +841,13 @@ TEST(Time, FromTM) {
tm.tm_min = 30;
tm.tm_sec = 42;
tm.tm_isdst = -1;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-03-09T03:30:42-04:00", absl::FormatTime(t, nyc)); // DST
tm.tm_isdst = 0;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-03-09T01:30:42-05:00", absl::FormatTime(t, nyc)); // STD
tm.tm_isdst = 1;
- t = FromTM(tm, nyc);
+ t = absl::FromTM(tm, nyc);
EXPECT_EQ("2014-03-09T03:30:42-04:00", absl::FormatTime(t, nyc)); // DST
// Adjusts tm to refer to a time with a year larger than 2147483647.
@@ -853,7 +858,7 @@ TEST(Time, FromTM) {
tm.tm_min = 2;
tm.tm_sec = 3;
tm.tm_isdst = -1;
- t = FromTM(tm, absl::UTCTimeZone());
+ t = absl::FromTM(tm, absl::UTCTimeZone());
EXPECT_EQ("2147483648-06-28T01:02:03+00:00",
absl::FormatTime(t, absl::UTCTimeZone()));
@@ -865,7 +870,7 @@ TEST(Time, FromTM) {
tm.tm_min = 2;
tm.tm_sec = 3;
tm.tm_isdst = -1;
- t = FromTM(tm, absl::UTCTimeZone());
+ t = absl::FromTM(tm, absl::UTCTimeZone());
EXPECT_EQ("178958989-08-28T01:02:03+00:00",
absl::FormatTime(t, absl::UTCTimeZone()));
}
@@ -878,8 +883,8 @@ TEST(Time, TMRoundTrip) {
absl::Time start = absl::FromCivil(absl::CivilHour(2014, 3, 9, 0), nyc);
absl::Time end = absl::FromCivil(absl::CivilHour(2014, 3, 9, 4), nyc);
for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
- struct tm tm = ToTM(t, nyc);
- absl::Time rt = FromTM(tm, nyc);
+ struct tm tm = absl::ToTM(t, nyc);
+ absl::Time rt = absl::FromTM(tm, nyc);
EXPECT_EQ(rt, t);
}
@@ -887,8 +892,8 @@ TEST(Time, TMRoundTrip) {
start = absl::FromCivil(absl::CivilHour(2014, 11, 2, 0), nyc);
end = absl::FromCivil(absl::CivilHour(2014, 11, 2, 4), nyc);
for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
- struct tm tm = ToTM(t, nyc);
- absl::Time rt = FromTM(tm, nyc);
+ struct tm tm = absl::ToTM(t, nyc);
+ absl::Time rt = absl::FromTM(tm, nyc);
EXPECT_EQ(rt, t);
}
@@ -896,8 +901,8 @@ TEST(Time, TMRoundTrip) {
start = absl::FromCivil(absl::CivilHour(2014, 6, 27, 22), nyc);
end = absl::FromCivil(absl::CivilHour(2014, 6, 28, 4), nyc);
for (absl::Time t = start; t < end; t += absl::Minutes(1)) {
- struct tm tm = ToTM(t, nyc);
- absl::Time rt = FromTM(tm, nyc);
+ struct tm tm = absl::ToTM(t, nyc);
+ absl::Time rt = absl::FromTM(tm, nyc);
EXPECT_EQ(rt, t);
}
}
@@ -985,30 +990,30 @@ TEST(Time, ConversionSaturation) {
tv.tv_sec = max_timeval_sec;
tv.tv_usec = 999998;
t = absl::TimeFromTimeval(tv);
- tv = ToTimeval(t);
+ tv = absl::ToTimeval(t);
EXPECT_EQ(max_timeval_sec, tv.tv_sec);
EXPECT_EQ(999998, tv.tv_usec);
t += absl::Microseconds(1);
- tv = ToTimeval(t);
+ tv = absl::ToTimeval(t);
EXPECT_EQ(max_timeval_sec, tv.tv_sec);
EXPECT_EQ(999999, tv.tv_usec);
t += absl::Microseconds(1); // no effect
- tv = ToTimeval(t);
+ tv = absl::ToTimeval(t);
EXPECT_EQ(max_timeval_sec, tv.tv_sec);
EXPECT_EQ(999999, tv.tv_usec);
tv.tv_sec = min_timeval_sec;
tv.tv_usec = 1;
t = absl::TimeFromTimeval(tv);
- tv = ToTimeval(t);
+ tv = absl::ToTimeval(t);
EXPECT_EQ(min_timeval_sec, tv.tv_sec);
EXPECT_EQ(1, tv.tv_usec);
t -= absl::Microseconds(1);
- tv = ToTimeval(t);
+ tv = absl::ToTimeval(t);
EXPECT_EQ(min_timeval_sec, tv.tv_sec);
EXPECT_EQ(0, tv.tv_usec);
t -= absl::Microseconds(1); // no effect
- tv = ToTimeval(t);
+ tv = absl::ToTimeval(t);
EXPECT_EQ(min_timeval_sec, tv.tv_sec);
EXPECT_EQ(0, tv.tv_usec);