【发布时间】:2020-10-12 18:56:42
【问题描述】:
如何在 C++ 中以这种格式 ('2002-05-30T09:30:10Z') 获取 UTC 时间的日期时间? 这是我做的函数。
void settime(){
// 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;
}
}
此函数现在以这种格式打印“UTC 日期和时间是:Mon Oct 12 18:56:59 2020”。
【问题讨论】:
标签: c++ c++11 visual-c++ c++17