是否有支持跨平台的C++ 方法来获取当前日期和时间?
发布于 2018-02-07 04:15:24
C++标准库没有提供默认的日期类型。C++从C继承了用于日期和时间操作的结构和函数,以及一些考虑到本地化的日期/时间输入和输出函数。
// Current date/time based on current system
time_t now = time(0);
// Convert now to tm struct for local timezone
tm* localtm = localtime(&now);
cout << "The local date and time is: " << asctime(localtm) << endl;
// Convert now to tm struct for UTC
tm* gmtm = gmtime(&now);
if (gmtm != NULL) {
cout << "The UTC date and time is: " << asctime(gmtm) << endl;
}
else {
cerr << "Failed to get the UTC date and time" << endl;
return EXIT_FAILURE;
}
https://stackoverflow.com/questions/-100003460
复制相似问题