How can we get the system boot time?
If we run the cmd command, execute systeminfo to view some system information, such as the system installation time, system startup time, and system type, so how can we get the system startup time in the program?
In delphi, the Now and GetTickCount methods are provided. The two methods can be used together to know the system boot time. The specific reasons are as follows:
Like other languages, Now obtains a number that differs from the specified date. This number is of the double type. You can use the FormatDataTime function of delphi to specify a display format, display the current time.
For example, the method for displaying the time as 19:57:20 is sTime: = FormatDateTime ('yyyy-mm-dd hh: mm: ss', Now );
GetTickCount is a function used to calculate the boot time after the system is started, that is, how long it has elapsed since the system was started. It is a windows running time in milliseconds.
Therefore, when calculating the system boot time, you can use the time difference between Now and GetTickCount. The specific implementation is as follows:
Procedure TForm1.Button1Click (Sender: TObject );
Var
T1: Int64;
T2, T3: Comp;
T4: TDateTime;
S: string;
Begin
T1: = GetTickCount; {Number of milliseconds from startup to present}
T2: = TimeStampToMSecs (DateTimeToTimeStamp (Now); {Number of milliseconds from 0001-1-1 to the current time}
T3: = T2-T1; {Number of milliseconds from 0001-1-1 to the start time}
T4: = TimeStampToDateTime (MSecsToTimeStamp (T3); {time from 0001-1-1 to start time}
S: = FormatDateTime ('yyyy-mm-dd hh: mm: ss', T4); or s: = DateTimeToStr (T4 );
ShowMessage (s); {display start time}
End;
But the GetTickCount time may be inaccurate, see: http://delphi.ktop.com.tw/board.php? Cid = 30 & fid = 72 & tid = 91441
The possible cause is:
In Windows, the accumulated GetTickCount () value and system time are obtained by running a fixed Timer.
In the Mobile system, IDLE may reduce the frequency due to power saving. The GetTickCount value is slower than normal.
The system compares the Time difference between the current system Time and the BIOS Real Time Clock at a fixed period. If the difference is too large,
Automatic Correction
Reference:
Http://msdn2.microsoft.com/en-us/library/ms724408.aspx
Http://msdn2.microsoft.com/en-us/library/ms724394.aspx