C++ 旧时间到现在的时间(以小时为单位)

c++ old time to now time in hours

本文关键字:时间 小时 为单位 C++      更新时间:2023-10-16

嗨,我如何获得创建文件的时间与现在之间的差异并将其转换为小时。

for (unsigned i = 0; i < strpokenter code herele.size(); i++)
{
const char * conv_my_str = strpole[i].c_str();
stat(conv_my_str, & stbuf);
oldate = ctime( & stbuf.st_atime);
cout << oldate;
time( & rawtime);
nowdate = ctime( & rawtime);
cout << nowdate;
getchar();
}

我通过自动和计时来解决它,但是如果您想要小时,则需要分钟示例,因为我首先说过,您可以轻松地在代码中将分钟更改为小时。

auto now = std::chrono::duration_cast<std::chrono::minutes>(actTime - std::chrono::system_clock::from_time_t(stbuf.st_ctime)).count();

看看GetFileTime这两个链接可能会在这里和这里有所帮助

如果我猜对了,问题是关于以小时为单位表示增量时间......因此,您在读取文件的创建时间或实际时间时不会遇到麻烦...... 所以你可以去:

#include <iostream>
#include <string>
std::string getDuration(const long ms) {
int seconds = (int)(ms / 1000) % 60;
int minutes = (int)((ms / (1000 * 60)) % 60);
int hours = (int)((ms / (1000 * 60 * 60)) % 24);
return "H:" + std::to_string(hours) +",M:"+ std::to_string(minutes)+",S:"+ std::to_string(seconds);
}
int main() {
long ms = 60 * 1000;
std::cout << "Duration: " << getDuration(ms) << std::endl;
return 0;
}