Formatting ISO time using fmtlib

I’m trying to get a std::string of ISO time using fmtlib. Consider the following code:

const auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
const auto creation_time = fmt::format("{:%FT%T%z}", fmt::localtime(t));

I’m getting the following error:

../deps/fmt-src/include/fmt/chrono.h: In function ‘size_t fmt::v7::detail::strftime(char*, size_t, const char*, const tm*)’: ../deps/fmt-src/include/fmt/chrono.h:377:48: error: format not a string literal, format string not checked [-Werror=format-nonliteral] return std::strftime(str, count, format, time);

Which I don’t get, as clearly fmt::localtime(t) is not a compile-time string.

What am I missing?

Using fmtlib v7.1.3, compiled using the following flags to work on embedded system:

target_compile_definitions(fmt PRIVATE FMT_STATIC_THOUSANDS_SEPARATOR FMT_USE_DOUBLE=0 FMT_USE_LONG_DOUBLE=0)

Answer

The problem is that you are compiling with the -Werror=format-nonliteral compiler flag while the chrono formatter calls strftime with a dynamic format string. The solution is to remove -Werror=format-nonliteral or add -Wno-error=format-nonliteral.

Alternatively you can use {fmt} 5a37e1 or later where this warning is suppressed.