var Form1: TForm1; SyncTime1: TSyncTime; FHostList: TStringList; FSychTime: Integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject); var Ini: TIniFile; I: Integer; S: string; begin FHostList := TStringList.Create; Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Setting.ini'); try Ini.ReadSectionValues('Host', FHostList); FSychTime := Ini.ReadInteger('Default', 'SychTimeSeq', 300); for I := 0 to FHostList.Count - 1 do begin S := FHostList[I]; Delete(S, 1, Pos('=', FHostList[I])); FHostList[I] := S; end; Unit1.SyncTime1 := TSyncTime.Create(False); finally Ini.Free; end; end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean); begin TerminateThread(SyncTime1.Handle, 0); SyncTime1.Free; FHostList.Free; ExitThread(0); end;
{ TSyncTime }
procedure TSyncTime.Execute; begin while not Terminated do begin while not SyncLocalTime() do begin Inc(FHostIndex); if FHostIndex >= FHostList.Count then FHostIndex := 0; Sleep(10000); end; Sleep(FSychTime * 1000); end; end;
destructor TSyncTime.Destroy; begin Self.FIdTimeObj.Free; inherited; end;
function TSyncTime.SyncLocalTime: Boolean; begin Result := False; try if FHostList.Count = 0 then Exit; Result := True; FIdTimeObj.Host := FHostList[FHostIndex]; FIdTimeObj.SyncTime(); except Result := false; end; end;
-----form------
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, IdTime, ExtCtrls, IniFiles;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormCloseQuery(Sender: TObject; var CanClose: Boolean);
private
public
end;
TSyncTime = class(TThread)
private
FIdTimeObj :TIdTime;
FHostIndex: Integer;
function SyncLocalTime: Boolean;
constructor Create(Suspend: Boolean);
public
destructor Destroy; override;
procedure Execute; override;
end;
var
Form1: TForm1;
SyncTime1: TSyncTime;
FHostList: TStringList;
FSychTime: Integer;
implementation
{$R *.dfm}
procedure TForm1.FormCreate(Sender: TObject);
var
Ini: TIniFile;
I: Integer;
S: string;
begin
FHostList := TStringList.Create;
Ini := TIniFile.Create(ExtractFilePath(Application.ExeName) + 'Setting.ini');
try
Ini.ReadSectionValues('Host', FHostList);
FSychTime := Ini.ReadInteger('Default', 'SychTimeSeq', 300);
for I := 0 to FHostList.Count - 1 do
begin
S := FHostList[I];
Delete(S, 1, Pos('=', FHostList[I]));
FHostList[I] := S;
end;
Unit1.SyncTime1 := TSyncTime.Create(False);
finally
Ini.Free;
end;
end;
procedure TForm1.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
TerminateThread(SyncTime1.Handle, 0);
SyncTime1.Free;
FHostList.Free;
ExitThread(0);
end;
{ TSyncTime }
procedure TSyncTime.Execute;
begin
while not Terminated do
begin
while not SyncLocalTime() do
begin
Inc(FHostIndex);
if FHostIndex >= FHostList.Count then FHostIndex := 0;
Sleep(10000);
end;
Sleep(FSychTime * 1000);
end;
end;
constructor TSyncTime.Create(Suspend: Boolean);
begin
inherited;
FHostIndex := 0;
FIdTimeObj := TIdTime.Create(nil);
end;
destructor TSyncTime.Destroy;
begin
Self.FIdTimeObj.Free;
inherited;
end;
function TSyncTime.SyncLocalTime: Boolean;
begin
Result := False;
try
if FHostList.Count = 0 then Exit;
Result := True;
FIdTimeObj.Host := FHostList[FHostIndex];
FIdTimeObj.SyncTime();
except
Result := false;
end;
end;
end.
---------setting.ini-------
[Default]
SychTimeSeq=300
[Host]
1=time.windows.com
2=time.nist.gov
3=time-nw.nist.gov
4=time-a.nist.gov
5=time-b.nist.gov
------------------------
使用TIdTime,就是如此简单:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
IdTime, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
IdTime1: TIdTime;
Button1: TButton;
Button2: TButton;
Panel1: TPanel;
Label1: TLabel;
Label2: TLabel;
Button3: TButton;
Label3: TLabel;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure Button3Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
//获取时间
procedure TForm1.Button1Click(Sender: TObject);
begin
IdTime1.Host:=Edit1.Text;
Label2.Caption:='网络时间:'+DatetimeToStr(IdTime1.DateTime);
Label1.Caption:='系统时间:'+DatetimeToStr(now);
end;
//校对时间
procedure TForm1.Button2Click(Sender: TObject);
begin
IdTime1.SyncTime;
end;
procedure TForm1.Button3Click(Sender: TObject);
begin
Close;
end;
end.
'210.0.235.14', // stdtime.gov.hk(香港)
'207.46.130.100', // time.windows.com
'203.117.180.36', // UTC(新加坡)
'131.107.1.10', // UTC(NIST)
'64.236.96.53'); // time.asia.apple.com
已经发送了。
香港也是用北京时间的。
关于北京方面有什么服务器以及IP地址我也不知道,网上搜索了几个也是不能用的。
关于超时,有可能是你的网速问题,也有可能跟防火墙和网关有关的,这很难帮你解决,
试一下尽量设置长一点的超时时间。
如果这样还会超时,那很大可能是你网络的问题了。