From 364de4a8e9645c369a42b6b11bba04f243f402b5 Mon Sep 17 00:00:00 2001 From: Craig Tiller Date: Fri, 18 Mar 2022 20:10:34 -0700 Subject: [PATCH] Add special logging for infinite times (#29158) --- src/core/lib/gprpp/time.cc | 12 ++++++++++++ test/core/gprpp/time_test.cc | 8 ++++++++ 2 files changed, 20 insertions(+) diff --git a/src/core/lib/gprpp/time.cc b/src/core/lib/gprpp/time.cc index f35dda2cced..c7f02d5c2f9 100644 --- a/src/core/lib/gprpp/time.cc +++ b/src/core/lib/gprpp/time.cc @@ -155,6 +155,12 @@ gpr_timespec Timestamp::as_timespec(gpr_clock_type clock_type) const { } std::string Timestamp::ToString() const { + if (millis_ == std::numeric_limits::max()) { + return "@∞"; + } + if (millis_ == std::numeric_limits::min()) { + return "@-∞"; + } return "@" + std::to_string(millis_) + "ms"; } @@ -167,6 +173,12 @@ Duration Duration::FromTimespec(gpr_timespec t) { } std::string Duration::ToString() const { + if (millis_ == std::numeric_limits::max()) { + return "∞"; + } + if (millis_ == std::numeric_limits::min()) { + return "-∞"; + } return std::to_string(millis_) + "ms"; } diff --git a/test/core/gprpp/time_test.cc b/test/core/gprpp/time_test.cc index 1e8ccb3866c..0a652b45997 100644 --- a/test/core/gprpp/time_test.cc +++ b/test/core/gprpp/time_test.cc @@ -35,6 +35,8 @@ TEST(TimestampTest, Infinities) { TEST(TimestampTest, ToString) { EXPECT_EQ(Timestamp::FromMillisecondsAfterProcessEpoch(42).ToString(), "@42ms"); + EXPECT_EQ(Timestamp::InfFuture().ToString(), "@∞"); + EXPECT_EQ(Timestamp::InfPast().ToString(), "@-∞"); } TEST(DurationTest, Empty) { EXPECT_EQ(Duration(), Duration::Zero()); } @@ -78,6 +80,12 @@ TEST(DurationTest, FromTimespan) { Duration::Milliseconds(1234)); } +TEST(DurationTest, ToString) { + EXPECT_EQ(Duration::Milliseconds(42).ToString(), "42ms"); + EXPECT_EQ(Duration::Infinity().ToString(), "∞"); + EXPECT_EQ(Duration::NegativeInfinity().ToString(), "-∞"); +} + } // namespace testing } // namespace grpc_core