C++ 计算网站运行时间(年、天、小时、分钟、秒)
以下是一个用 C++ 编写的代码示例,用于计算运行时间并以年、天、小时、分钟和秒为单位进行显示,同时以秒钟为动态显示。代码中使用了 '
#include <iostream>
#include <chrono>
int main() {
// 设置网站创建时间(示例为2022年1月1日0时0分0秒)
std::chrono::time_point<std::chrono::system_clock> websiteCreationTime =
std::chrono::system_clock::from_time_t(std::mktime(std::localtime(&websiteCreationTime)));
// 获取当前时间
auto currentTime = std::chrono::system_clock::now();
// 计算运行时间(单位:秒)
auto runningTimeSeconds = std::chrono::duration_cast<std::chrono::seconds>(currentTime - websiteCreationTime).count();
// 计算运行时间(单位:年)
auto runningTimeYears = runningTimeSeconds / (365 * 24 * 60 * 60);
// 计算运行时间(单位:天)
auto runningTimeDays = (runningTimeSeconds % (365 * 24 * 60 * 60)) / (24 * 60 * 60);
// 计算运行时间(单位:小时)
auto runningTimeHours = (runningTimeSeconds % (24 * 60 * 60)) / (60 * 60);
// 计算运行时间(单位:分钟)
auto runningTimeMinutes = (runningTimeSeconds % (60 * 60)) / 60;
// 输出网站运行时间
std::cout << '网站运行时间: ' << runningTimeYears << '年 '
<< runningTimeDays << '天 '
<< runningTimeHours << '小时 '
<< runningTimeMinutes << '分钟 '
<< runningTimeSeconds << '秒钟' << std::endl;
return 0;
}
这段代码使用了 '
原文地址: https://www.cveoy.top/t/topic/n89 著作权归作者所有。请勿转载和采集!