Specialize formatter for chrono durations

This commit is contained in:
Victor Zverovich 2018-11-28 09:23:23 -08:00
parent a5a9805a91
commit f5fe849238
2 changed files with 99 additions and 6 deletions

View File

@ -11,6 +11,13 @@
#include "format.h"
#include <ctime>
#ifndef FMT_USE_CHRONO
# define FMT_USE_CHRONO 0
#endif
#if FMT_USE_CHRONO
# include <chrono>
#endif
FMT_BEGIN_NAMESPACE
// Prevents expansion of a preceding token as a function-style macro.
@ -24,11 +31,9 @@ inline null<> gmtime_r(...) { return null<>(); }
inline null<> gmtime_s(...) { return null<>(); }
// Parses a put_time-like format string and invokes handler actions.
template <bool IS_CONSTEXPR, typename Char, typename Handler>
FMT_CONSTEXPR void parse_datetime_format(
basic_string_view<Char> format_str, Handler &&handler) {
auto begin = format_str.data();
auto end = begin + format_str.size();
template <typename Char, typename Handler>
FMT_CONSTEXPR const Char *parse_chrono_format(
const Char *begin, const Char *end, Handler &&handler) {
auto ptr = begin;
while (ptr != end) {
auto c = *ptr;
@ -65,13 +70,94 @@ FMT_CONSTEXPR void parse_datetime_format(
case 'B':
handler.on_full_month();
break;
// Hour, minute, second:
case 'S':
handler.on_second();
break;
// TODO: parse more format specifiers
}
}
if (begin != ptr)
handler.on_text(begin, ptr);
return ptr;
}
} // namespace internal
struct chrono_format_checker {
template <typename Char>
void on_text(const Char *, const Char *) {}
void on_abbr_weekday() {}
void on_full_weekday() {}
void on_dec0_weekday() {}
void on_dec1_weekday() {}
void on_abbr_month() {}
void on_full_month() {}
void on_second() {}
};
} // namespace internal
#if FMT_USE_CHRONO
namespace internal {
template <typename OutputIt, typename Char>
struct chrono_formatter {
OutputIt out;
std::chrono::seconds s;
std::chrono::milliseconds ms;
explicit chrono_formatter(OutputIt o) : out(o) {}
template <typename Int>
void write(Int value, int width) {
typedef typename int_traits<Int>::main_type main_type;
main_type n = value;
auto num_digits = internal::count_digits(n);
if (width > num_digits)
out = std::fill_n(out, width - num_digits, '0');
out = format_decimal<Char>(out, n, num_digits);
}
void on_text(const Char *, const Char *) {}
void on_abbr_weekday() {}
void on_full_weekday() {}
void on_dec0_weekday() {}
void on_dec1_weekday() {}
void on_abbr_month() {}
void on_full_month() {}
void on_second() {
write(s.count(), 2);
if (ms != std::chrono::milliseconds()) {
*out++ = '.';
write(ms.count(), 3);
}
}
};
} // namespace internal
template <typename Rep, typename Period, typename Char>
struct formatter<std::chrono::duration<Rep, Period>, Char> {
mutable basic_string_view<Char> format_str;
using Duration = std::chrono::duration<Rep, Period>;
FMT_CONSTEXPR auto parse(basic_parse_context<Char> &ctx)
-> decltype(ctx.begin()) {
auto begin = ctx.begin(), end = ctx.end();
end = parse_chrono_format(begin, end, internal::chrono_format_checker());
format_str = basic_string_view<Char>(&*begin, end - begin);
return end;
}
template <typename FormatContext>
auto format(const Duration &d, FormatContext &ctx)
-> decltype(ctx.out()) {
internal::chrono_formatter<decltype(ctx.out()), Char> f(ctx.out());
f.s = std::chrono::duration_cast<std::chrono::seconds>(d);
f.ms = std::chrono::duration_cast<std::chrono::milliseconds>(d - f.s);
parse_chrono_format(format_str.begin(), format_str.end(), f);
return f.out;
}
};
#endif // FMT_USE_CHRONO
// Thread-safe replacement for std::localtime
inline std::tm localtime(std::time_t time) {

View File

@ -57,3 +57,10 @@ TEST(TimeTest, GMTime) {
std::tm tm = *std::gmtime(&t);
EXPECT_TRUE(EqualTime(tm, fmt::gmtime(t)));
}
#if FMT_USE_CHRONO
TEST(TimeTest, Chrono) {
EXPECT_EQ("42", fmt::format("{:%S}", std::chrono::seconds(42)));
EXPECT_EQ("01.234", fmt::format("{:%S}", std::chrono::milliseconds(1234)));
}
#endif