c++c++-chronostdformat

std::format specifier with multiple custom format specifiers


I want to print a row of time values in the format %H%M%S and align each "cell" to specific multiple of width using std::format. My current approach is this:

tps_headings += std::format("{1:%H:%M:%S:<{0}} ", width, tp_seconds);

whereas tp_seconds is a seconds duration from std::chrono and with is a size_t.

Which I derived from this answer.

However I can't seem to get it to work like this, std::format fails on parsing the string:

/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/bits/chrono_io.h:303:35: error: call to non-'constexpr' function 'void std::__throw_format_error(const char*)'
  303 |               __throw_format_error("chrono format error: '{' in chrono-specs");
      |               ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/bits/chrono_io.h:39:
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:156:3: note: 'void std::__throw_format_error(const char*)' declared here
  156 |   __throw_format_error(const char* __what)
      |   ^~~~~~~~~~~~~~~~~~~~
/home/myuser/dev2/aurora-light-controller/components/a_light_emitter/tables.ipp:243:43:   in 'constexpr' expansion of 'std::basic_format_string<char, const unsigned int&, const double&>("{1:.2f:<{0}} ")'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:3672:19:   in 'constexpr' expansion of '__scanner.std::__format::_Checking_scanner<char, unsigned int, double>::<anonymous>.std::__format::_Scanner<char>::_M_scan()'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:3482:30:   in 'constexpr' expansion of '((std::__format::_Scanner<char>*)this)->std::__format::_Scanner<char>::_M_on_replacement_field()'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:3534:15:   in 'constexpr' expansion of '((std::__format::_Scanner<char>*)this)->std::__format::_Scanner<char>::_M_format_arg(__id)'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:3610:33:   in 'constexpr' expansion of '((std::__format::_Checking_scanner<char, unsigned int, double>*)this)->std::__format::_Checking_scanner<char, unsigned int, double>::_M_parse_format_spec<unsigned int, double>(__id)'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:3627:41:   in 'constexpr' expansion of '((std::__format::_Checking_scanner<char, unsigned int, double>*)this)->std::__format::_Checking_scanner<char, unsigned int, double>::_M_parse_format_spec<double>((__id - 1))'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:3624:40:   in 'constexpr' expansion of '__f.std::formatter<double, char>::parse(((std::__format::_Checking_scanner<char, unsigned int, double>*)this)->std::__format::_Checking_scanner<char, unsigned int, double>::<anonymous>.std::__format::_Scanner<char>::_M_pc)'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:2025:26:   in 'constexpr' expansion of '((std::formatter<double, char>*)this)->std::formatter<double, char>::_M_f.std::__format::__formatter_fp<char>::parse((* & __pc))'
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:1410:48: error: call to non-'constexpr' function 'void std::__format::__failed_to_parse_format_spec()'
 1410 |         __format::__failed_to_parse_format_spec();
      |         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
/home/myuser/.espressif/tools/xtensa-esp-elf/esp-13.2.0_20230928/xtensa-esp-elf/xtensa-esp-elf/include/c++/13.2.0/format:185:3: note: 'void std::__format::__failed_to_parse_format_spec()' declared here
  185 |   __failed_to_parse_format_spec()
      |   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~

How can I fix this?


Solution

  • <{0} needs to come before the chrono specifiers.

    tps_headings += std::format("{1:<{0}%H:%M:%S} ", width, tp_seconds);
    

    See https://en.cppreference.com/w/cpp/chrono/duration/formatter#Format_specification for the grammar of the format specification.