jeudi 26 mars 2020

What is the best approach to get a millisecond-rounded timestamp string in Howard Hinnant's Date library?

I have the following code, using Date library:

#include "date.h"
#include <iostream>
#include <sstream>

using namespace date;
using namespace std::chrono;

int main()
{
    auto now = system_clock::now();
    std::stringstream ss;
    ss << now;
    std::string nowStr = ss.str();   // I need a string
    std::cout << nowStr << " UTC\n";
}

The result is:

2020-03-26 17:38:24.473372486 UTC

Is stringstream the correct approach to obtain a string from the chrono::timepoint that now() returns? And, if so, how can I round those nanoseconds to milliseconds?

Aucun commentaire:

Enregistrer un commentaire