Support floating point sec
This commit is contained in:
parent
f6efeba365
commit
43c2eae78d
@ -676,6 +676,19 @@ enum class pad_type {
|
|||||||
space,
|
space,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
template <typename OutputIt>
|
||||||
|
auto write_padding(OutputIt out, int width, pad_type pad) {
|
||||||
|
switch (pad) {
|
||||||
|
case pad_type::zero:
|
||||||
|
case pad_type::unspecified:
|
||||||
|
return std::fill_n(out, width, '0');
|
||||||
|
case pad_type::space:
|
||||||
|
return std::fill_n(out, width, ' ');
|
||||||
|
case pad_type::none:
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parses a put_time-like format string and invokes handler actions.
|
// Parses a put_time-like format string and invokes handler actions.
|
||||||
template <typename Char, typename Handler>
|
template <typename Char, typename Handler>
|
||||||
FMT_CONSTEXPR const Char* parse_chrono_format(const Char* begin,
|
FMT_CONSTEXPR const Char* parse_chrono_format(const Char* begin,
|
||||||
@ -1878,17 +1891,7 @@ struct chrono_formatter {
|
|||||||
to_unsigned(to_nonnegative_int(value, max_value<int>()));
|
to_unsigned(to_nonnegative_int(value, max_value<int>()));
|
||||||
int num_digits = detail::count_digits(n);
|
int num_digits = detail::count_digits(n);
|
||||||
if (width > num_digits) {
|
if (width > num_digits) {
|
||||||
switch (pad) {
|
out = detail::write_padding(out, width - num_digits, pad);
|
||||||
case pad_type::zero:
|
|
||||||
case pad_type::unspecified:
|
|
||||||
out = std::fill_n(out, width - num_digits, '0');
|
|
||||||
break;
|
|
||||||
case pad_type::space:
|
|
||||||
out = std::fill_n(out, width - num_digits, ' ');
|
|
||||||
break;
|
|
||||||
case pad_type::none:
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
out = format_decimal<char_type>(out, n, num_digits).end;
|
out = format_decimal<char_type>(out, n, num_digits).end;
|
||||||
}
|
}
|
||||||
@ -1974,7 +1977,9 @@ struct chrono_formatter {
|
|||||||
write_floating_seconds(buf, std::chrono::duration<rep, Period>(val),
|
write_floating_seconds(buf, std::chrono::duration<rep, Period>(val),
|
||||||
precision);
|
precision);
|
||||||
if (negative) *out++ = '-';
|
if (negative) *out++ = '-';
|
||||||
if (buf.size() < 2 || buf[1] == '.') *out++ = '0';
|
if (buf.size() < 2 || buf[1] == '.') {
|
||||||
|
out = detail::write_padding(out, 1, pad);
|
||||||
|
}
|
||||||
out = std::copy(buf.begin(), buf.end(), out);
|
out = std::copy(buf.begin(), buf.end(), out);
|
||||||
} else {
|
} else {
|
||||||
write(second(), 2, pad);
|
write(second(), 2, pad);
|
||||||
|
|||||||
@ -949,4 +949,19 @@ TEST(chrono_test, glibc_extensions) {
|
|||||||
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");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const auto d = std::chrono::seconds(3) + std::chrono::milliseconds(140);
|
||||||
|
EXPECT_EQ(fmt::format("{:%S}", d), "03.140");
|
||||||
|
EXPECT_EQ(fmt::format("{:%0S}", d), "03.140");
|
||||||
|
EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140");
|
||||||
|
EXPECT_EQ(fmt::format("{:%-S}", d), "3.140");
|
||||||
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
const auto d = std::chrono::duration<double>(3.14);
|
||||||
|
EXPECT_EQ(fmt::format("{:%S}", d), "03.140000");
|
||||||
|
EXPECT_EQ(fmt::format("{:%0S}", d), "03.140000");
|
||||||
|
EXPECT_EQ(fmt::format("{:%_S}", d), " 3.140000");
|
||||||
|
EXPECT_EQ(fmt::format("{:%-S}", d), "3.140000");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user