首页 > 代码库 > 从服务器上获得网络时间的几种方法

从服务器上获得网络时间的几种方法

从服务器上获得网络时间的几种方法

1. 用Net Time \\server
2. NT平台用Win32 API函数:NetRemoteTOD
3. 用文件的创建时间
在服务器的共享文件夹下创建新文件,取新文件的创建时间。
4. 用HTTP协议头,有服务器的时间
5. 使用Telnet,取返回值
Telnet Server 13
6. 使用数据库
a) Oracle: select sysdate from dual
b) Informix: select current() from systables
c) Sql Server: select getdate()
7. 使用DCOM Server
创建一个取得服务器时间的DCOM Server,在客户端调用

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

自动与Internet时间服务器同步

  Windows默认的二个Internet时间服务器:time.windows.com和time.nist.gov大家肯定经常用,反正在我这里是经常时间同步失败,大概因为服务器是国外的,同步的机器又多,服务器太忙了吧。
  
  下面这个IP是中国国家授时中心的Internet时间服务器地址,用它来代替time.windows.com和time.nist.gov服务器,同步时间快。同步成功的机会大大增加。

210.72.145.44
大家不妨试试。

中国国家授时中心:
http://www.ntsc.ac.cn/

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

取指定IP或服务器时间
//方法一:
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 ‘NetRemoteTOD‘;
function ServerTime(const UNCServer: string; var Stamp: TDateTime): Integer;
var
ServerName: PWideChar;
tod: 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, @tod);
if Result = 0 then
begin
try
Year := tod^.Year;
Month := tod^.Month;
Day := tod^.Day;
Hour := tod^.Hours;
Min := tod^.Minutes;
Sec := tod^.Seconds;
MSec := tod^.Hunds * 10;
if tod^.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) - (tod^.TimeZone / 1440);
finally
NetApiBufferFree(tod);
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.

type
TIME_OF_DAY_INFO = record //api 傳回的資料格式
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‘; //宣告在 netapi32.dll 下的 NetRemoteTOD api
//..
function GetRemoteTOD(Host: WideString; TODInfo: LPBYTE): Boolean;
begin
Result := (NetRemoteTOD(pWideChar(Host), TODInfo) = 0);
end;

function GetRemoteDateTime(Host: WideString): TDateTime;
var
TOD : PTIME_OF_DAY_INFO;
begin
if NetRemoteTOD(pWideChar(Host), @TOD) = 0 then
Result := EncodeDate(TOD^.tod_year, TOD^.tod_month, TOD^.tod_day) +
EncodeTime(TOD^.tod_hours, TOD^.tod_mins, TOD^.tod_secs, TOD^.tod_hunds * 10) - TOD^.tod_timezone / 60 /
24
else
Result := 0;
end;
//
procedure TForm1.Button2Click(Sender: TObject);
var
t: TDatetime;
begin
t:=GetRemoteDateTime(‘\\192.168.6.101‘); // 主機前要加 \\, 而且要先 trust 過
showmessage(formatdatetime(‘yyyy/mm/dd hh:nn:ss‘,t));
end;
方法二:
假設您的伺服器上有裝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;
假設您的伺服器上有裝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;

参考
http://hi.baidu.com/lobtao/item/843aaf0bd08b44803d42e278

从服务器上获得网络时间的几种方法