Several methods for obtaining network time from the server

Source: Internet
Author: User
Tags net time

Several methods for obtaining network time from the server

1. Use Net time \ Server
2. Win32 API function used by the NT platform: netremotetod
3. File Creation Time
Create a new file in the shared folder of the server and obtain the time when the new file was created.
4. Use the HTTP header to have the server time
5. Use telnet to obtain the returned value.
Telnet Server 13
6. Use the database
A) Oracle: Select sysdate from dual
B) Informix: Select current () from tables Ables
C) SQL SERVER: Select getdate ()
7. Use DCOM Server
Create a DCOM server that obtains the server time and call

---------------------------------------------------------------

Automatic synchronization with Internet Time Server

By default, Windows has two Internet time servers: time.windows.com and time.nist.gov, which are usually used by everyone. However, I often encounter time synchronization failures because the servers are foreign and there are many Synchronous Machines, the server is too busy.
  
The following IP address is the Internet time server address of China's National Time Service Center. It is used to replace time.windows.com and time.nist.gov servers, and the synchronization time is fast. The chance of successful synchronization is greatly increased.

210.72.145.44
Try it.

China National Time Service Center:
Http://www.ntsc.ac.cn/

---------------------------------------------------------------

Obtains the specified IP address or server time.
// Method 1:
Unit unit1;
Interface
Uses
Windows, messages, sysutils, variants, classes, graphics, controls, forms,
Dialogs, stdctrls;
Type
Ttodinfo = record
Elapsedtime: integer; {number of seconds since 00:00:00 January 1, 1970}
Milliseconds: integer; {Number of milliseconds since last system reset}
Hours: integer; {current hour (0-23 )}
Minutes: integer; {current minute (0-59 )}
Seconds: integer; {current second (0-59 )}
Hunds: integer; {current hundredth of a second (0-99 )}
Timezone: integer; {time against GMT in minutes}
{West of Greenwich gives positive, East negative values}
{Value of-1 means undefined time zone}
Interval: integer; {clock tick interval in ten-thousandth of a second (0.0001 S )}
Day: integer; {day of the month (1-31 )}
Month: integer; {month of the year (1-12 )}
Year: integer; {year}
Weekday: integer; {day of the week (0-6) 0 = Sunday, 1 = Monday etc .}
End;
Ptodinfo = ^ ttodinfo;
Type
Tform1 = Class (tform)
Label1: tlabel;
Button1: tbutton;
Procedure button1click (Sender: tobject );
Private
{Private Declarations}
Public
{Public declarations}
End;
VaR
Form1: tform1;
Const
Netapi32 = 'netapi32. dll ';
Function netapibufferfree (buffer: pointer): integer; stdcall;
Function netremotetod (uncservername: pwidechar; Info: pointer): integer; stdcall;
Function servertime (const uncserver: string; var Stamp: tdatetime): integer;
Implementation
{$ R *. DFM}
Function netapibufferfree; External netapi32 name 'netapibufferfree ';
Function netremotetod; External netapi32 name 'netremoteod ';
Function servertime (const uncserver: string; var Stamp: tdatetime): integer;
VaR
Servername: pwidechar;
TODD: ptodinfo;
Year, month, day, hour, Min, SEC, msec: word;
Begin
Getmem (servername, (length (uncserver) + 1) * sizeof (widechar ));
Try
Servername: = stringtowidechar (uncserver, servername, length (uncserver) + 1 );
Result: = netremotetod (servername, @ Todd );
If result = 0 then
Begin
Try
Year: = Todd ^. Year;
Month: = Todd ^. month;
Day: = Todd ^. Day;
Hour: = Todd ^. hours;
Min: = Todd ^. minutes;
SEC: = Todd ^. seconds;
MSEC: = maid * 10;
If Todd ^. timezone =-1 then {undefined timezone}
Stamp: = encodedate (year, month, day) +
Encodetime (hour, Min, SEC, msec)
Else
Stamp: = encodedate (year, month, day) +
Encodetime (hour, Min, SEC, msec)-(Todd ^. timezone/1440 );
Finally
Netapibufferfree (Todd );
End;
End;
Finally
Freemem (servername );
End;
End;
Procedure tform1.button1click (Sender: tobject );
VaR
T: tdatetime;
Begin
Servertime ('192. 168.192.187 ', t );
Label1.caption: = datetimetostr (t );
End;
End.
Or
Type
Time_of_day_info = record // format of the API metadata returned
Tod_elapsedt: DWORD;
Tod_msecs: DWORD;
Tod_hours: DWORD;
Tod_mins: DWORD;
Tod_secs: DWORD;
Tod_hunds: DWORD;
Tod_timezone: longint;
Tod_tinterval: DWORD;
Tod_day: DWORD;
Tod_month: DWORD;
Tod_year: DWORD;
Tod_weekday: DWORD;
End;
Ptime_of_day_info = ^ time_of_day_info;

Lpbyte = ^ byte;
Net_api_status = DWORD;
//..
Function netremotetod (hostname: pwidechar; Buffer: lpbyte): net_api_status;
Stdcall; External 'netapi32. dll 'name' netremotetod '; // declare the netremotetod API under netapi32.dll
//..
Function getremotetod (HOST: widestring; todinfo: lpbyte): Boolean;
Begin
Result: = (netremotetod (pwidechar (host), todinfo) = 0 );
End;

Function getremotedatetime (HOST: widestring): tdatetime;
VaR
TODD: ptime_of_day_info;
Begin
If netremotetod (pwidechar (host), @ Todd) = 0 then
Result: = encodedate (FIG, FIG) +
Encodetime (FIG, fig, fig * 10)-Fig ^. tod_timezone/60/
24
Else
Result: = 0;
End;
//
Procedure tform1.button2click (Sender: tobject );
VaR
T: tdatetime;
Begin
T: = getremotedatetime ('\ 192.168.6.101'); // Add \ before the host and trust it first
Showmessage (formatdatetime ('yyyy/MM/dd hh: NN: ss', t ));
End;
Method 2:
If your server has a temporary ms SQL:

Function getserverdate: tdatetime;
VaR
Aqry: tquery;
Begin
Aqry: = tquery. Create (NiL );
Try
Aqry. databasename: = 'db ';
Aqry. close;
Aqry. SQL. Clear;
Aqry. SQL. Add ('select getdate () dbdate ');
Aqry. open;
Result: = aqry. fieldbyname ('dbdate'). asdatetime;
Finally
Freeandnil (aqry );
End;
End;
If your server has another oracle:

Function getserverdate: tdatetime;
VaR
Aqry: tquery;
Begin
Aqry: = tquery. Create (NiL );
Try
Aqry. databasename: = 'db ';
Aqry. close;
Aqry. SQL. Clear;
Aqry. SQL. Add ('select sysdate dbdate from dual ');
Aqry. open;
Result: = aqry. fieldbyname ('dbdate'). asdatetime;
Finally
Freeandnil (aqry );
End;
End;

Reference
Http://hi.baidu.com/lobtao/item/843aaf0bd08b44803d42e278

Several methods for obtaining network time from the server

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.