Working with Time
In C++, the <chrono>
library provides facilities for working with dates, times, and durations. This library is part of the C++ Standard Library and provides a high-level interface for manipulating time-related information.
To work with time in C++, include the <chrono>
and <ctime>
headers:
TEXT/X-C++SRC
1#include <iostream>
2#include <chrono>
3#include <ctime>
xxxxxxxxxx
19
int main() {
// Get the current time
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
// Convert to a time_t object
std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
// Convert to a string
std::string timeString = std::ctime(¤tTime);
// Print the current time
std::cout << "Current time: " << timeString;
return 0;
}
OUTPUT
:001 > Cmd/Ctrl-Enter to run, Cmd/Ctrl-/ to comment