Commit 52578edd by Abseil Team Committed by Copybara-Service

Support AbslStringify in absl::Time and absl::Duration.

PiperOrigin-RevId: 516363556
Change-Id: Iae5e781d46dc8a8c4242ab460b57b65271b93159
parent 7f47b00f
......@@ -91,6 +91,7 @@ cc_test(
"//absl/base:config",
"//absl/base:core_headers",
"//absl/numeric:int128",
"//absl/strings:str_format",
"//absl/time/internal/cctz:time_zone",
"@com_google_googletest//:gtest_main",
],
......
......@@ -122,6 +122,8 @@ absl_cc_test(
absl::time
absl::config
absl::core_headers
absl::strings
absl::str_format
absl::time_zone
GTest::gmock_main
)
......
......@@ -16,8 +16,8 @@
#include <winsock2.h> // for timeval
#endif
#include <chrono> // NOLINT(build/c++11)
#include <cfloat>
#include <chrono> // NOLINT(build/c++11)
#include <cmath>
#include <cstdint>
#include <ctime>
......@@ -28,6 +28,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/str_format.h"
#include "absl/time/time.h"
namespace {
......@@ -1853,4 +1854,11 @@ TEST(Duration, FormatParseRoundTrip) {
#undef TEST_PARSE_ROUNDTRIP
}
TEST(Duration, AbslStringify) {
// FormatDuration is already well tested, so just use one test case here to
// verify that StrFormat("%v", d) works as expected.
absl::Duration d = absl::Seconds(1);
EXPECT_EQ(absl::StrFormat("%v", d), absl::FormatDuration(d));
}
} // namespace
......@@ -609,6 +609,12 @@ inline std::ostream& operator<<(std::ostream& os, Duration d) {
return os << FormatDuration(d);
}
// Support for StrFormat(), StrCat() etc.
template <typename Sink>
void AbslStringify(Sink& sink, Duration d) {
sink.Append(FormatDuration(d));
}
// ParseDuration()
//
// Parses a duration string consisting of a possibly signed sequence of
......@@ -1386,6 +1392,12 @@ inline std::ostream& operator<<(std::ostream& os, Time t) {
return os << FormatTime(t);
}
// Support for StrFormat(), StrCat() etc.
template <typename Sink>
void AbslStringify(Sink& sink, Time t) {
sink.Append(FormatTime(t));
}
// ParseTime()
//
// Parses an input string according to the provided format string and
......
......@@ -28,6 +28,7 @@
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/numeric/int128.h"
#include "absl/strings/str_format.h"
#include "absl/time/clock.h"
#include "absl/time/internal/test_util.h"
......@@ -1287,4 +1288,11 @@ TEST(Time, PrevTransitionNYC) {
// We have a transition but we don't know which one.
}
TEST(Time, AbslStringify) {
// FormatTime is already well tested, so just use one test case here to
// verify that StrFormat("%v", t) works as expected.
absl::Time t = absl::Now();
EXPECT_EQ(absl::StrFormat("%v", t), absl::FormatTime(t));
}
} // namespace
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment