I'm trying to format a duration with fmt's chrono impl and specify the precision for the seconds part.
I'm reading the documentation but think i don't understand how to do it; whatever i try, it either doesn't format as expected or the compiler reports an error.
Here's what i tried so far:
#include <chrono>
#include <ratio>
#define FMT_HEADER_ONLY
#include <fmt/chrono.h>
#include <iostream>
int main() {
std::chrono::duration<std::int64_t, std::nano> const d{3601123456789};
std::cout << fmt::format("1a {:%H:%M:%S}\n", d);
std::cout << fmt::format("1_ {: >15%S} <-- width works; precision won't compile\n", d);
// The fmt docs say that "Precision is valid only for std::chrono::duration types with a floating-point representation type."
// The following format doesn't compile with "d" whose ticks are represented as integer.
// It does with "dd" whose ticks are represented as double, but the result is still not as expected.
std::cout << fmt::format("2_ {:.2%S} <-- this is confusing; precision doesn't work\n", dd);
std::cout << fmt::format("2_ {: >15.2%S} <-- width works; precision doesn't\n", dd);
}
Edit:
Just checked with std::format on msvc; the results are the same.