[C++] 纯文本查看 复制代码 #include <iostream>
#include <windows.h>
int main()
{
// 获取上一次关机时间
SYSTEMTIME stLastBootTime;
GetSystemTime(&stLastBootTime);
FILETIME ftLastBootTime;
SystemTimeToFileTime(&stLastBootTime, &ftLastBootTime);
// 获取当前时间
SYSTEMTIME stCurrentTime;
GetLocalTime(&stCurrentTime);
FILETIME ftCurrentTime;
SystemTimeToFileTime(&stCurrentTime, &ftCurrentTime);
// 计算时间差
ULARGE_INTEGER liLastBootTime = { ftLastBootTime.dwLowDateTime, ftLastBootTime.dwHighDateTime };
ULARGE_INTEGER liCurrentTime = { ftCurrentTime.dwLowDateTime, ftCurrentTime.dwHighDateTime };
ULONGLONG diff = liCurrentTime.QuadPart - liLastBootTime.QuadPart;
diff /= 10000000; // 转换为秒
// 格式化输出时间差
int hours = diff / 3600;
int minutes = (diff % 3600) / 60;
int seconds = diff % 60;
printf("%d时%d分%d秒", hours, minutes, seconds);
return 0;
} |