From 4a0255b16e85233849ec34c48f428ad8d2936eda Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 14 Nov 2023 09:13:06 -0800 Subject: Support AbslStringify in absl Civil Time types. PiperOrigin-RevId: 582342910 Change-Id: Iada5420867779ed352d8fb0695ff6d6044acfba2 --- absl/time/civil_time.h | 26 ++++++++++++++++++++++++++ absl/time/civil_time_test.cc | 20 +++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) (limited to 'absl') 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 +void AbslStringify(Sink& sink, CivilSecond c) { + sink.Append(FormatCivilTime(c)); +} +template +void AbslStringify(Sink& sink, CivilMinute c) { + sink.Append(FormatCivilTime(c)); +} +template +void AbslStringify(Sink& sink, CivilHour c) { + sink.Append(FormatCivilTime(c)); +} +template +void AbslStringify(Sink& sink, CivilDay c) { + sink.Append(FormatCivilTime(c)); +} +template +void AbslStringify(Sink& sink, CivilMonth c) { + sink.Append(FormatCivilTime(c)); +} +template +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 #include -#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); { -- cgit v1.2.3