【发布时间】:2019-01-23 10:30:20
【问题描述】:
我正在使用 std::chrono 在 C++ 中创建一个 RFC3339 时间戳,包括毫秒和 UTC,如下所示:
#include <chrono>
#include <ctime>
#include <iomanip>
using namespace std;
using namespace std::chrono;
string now_rfc3339() {
const auto now = system_clock::now();
const auto millis = duration_cast<milliseconds>(now.time_since_epoch()).count() % 1000;
const auto c_now = system_clock::to_time_t(now);
stringstream ss;
ss << put_time(gmtime(&c_now), "%FT%T") <<
'.' << setfill('0') << setw(3) << millis << 'Z';
return ss.str();
}
// output like 2019-01-23T10:18:32.079Z
(原谅usings)
有没有更直接的方法来获取now 的毫秒数? %1000 now 在毫秒内到达那里似乎有些麻烦。或任何其他 cmet 如何做到这一点更惯用?
【问题讨论】: