电脑开机时间的计算代码

时间:2021-09-17 06:57:23

函数功能:GetTickCount返回(retrieve)从操作系统启动到现在所经过(elapsed)的毫秒数,它的返回值是DWORD.

知道了这个,这个程序也就不是什么难事了。。。

CODE:

 

复制代码 代码如下:

#include <stdlib.h>
 #include <time.h>
 #include <windows.h>
 #include <stdio.h>

 typedef struct node
 {
     int h;
     int m;
     int s;
 }
 *PTime;

 void sleep(long wait);

 void gettime();

 int main()
 {
     PTime times;
     int flag = 1;
     char time[128];
     do
     {
         _strtime(time); // Gets the current system time (do not include the date)
         system("cls"); // clear screen
         printf("OS time: %s\n",time);

         gettime(times); // call gettime()
         sleep(1000); // sleep 1 second

         printf("已开机时间: %02d小时%02d分%02d秒\n", times->h, times->m, times->s);
     }while(flag); // always cycle

     return 0;
 }

 void sleep(long wait)
 {
     long goal; // define total time
     goal = wait + clock();
     while(goal > clock());
 }

 PTime gettime(PTime T)
 {
     int i = GetTickCount();
     T->h = (i / 1000) / 3600;
     T->m = (i / 1000) / 60 - T->h * 60;
     T->s = (i / 1000) - T->h * 3600 - T->m * 60;
     return T;
 }