1 | initial version |
One method to get a printable output of a C++11 timepoint is to use the std::puttime function in combination with a std::tm structure.
Here's an example code snippet:
#include <iostream>
#include <iomanip>
#include <chrono>
int main() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm* now_tm = std::localtime(&now_c);
std::cout << "Current date and time: " << std::put_time(now_tm, "%c") << '\n';
return 0;
}
In this example, we first obtain the current system time as a timepoint using std::chrono::systemclock::now(). We then convert this timepoint to a timet value using std::chrono::systemclock::totimet(). This timet value can be used to obtain a std::tm structure using std::localtime(). Finally, we use std::put_time() to format the std::tm structure as a printable output. The "%c" format specifier represents the preferred date and time representation for the current locale.