Skip to content
Merged
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
15 changes: 14 additions & 1 deletion include/fmt/chrono.h
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ template <typename Char> struct formatter<std::tm, Char> {
enum class spec {
unknown,
year_month_day,
hh_mm_ss,
};
spec spec_ = spec::unknown;

Expand All @@ -563,7 +564,13 @@ template <typename Char> struct formatter<std::tm, Char> {
while (end != ctx.end() && *end != '}') ++end;
auto size = detail::to_unsigned(end - it);
specs = {it, size};
if (specs == string_view("%F", 2)) spec_ = spec::year_month_day;
// basic_string_view<>::compare isn't constexpr before C++17

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no basic_string_view<> before C++17.

Could it be that the internal replacement should be updated?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Problem in std::char_traits<>::compare:

fmt/include/fmt/core.h

Lines 472 to 478 in 3d0c7ae

FMT_CONSTEXPR_CHAR_TRAITS auto compare(basic_string_view other) const -> int {
size_t str_size = size_ < other.size_ ? size_ : other.size_;
int result = std::char_traits<Char>::compare(data_, other.data_, str_size);
if (result == 0)
result = size_ == other.size_ ? 0 : (size_ < other.size_ ? -1 : 1);
return result;
}

constexpr std::char_traits<>::compare starts from C++17 only

if (specs.size() == 2 && specs[0] == Char('%')) {
if (specs[1] == Char('F'))
spec_ = spec::year_month_day;
else if (specs[1] == Char('T'))
spec_ = spec::hh_mm_ss;
}
return end;
}

Expand All @@ -578,6 +585,12 @@ template <typename Char> struct formatter<std::tm, Char> {
detail::to_unsigned(tm.tm_mon + 1),
detail::to_unsigned(tm.tm_mday), '-');
return std::copy_n(buf, sizeof(buf), ctx.out());
} else if (spec_ == spec::hh_mm_ss) {
char buf[8];
detail::write_digit2_separated(buf, detail::to_unsigned(tm.tm_hour),
detail::to_unsigned(tm.tm_min),
detail::to_unsigned(tm.tm_sec), ':');
return std::copy_n(buf, sizeof(buf), ctx.out());
}
basic_memory_buffer<Char> tm_format;
tm_format.append(specs.begin(), specs.end());
Expand Down
1 change: 1 addition & 0 deletions test/chrono-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ TEST(chrono_test, format_tm) {
EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
"The date is 2016-04-25 11:22:33.");
EXPECT_EQ(fmt::format("{:%F}", tm), "2016-04-25");
EXPECT_EQ(fmt::format("{:%T}", tm), "11:22:33");
}

TEST(chrono_test, grow_buffer) {
Expand Down
2 changes: 2 additions & 0 deletions test/xchar-test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ TEST(xchar_test, chrono) {
EXPECT_EQ(fmt::format("The date is {:%Y-%m-%d %H:%M:%S}.", tm),
"The date is 2016-04-25 11:22:33.");
EXPECT_EQ(L"42s", fmt::format(L"{}", std::chrono::seconds(42)));
EXPECT_EQ(fmt::format(L"{:%F}", tm), L"2016-04-25");
EXPECT_EQ(fmt::format(L"{:%T}", tm), L"11:22:33");
}

TEST(xchar_test, color) {
Expand Down