【问题标题】:std::time_point from and to std::stringstd::time_point 从和到 std::string
【发布时间】:2020-09-17 01:54:50
【问题描述】:

我正在尝试使用 c++20 std::chrono 替换一些 boost::gregorian 代码,希望消除 boost 构建依赖。代码正在读取和写入 json(使用 nlohmann),因此将日期与 std::string 相互转换的能力至关重要。

在 Ubuntu 20.04 上使用 g++ 9.3.0。 2 个编译时错误,一个在 std::chrono::parse() 上,第二个在 std::put_time()

对于 std::chrono::parse() 上的错误 A,我看到 here 包含 chrono::parse 的日历支持 (P0355R7) 在 gcc libstdc++ 中尚不可用。任何人都知道这是否正确或有指向此 ETA 的链接?还是我调用 parse() 的方式有问题?

对于 std::put_time() 的错误 B:因为 std:put_time() 被记录为 c++11,所以感觉我在这里遗漏了一些愚蠢的东西。还发现需要通过 c 的 time_t 和 tm 进行隐藏很奇怪。有没有更好的方法可以将 std::chrono::time_point 直接转换为 std::string 而不求助于 c?

#include <chrono>
#include <string>
#include <sstream>
#include <iostream>

int main(int argc, char *argv[]) {
    std::chrono::system_clock::time_point myDate;

    //Create time point from string
    //Ref: https://en.cppreference.com/w/cpp/chrono/parse
    std::stringstream ss;
    ss << "2020-05-24";
    ss >> std::chrono::parse("%Y-%m-%e", myDate);   //error A: ‘parse’ is not a member of ‘std::chrono’

    //Write time point to string
    //https://en.cppreference.com/w/cpp/io/manip/put_time
    //http://cgi.cse.unsw.edu.au/~cs6771/cppreference/en/cpp/chrono/time_point.html
    std::string dateString;
    std::time_t dateTime = std::chrono::system_clock::to_time_t(myDate);
    std::tm tm = *std::localtime(&dateTime);
    dateString = std::put_time(&tm, "%Y-%m-%e");    //error B: ‘put_time’ is not a member of ‘std’

    //Write out
    std::cout << "date: " << dateString << "\n";

    return 0;
}

【问题讨论】:

    标签: c++ c++20 chrono


    【解决方案1】:

    C++20 &lt;chrono&gt; 仍在为 gcc 构建。我没有看到任何公开的预计到达时间。

    std::chrono::parse 的语法看起来是正确的。如果您愿意使用free, open-source, header-only preview of C++20 &lt;chrono&gt;,则可以通过添加#include "date/date.h" 并改用date::parse 来使其工作。

    请注意,生成的 myDate 将为 2020-05-24 00:00:00 UTC。

    std::put_time 位于标头&lt;iomanip&gt; 中,是一个操纵器。添加该标题和&lt;iostream&gt; 后,您将像这样使用它:

    std::cout << "date: " << std::put_time(&tm, "%Y-%m-%e") << '\n';
    

    如果您需要 std::string 中的输出,则必须先将操纵器流式传输到 std::stringstream

    C++20 &lt;chrono&gt; 将提供替代 C API 的格式:

    std::cout << "date: " << std::format("{%Y-%m-%e}", myDate) << '\n';
    

    preview library 也提供了一个稍微改变的格式字符串:

    std::cout << "date: " << date::format("%Y-%m-%e", myDate) << '\n';
    

    【讨论】:

    • 谢谢霍华德,清楚 parse();包括 让 std::put_time() 工作
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-13
    • 1970-01-01
    • 2013-12-05
    • 1970-01-01
    • 2015-06-04
    • 1970-01-01
    相关资源
    最近更新 更多