diff options
author | Abseil Team <absl-team@google.com> | 2023-03-13 17:04:58 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-03-13 17:05:47 -0700 |
commit | 52578edd8950227f1eb28821622c861804241de6 (patch) | |
tree | b5d91d0b9d53f10e45937cc6c5157c6ae22c683a | |
parent | 7f47b00fca75bee477c8a2e3e8fc74a1cf7c743f (diff) |
Support AbslStringify in absl::Time and absl::Duration.
PiperOrigin-RevId: 516363556
Change-Id: Iae5e781d46dc8a8c4242ab460b57b65271b93159
-rw-r--r-- | absl/time/BUILD.bazel | 1 | ||||
-rw-r--r-- | absl/time/CMakeLists.txt | 2 | ||||
-rw-r--r-- | absl/time/duration_test.cc | 10 | ||||
-rw-r--r-- | absl/time/time.h | 12 | ||||
-rw-r--r-- | absl/time/time_test.cc | 8 |
5 files changed, 32 insertions, 1 deletions
diff --git a/absl/time/BUILD.bazel b/absl/time/BUILD.bazel index c7b07c2f..88d20887 100644 --- a/absl/time/BUILD.bazel +++ b/absl/time/BUILD.bazel @@ -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", ], diff --git a/absl/time/CMakeLists.txt b/absl/time/CMakeLists.txt index 7b720540..b3124251 100644 --- a/absl/time/CMakeLists.txt +++ b/absl/time/CMakeLists.txt @@ -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 ) diff --git a/absl/time/duration_test.cc b/absl/time/duration_test.cc index b7abf4ba..76d3217b 100644 --- a/absl/time/duration_test.cc +++ b/absl/time/duration_test.cc @@ -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 diff --git a/absl/time/time.h b/absl/time/time.h index cc390082..01b55322 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -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 diff --git a/absl/time/time_test.cc b/absl/time/time_test.cc index d235e9ad..6a89399d 100644 --- a/absl/time/time_test.cc +++ b/absl/time/time_test.cc @@ -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 |