Fixed absolute values, correct subsecond width and zero padding

This commit is contained in:
Filip Brcic 2021-05-18 21:00:39 +02:00
parent e2c27043c1
commit 3bf4abab98
No known key found for this signature in database
GPG Key ID: 15FB92681DE569DA
2 changed files with 33 additions and 10 deletions

View File

@ -405,18 +405,34 @@ inline size_t strftime(wchar_t* str, size_t count, const wchar_t* format,
FMT_END_DETAIL_NAMESPACE FMT_END_DETAIL_NAMESPACE
template <typename Char, typename Duration> template <class Rep, class Period, class = std::enable_if<
struct formatter<std::chrono::time_point<std::chrono::system_clock, Duration>, std::chrono::duration<Rep, Period>::min() < std::chrono::duration<Rep, Period>::zero()>>
constexpr std::chrono::duration<Rep, Period> abs(std::chrono::duration<Rep, Period> d)
{
return d >= d.zero() ? d : -d;
}
template <typename Char, typename Rep, typename Period>
struct formatter<std::chrono::time_point<std::chrono::system_clock, std::chrono::duration<Rep, Period>>,
Char> : formatter<std::tm, Char> { Char> : formatter<std::tm, Char> {
template <typename FormatContext> template <typename FormatContext>
auto format(std::chrono::time_point<std::chrono::system_clock> val, auto format(std::chrono::time_point<std::chrono::system_clock> val,
FormatContext& ctx) -> decltype(ctx.out()) { FormatContext& ctx) -> decltype(ctx.out()) {
std::tm time = localtime(val); std::tm time = localtime(val);
auto epoch = val.time_since_epoch(); auto epoch = val.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::milliseconds>(epoch).count() / 1000; auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch);
auto today = std::chrono::high_resolution_clock::duration(std::chrono::milliseconds(seconds * 1000)); auto subseconds = abs(std::chrono::duration_cast<std::chrono::duration<Rep, Period>>(epoch - seconds));
auto subseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(epoch - today);
return formatter<std::tm, Char>::format(time, ctx, subseconds.count()); if (subseconds.count() > 0) {
auto width = std::to_string(Period::den).size() - 1;
std::basic_stringstream<Char> os;
os.fill('0');
os.width(width);
os << subseconds.count();
auto formatted_ss = os.str();
return formatter<std::tm, Char>::format(time, ctx, formatted_ss);
}
return formatter<std::tm, Char>::format(time, ctx);
} }
}; };
@ -432,7 +448,7 @@ template <typename Char> struct formatter<std::tm, Char> {
} }
template <typename FormatContext> template <typename FormatContext>
auto format(const std::tm& tm, FormatContext& ctx, int subseconds = 0) const auto format(const std::tm& tm, FormatContext& ctx, const std::basic_string<Char> subseconds = {}) const
-> decltype(ctx.out()) { -> decltype(ctx.out()) {
basic_memory_buffer<Char> tm_format; basic_memory_buffer<Char> tm_format;
tm_format.append(specs.begin(), specs.end()); tm_format.append(specs.begin(), specs.end());
@ -441,8 +457,9 @@ template <typename Char> struct formatter<std::tm, Char> {
// https://github.com/fmtlib/fmt/issues/2238 // https://github.com/fmtlib/fmt/issues/2238
auto hasS = std::find(tm_format.begin(), tm_format.end(), 'S') != tm_format.end(); auto hasS = std::find(tm_format.begin(), tm_format.end(), 'S') != tm_format.end();
auto hasT = std::find(tm_format.begin(), tm_format.end(), 'T') != tm_format.end(); auto hasT = std::find(tm_format.begin(), tm_format.end(), 'T') != tm_format.end();
auto writeSubseconds = (hasS || hasT) && (subseconds != 0); auto writeSubseconds = (hasS || hasT) && (subseconds.size() > 0);
if (writeSubseconds) if (writeSubseconds)
// should be std::use_facet<std::numpunct<Char>>(locale).decimal_point();
tm_format.push_back('.'); tm_format.push_back('.');
else else
tm_format.push_back(' '); tm_format.push_back(' ');
@ -460,12 +477,14 @@ template <typename Char> struct formatter<std::tm, Char> {
buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH)); buf.reserve(buf.capacity() + (size > MIN_GROWTH ? size : MIN_GROWTH));
} }
auto buf_end = buf.end() - 1;
if (writeSubseconds) { if (writeSubseconds) {
buf.append(std::to_string(subseconds)); buf.append(subseconds);
buf_end = buf.end();
} }
// Remove the extra space. // Remove the extra space.
return std::copy(buf.begin(), buf.end() - 1, ctx.out()); return std::copy(buf.begin(), buf_end, ctx.out());
} }
basic_string_view<Char> specs; basic_string_view<Char> specs;

View File

@ -100,6 +100,10 @@ TEST(chrono_test, time_point) {
std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>; std::chrono::time_point<std::chrono::system_clock, std::chrono::seconds>;
auto t2 = time_point(std::chrono::seconds(42)); auto t2 = time_point(std::chrono::seconds(42));
EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2)); EXPECT_EQ(strftime(t2), fmt::format("{:%Y-%m-%d %H:%M:%S}", t2));
using time_point_2 = std::chrono::time_point<std::chrono::system_clock,
std::chrono::milliseconds>;
auto t3 = time_point_2(std::chrono::milliseconds(1023));
EXPECT_EQ("01.023", fmt::format("{:%S}", t3));
} }
#ifndef FMT_STATIC_THOUSANDS_SEPARATOR #ifndef FMT_STATIC_THOUSANDS_SEPARATOR