如何解决在C ++中以UTC格式获取日期和时间
如何在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日期和时间为:2020年10月12日星期一:18:56:59”格式打印。
解决方法
使用此free,open-source,header-only preview of C++20 <chrono>
:
WHERE value IN (4,5,6,<...night time hours...>)
这可以通过以下方式轻松移植到C ++ 20:
- 拖放
#include "date/date.h" #include <chrono> #include <iostream> int main() { namespace cr = std::chrono; std::cout << date::format("%FT%TZ",cr::floor<cr::seconds>(cr::system_clock::now())) << '\n'; }
- 将
#include "date/date.h"
更改为date::format
- 将
std::format
更改为"%FT%TZ"
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。