I'm creating an RFC3339 timestamp, including milliseconds and in UTC, in C++ using std::chrono
like so:
#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
(forgive the using
s)
Is there a more straight forward way of getting the milliseconds of now
? It seems somewhat cumbersome to %1000
the now
in milliseconds to get there. Or any other comments on how to do this more idiomatic?