Windows UpTime
A C console program that reports system uptime in days, hours, minutes, and seconds from GetTickCount.
The shortest possible uptime reader. GetTickCount returns the milliseconds since boot; divide by 1000 and peel off days, hours, and minutes with three little loops (the original does it by repeated subtraction rather than division - a charmingly literal way to parse time). The result prints as "X Days, Y Hours, Z Minutes, W Seconds". Handy embedded in any diagnostic tool that wants a one-line "how long has this box been up?" answer. Note the tick count wraps at roughly 49.7 days on 32-bit systems, so very long uptimes will read low.
#include <Windows.H>
#include <Stdio.H>
long int Mins = 0; //Minutes
long int Secs = 0; //Seconds
long int Hous = 0; //Hours
long int Days = 0; //Days
int main()
{
int Temp_Time = GetTickCount()/1000;
//--------------------------------------------------------------------
while(Temp_Time >= 60) // Parse Minutes
{
if(Temp_Time >= 60)
{
Mins++;
Temp_Time = Temp_Time-60;
}
}
//--------------------------------------------------------------------
while(Mins >= 60) // Parse Hours
{
if(Mins >= 60)
{
Hous++;
Mins = Mins-60;
}
}
//--------------------------------------------------------------------
while(Hous >= 24) // Parse Days
{
if(Hous >= 24)
{
Days++;
Hous = Hous-24;
}
}
//--------------------------------------------------------------------
Secs = Temp_Time; //Seconds Equal Leftovers
printf("%d Days, %d Hours, %d Minutes, %d Seconds",Days,Hous,Mins,Secs);
return 0;
}
The same logic appears in the DLL Skeleton article's GetUpTime export - this is the standalone console version of it. If you want modern robustness, use GetTickCount64 (Vista+) or compute from GetSystemTime minus the kernel start time from the performance counters.