summaryrefslogtreecommitdiff
path: root/absl
diff options
context:
space:
mode:
Diffstat (limited to 'absl')
-rw-r--r--absl/time/civil_time.h26
-rw-r--r--absl/time/civil_time_test.cc20
2 files changed, 45 insertions, 1 deletions
diff --git a/absl/time/civil_time.h b/absl/time/civil_time.h
index 5855bc73..3e904a11 100644
--- a/absl/time/civil_time.h
+++ b/absl/time/civil_time.h
@@ -462,6 +462,32 @@ std::string FormatCivilTime(CivilDay c);
std::string FormatCivilTime(CivilMonth c);
std::string FormatCivilTime(CivilYear c);
+// Support for StrFormat(), StrCat(), etc
+template <typename Sink>
+void AbslStringify(Sink& sink, CivilSecond c) {
+ sink.Append(FormatCivilTime(c));
+}
+template <typename Sink>
+void AbslStringify(Sink& sink, CivilMinute c) {
+ sink.Append(FormatCivilTime(c));
+}
+template <typename Sink>
+void AbslStringify(Sink& sink, CivilHour c) {
+ sink.Append(FormatCivilTime(c));
+}
+template <typename Sink>
+void AbslStringify(Sink& sink, CivilDay c) {
+ sink.Append(FormatCivilTime(c));
+}
+template <typename Sink>
+void AbslStringify(Sink& sink, CivilMonth c) {
+ sink.Append(FormatCivilTime(c));
+}
+template <typename Sink>
+void AbslStringify(Sink& sink, CivilYear c) {
+ sink.Append(FormatCivilTime(c));
+}
+
// absl::ParseCivilTime()
//
// Parses a civil-time value from the specified `absl::string_view` into the
diff --git a/absl/time/civil_time_test.cc b/absl/time/civil_time_test.cc
index c7a085bf..19292a9c 100644
--- a/absl/time/civil_time_test.cc
+++ b/absl/time/civil_time_test.cc
@@ -19,8 +19,9 @@
#include <sstream>
#include <type_traits>
-#include "absl/base/macros.h"
#include "gtest/gtest.h"
+#include "absl/base/macros.h"
+#include "absl/strings/str_format.h"
namespace {
@@ -869,6 +870,23 @@ TEST(CivilTime, ParseEdgeCases) {
EXPECT_FALSE(absl::ParseLenientCivilTime("9223372036854775808", &y)) << y;
}
+TEST(CivilTime, AbslStringify) {
+ EXPECT_EQ("2015-01-02T03:04:05",
+ absl::StrFormat("%v", absl::CivilSecond(2015, 1, 2, 3, 4, 5)));
+
+ EXPECT_EQ("2015-01-02T03:04",
+ absl::StrFormat("%v", absl::CivilMinute(2015, 1, 2, 3, 4)));
+
+ EXPECT_EQ("2015-01-02T03",
+ absl::StrFormat("%v", absl::CivilHour(2015, 1, 2, 3)));
+
+ EXPECT_EQ("2015-01-02", absl::StrFormat("%v", absl::CivilDay(2015, 1, 2)));
+
+ EXPECT_EQ("2015-01", absl::StrFormat("%v", absl::CivilMonth(2015, 1)));
+
+ EXPECT_EQ("2015", absl::StrFormat("%v", absl::CivilYear(2015)));
+}
+
TEST(CivilTime, OutputStream) {
absl::CivilSecond cs(2016, 2, 3, 4, 5, 6);
{