【发布时间】:2021-08-12 11:39:45
【问题描述】:
尝试编译以下示例时:
std::chrono::sys_time<std::chrono::microseconds> timestamp;
std::stringstream ss = foo();
ss >> std::chrono::parse("%Y-%m-%d %T", timestamp);
我明白了:
error: ‘parse’ is not a member of ‘std::chrono’
15 | ss >> std::chrono::parse("%Y-%m-%d %T", timestamp);
| ^~~~~
我没想到会这样,因为我使用的是我能找到的最新的g++。
更多信息:
$ g++-11 --version
g++-11 (Ubuntu 11.1.0-1ubuntu1~18.04.1) 11.1.0
$ g++-11 -v
Using built-in specs.
COLLECT_GCC=g++-11
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/11/lto-wrapper
OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa
OFFLOAD_TARGET_DEFAULT=1
Target: x86_64-linux-gnu
Configured with: (...)
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 11.1.0 (Ubuntu 11.1.0-1ubuntu1~18.04.1)
我正在编译:
g++-11 -std=c++2a -o test time.cc
g++-11.1.0 不支持这个功能吗?
【问题讨论】:
-
GCC 是否在其标准库中宣传对此的支持?
-
见en.cppreference.com/w/cpp/compiler_support,只有visual studio有完整的实现(搜索“日历和时区”)
-
该死的。我发现这很令人沮丧,以至于 C 或 C++ 中没有默认方法来解析具有微秒精度的时间戳。我最终滚动自己的
sscanf写入struct tm为除ts_sec之外的所有字段,我将其设置为零。然后我使用timegm以秒为单位获取时间戳,并将其与我在 scanf 中解析的秒数的浮点数相加(考虑到 c 的单位)。