Computer boot time calculation code

  • 2020-04-01 23:39:24
  • OfStack

GetTickCount returns the number of milliseconds elapsed from the operating system startup to the present time of the (retrieve), with a return value of DWORD.

Knowing this, the program is not that difficult...

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: %sn",time);

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

         printf(" Boot time : %02d hours %02d points %02d seconds 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;
 }


Related articles: