Get standard time from Internet Time Server

Source: Internet
Author: User

Because the local system is not accurate, it is decided to obtain the system from the Internet to correct the system.

ProgramDesign Procedure

1. Create a project;

2. Place a tedit control and a tclientsocket (on the Internet page) control on the form. Tedit is used to display the Greenwich Mean obtained after being converted to Beijing, and tclientsocket is used to obtain Greenwich mean from the Internet;

3. ProgramCode

Procedure tform1.formcreate (Sender: tobject );

Begin

Clientsocket1.host: = 'tick .usno.navy.mil '; // US Naval Observatory;

Clientsocket1.port: = 13;

Clientsocket1.open;

End;

Procedure tform1.clientsocket1read (Sender: tobject;

Socket: tcustomwinsocket );

VaR

Temptime: tdatetime;

HH, mm, SS, Ms: word;

Stime: string;

Begin

Stime: = socket. receivetext; // obtain Greenwich Mean;

Stime: = copy (stime,); // obtain the part;

Decodetime (strtotime (stime), HH, mm, SS, MS );

Hh: = (HH + 8) mod 24; // convert it to Beijing;

Temptime: = encodetime (HH, mm, SS, MS );

Edit1.text: = timetostr (temptime );

End;

4. Compile and run the program. wait a few seconds (depending on your network speed, wait for about one second). The tedit control displays the Beijing data converted from Greenwich.

How is it? It's easy. Give it a try! Note: Due to network latency

A program may get a few seconds of error, but unless you control the rocket launch, no

Then, this does not matter.

It is obtained from the US Naval Observatory. You can also obtain it from other sources. However, because each format is different, you should handle it when writing a program.

Domain Name Organization

Clock. SGI. comsgi

Tick.mit.edu MIT

Time. NIST. govnist

Related Article

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.