【发布时间】: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;
}
【问题讨论】: