In modern C++, the <chrono> library provides a powerful and flexible way to work with dates and time points. It allows you to easily convert between different date formats using its various duration and time point classes.
Formatting a date as a string
To convert a std::chrono::system_clock time point to a string representation, you can use the std::put_time function along with a std::tm object. The std::tm object holds the individual components of the date and time.
Here’s an example of converting a std::chrono::system_clock time point to a string formatted as “YYYY-MM-DD”:
#include <iostream>
#include <chrono>
#include <iomanip>
int main() {
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t time = std::chrono::system_clock::to_time_t(now);
std::tm* tm = std::localtime(&time);
std::cout << std::put_time(tm, "%Y-%m-%d") << std::endl;
return 0;
}
In this example, we first obtain the current system time as a std::chrono::system_clock::time_point using std::chrono::system_clock::now(). Then, we convert it to a std::time_t value using std::chrono::system_clock::to_time_t. Finally, we convert the std::time_t value to a std::tm object using std::localtime.
The std::put_time function, which is part of the <iomanip> header, allows us to format the std::tm object as a string. In this case, we use the format string “%Y-%m-%d” to represent the date as “YYYY-MM-DD”.
Parsing a string as a date
To convert a string representation of a date to a std::chrono::system_clock::time_point, you can use std::get_time function along with a std::tm object. The std::get_time function parses the string according to a specified format and fills the std::tm object with the parsed values.
Here’s an example of parsing a string formatted as “YYYY-MM-DD” into a std::chrono::system_clock::time_point:
#include <iostream>
#include <chrono>
#include <iomanip>
#include <sstream>
int main() {
std::string dateString = "2022-01-01";
std::tm tm = {};
std::stringstream ss(dateString);
ss >> std::get_time(&tm, "%Y-%m-%d");
auto timePoint = std::chrono::system_clock::from_time_t(std::mktime(&tm));
std::cout << timePoint.time_since_epoch().count() << std::endl;
return 0;
}
In this example, we first define a string dateString containing the date in the format “YYYY-MM-DD”. Then, we initialize a std::tm object with zeros. Next, we create a std::stringstream and initialize it with the dateString. We then use std::get_time to parse the string and store the result in the std::tm object.
Finally, we convert the std::tm object to a std::chrono::system_clock::time_point using std::chrono::system_clock::from_time_t and std::mktime. The from_time_t function converts a std::time_t value to a std::chrono::system_clock::time_point, while std::mktime converts a std::tm object to a std::time_t value.
Conclusion
With the <chrono> library in C++, converting between different date formats is made easy. Whether you need to format a date as a string or parse a string as a date, the std::chrono facilities provide a convenient way to perform these operations. By leveraging the powerful features of <chrono>, you can handle date manipulations efficiently in your C++ programs.
#cplusplus #datetime