diff options
author | Abseil Team <absl-team@google.com> | 2023-11-14 09:13:06 -0800 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-11-14 09:13:48 -0800 |
commit | 4a0255b16e85233849ec34c48f428ad8d2936eda (patch) | |
tree | 9a3e52cd268589fa6a64cd9437fde2d2204bd5d5 /absl | |
parent | 716fa00789b60ff52473eabc3ac201eb61744392 (diff) |
Support AbslStringify in absl Civil Time types.
PiperOrigin-RevId: 582342910
Change-Id: Iada5420867779ed352d8fb0695ff6d6044acfba2
Diffstat (limited to 'absl')
-rw-r--r-- | absl/time/civil_time.h | 26 | ||||
-rw-r--r-- | absl/time/civil_time_test.cc | 20 |
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); { |