From e6cc65396937e0c6f5334d67f1a817f8c362aa27 Mon Sep 17 00:00:00 2001 From: Zaheen Jamil Date: Fri, 24 May 2024 23:36:21 +1000 Subject: [PATCH 1/2] Add %k, %l format specifiers --- include/fmt/chrono.h | 12 ++++++++++++ test/chrono-test.cc | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index d61b083273f9..ddf0efa739de 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -819,6 +819,12 @@ FMT_CONSTEXPR auto parse_chrono_format(const Char* begin, const Char* end, case 'I': handler.on_12_hour(numeric_system::standard, pad); break; + case 'k': + handler.on_24_hour(numeric_system::standard, pad_type::space); + break; + case 'l': + handler.on_12_hour(numeric_system::standard, pad_type::space); + break; case 'M': handler.on_minute(numeric_system::standard, pad); break; @@ -936,6 +942,12 @@ FMT_CONSTEXPR auto parse_chrono_format(const Char* begin, const Char* end, case 'M': handler.on_minute(numeric_system::alternative, pad); break; + case 'k': + handler.on_24_hour(numeric_system::alternative, pad_type::space); + break; + case 'l': + handler.on_12_hour(numeric_system::alternative, pad_type::space); + break; case 'S': handler.on_second(numeric_system::alternative, pad); break; diff --git a/test/chrono-test.cc b/test/chrono-test.cc index 1055c7982097..705316e73518 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -980,6 +980,8 @@ TEST(chrono_test, glibc_extensions) { EXPECT_EQ(fmt::format("{:%0OI,%0OH,%0OM,%0OS}", d), "01,01,02,03"); EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", d), " 1, 1, 2, 3"); EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", d), "1,1,2,3"); + + EXPECT_EQ(fmt::format("{:%k,%l}", d), " 1, 1"); } { @@ -993,6 +995,8 @@ TEST(chrono_test, glibc_extensions) { EXPECT_EQ(fmt::format("{:%0OI,%0OH,%0OM,%0OS}", tm), "01,01,02,03"); EXPECT_EQ(fmt::format("{:%_OI,%_OH,%_OM,%_OS}", tm), " 1, 1, 2, 3"); EXPECT_EQ(fmt::format("{:%-OI,%-OH,%-OM,%-OS}", tm), "1,1,2,3"); + + EXPECT_EQ(fmt::format("{:%k,%l}", tm), " 1, 1"); } { From d8d0cd34f2906c3247b8a2ce8afbeea05209195c Mon Sep 17 00:00:00 2001 From: Zaheen Jamil Date: Tue, 28 May 2024 05:19:18 +1000 Subject: [PATCH 2/2] Documentation --- doc/syntax.rst | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/doc/syntax.rst b/doc/syntax.rst index 16326c28d30d..ddd0cc7e897b 100644 --- a/doc/syntax.rst +++ b/doc/syntax.rst @@ -322,9 +322,10 @@ Format specifications for chrono duration and time point types as well as literal_char: modifier: "E" | "O" chrono_type: "a" | "A" | "b" | "B" | "c" | "C" | "d" | "D" | "e" | "F" | - : "g" | "G" | "h" | "H" | "I" | "j" | "m" | "M" | "n" | "p" | - : "q" | "Q" | "r" | "R" | "S" | "t" | "T" | "u" | "U" | "V" | - : "w" | "W" | "x" | "X" | "y" | "Y" | "z" | "Z" | "%" + : "g" | "G" | "h" | "H" | "I" | "j" | "k" | "l" | "m" | "M" | + : "n" | "p" | "q" | "Q" | "r" | "R" | "S" | "t" | "T" | "u" | + : "U" | "V" | "w" | "W" | "x" | "X" | "y" | "Y" | "z" | "Z" | + : "%" Literal chars are copied unchanged to the output. Precision is valid only for ``std::chrono::duration`` types with a floating-point representation type. @@ -392,6 +393,14 @@ The available presentation types (*chrono_type*) are: | | year as a decimal number. Jan 1 is 001. If the result is less than | | | three digits, it is left-padded with 0 to three digits. | +---------+--------------------------------------------------------------------+ +| ``'k'`` | The hour (24-hour clock) as a decimal number. If the result is a | +| | single digit, it is prefixed with a space. The modified command | +| | ``%Ok`` produces the locale's alternative representation. | ++---------+--------------------------------------------------------------------+ +| ``'l'`` | The hour (12-hour clock) as a decimal number. If the result is a | +| | single digit, it is prefixed with a space. The modified command | +| | ``%Ol`` produces the locale's alternative representation. | ++---------+--------------------------------------------------------------------+ | ``'m'`` | The month as a decimal number. Jan is 01. If the result is a | | | single digit, it is prefixed with 0. The modified command ``%Om`` | | | produces the locale's alternative representation. |