Firstly, std::time_t really captures both the date and time, since it usually represents seconds since January 1, 1970.
There is not much support for handling dates in C ++ 11. You still have to depend on the promotion if you do not want to do this, mainly manually. Here's how to do it manually.
You can use it in thread safe mode and together with any std::chrono::*clock , for example std::system_clock , for example:
std::string get_date_string(std::chrono::time_point t) { auto as_time_t = std::chrono::system_clock::to_time_t(t); struct tm tm; if (::gmtime_r(&as_time_t, &tm)) if (std::strftime(some_buffer, sizeof(some_buffer), "%F", &tm)) return std::string{some_buffer}; throw std::runtime_error("Failed to get current date as string"); }
In another place you can specify:
get_date_string(std::system_clock::now());
Relatively good, this solution is that at the API level you still use modern portable C ++ concepts such as std::chrono::time_point and, of course, std::string .
source share