使用std::format格式化chrono时间并对齐时解析失败的修复方法
问题描述
我想用std::format打印%H:%M:%S格式的时间值,并且把每个“单元格”对齐到width的特定倍数。当前代码如下:
tps_headings += std::format("{1:%H:%M:%S:<{0}} ", width, tp_seconds);
其中tp_seconds是std::chrono的秒时长,width是size_t类型。运行时std::format解析字符串失败,报错信息如下:
/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() | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
修复方案
问题根源是格式字符串的写法错误:std::chrono的格式说明(%H:%M:%S)和对齐/宽度参数的嵌套写法冲突,编译器会把<{0}当成chrono格式的一部分,导致解析失败。
以下两种方法可以解决问题:
方法一:拆分格式步骤
先将时间格式化为字符串,再对字符串做对齐处理:
auto time_str = std::format("{:%H:%M:%S}", tp_seconds); tps_headings += std::format("{:<{}} ", time_str, width);
方法二:调整格式字符串结构
将对齐和宽度参数放在chrono格式说明的前面,作为整个替换字段的格式修饰,而非chrono格式的一部分:
tps_headings += std::format("{1:<{0}:%H:%M:%S} ", width, tp_seconds);
内容的提问来源于stack exchange,提问作者glades
相关产品推荐
相关产品推荐

