Obtaining the current date and time with std::chrono

In C++, the <chrono> library provides a convenient way to work with time durations and time points. With std::chrono, you can easily obtain the current date and time. Let’s see how we can do this.

Including the necessary header

First, we need to include the <chrono> header in our code:

#include <chrono>

Getting the current time point

To obtain the current time point, we’ll use the std::chrono::system_clock::now() function. This function returns the current time point based on the system clock. Here’s an example:

auto currentTime = std::chrono::system_clock::now();

The currentTime variable will now hold the current time point.

Converting the time point to a specific format

If you want to obtain the current date and time in a specific format, you can use the std::chrono::system_clock::to_time_t() function along with std::localtime() or std::gmtime() to convert the time point to a std::tm structure.

Here’s an example that converts the time point to a std::tm structure and extracts the current date and time components:

std::time_t currentTimeT = std::chrono::system_clock::to_time_t(currentTime);
std::tm* currentTimeStruct = std::localtime(&currentTimeT);

int year = currentTimeStruct->tm_year + 1900;
int month = currentTimeStruct->tm_mon + 1;
int day = currentTimeStruct->tm_mday;
int hour = currentTimeStruct->tm_hour;
int minute = currentTimeStruct->tm_min;
int second = currentTimeStruct->tm_sec;

std::cout << "Current date and time: " << year << "-" << month << "-" << day << " " << hour << ":" << minute << ":" << second << std::endl;

The code above uses std::localtime() to convert the time point to a tm structure and then extracts the individual components using the appropriate members of the structure.

Conclusion

With std::chrono, obtaining the current date and time in C++ is straightforward. By using std::chrono::system_clock::now() to get the time point and then converting it to the desired format, you can easily access the current date and time information.

For more information, you can refer to the C++ reference for std::chrono and its related functions and classes.

#cplusplus #datetime