Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for %k and %l chrono specifiers #3982

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions doc/syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,9 +271,10 @@ conversion_spec ::= "%" [modifier] chrono_type
literal_char ::= <a character other than '{', '}' or '%'>
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" |
"%"
</pre>

Literal chars are copied unchanged to the output. Precision is valid
Expand All @@ -300,6 +301,8 @@ The available presentation types (*chrono_type*) are:
| `'H'` | The hour (24-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command `%OH` produces the locale's alternative representation. |
| `'I'` | The hour (12-hour clock) as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command `%OI` produces the locale's alternative representation. |
| `'j'` | If the type being formatted is a specialization of duration, the decimal number of days without padding. Otherwise, the day of the 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 `%0k` 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 `%0l` 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. |
| `'M'` | The minute as a decimal number. If the result is a single digit, it is prefixed with 0. The modified command `%OM` produces the locale's alternative representation. |
| `'n'` | A new-line character. |
Expand Down
12 changes: 12 additions & 0 deletions include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

{
Expand All @@ -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");
}

{
Expand Down
Loading