Formatting time points in C++ can be done using the std::chrono
library combined with the std::put_time
function for output. C++11 and later Standards introduced a more consistent time library.
The following example demonstrates how to format a time point using C++:
#include
#include
#include
int main() {
// Get the current time point
auto now = std::chrono::system_clock::now();
// Convert it to time_t
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
// Format the time using std::put_time
std::cout << "Current time: " << std::put_time(std::localtime(&now_time_t), "%Y-%m-%d %H:%M:%S") << std::endl;
return 0;
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?