【问题标题】:Cannot use std::chrono::parse even with -std=c++2a and g++-11即使使用 -std=c++2a 和 g++-11 也无法使用 std::chrono::parse
【发布时间】: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 是否在其标准库中宣传对此的支持?
  • 可能是stackoverflow.com/questions/67906096/…的重复问题?
  • en.cppreference.com/w/cpp/compiler_support,只有visual studio有完整的实现(搜索“日历和时区”)
  • 该死的。我发现这很令人沮丧,以至于 C 或 C++ 中没有默认方法来解析具有微秒精度的时间戳。我最终滚动自己的sscanf 写入struct tm 为除ts_sec 之外的所有字段,我将其设置为零。然后我使用timegm 以秒为单位获取时间戳,并将其与我在 scanf 中解析的秒数的浮点数相加(考虑到 c 的单位)。

标签: c++ g++


【解决方案1】:

在您最喜欢的 C++ 供应商发布 std::chrono::parse 之前,存在一个 free, open-source preview of this part of C++20

std::chrono::sys_time<std::chrono::microseconds> timestamp;
std::stringstream ss = foo();
ss >> date::parse("%Y-%m-%d %T", timestamp);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 2012-08-21
    • 1970-01-01
    • 2017-05-22
    • 1970-01-01
    • 2023-04-03
    • 2018-09-12
    相关资源
    最近更新 更多