Precautions for getting the system boot time and gettickcount

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.