vendredi 29 avril 2016

C++11 print current system time (including milliseconds)

I wish to print the current time (based on say std::chrono::system_clock) whilst including milliseconds, e.g. 12:32:45.287. I can do it without milliseconds, with:

std::time_t time = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
char buffer[sizeof("HH:MM:SS")];
if (std::strftime(buffer, sizeof(buffer), "%H:%M:%S", std::localtime(&time)) == 0)
    std::cout << "Error formating time" << std::endl;
else std::cout << buffer << std::endl;

It would also be good if I could get an integer number of milliseconds. (I can get seconds with std::localtime(&time)->tm_sec)

Aucun commentaire:

Enregistrer un commentaire