uses IdTime;procedure SyncTime; begin with TIDTime.Create(nil) do try try Host := 校时服务器; if SyncTime then 成功 except 失败 end; finally Free; end; end;function SetLocalTime(Value: TDateTime): boolean; {I admit that this routine is a little more complicated than the one in Indy 8.0. However, this routine does support Windows NT privillages meaning it will work if you have administrative rights under that OSOriginal author Kerry G. Neighbour with modifications and testing from J. Peter Mugaas} var dSysTime: TSystemTime; buffer: DWord; tkp, tpko: TTokenPrivileges; hToken: THandle; begin Result := False; if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then begin if not Windows.OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken) then begin exit; end; Windows.LookupPrivilegeValue(nil, 'SE_SYSTEMTIME_NAME', tkp.Privileges[0].Luid); {Do not Localize} tkp.PrivilegeCount := 1; tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED; if not Windows.AdjustTokenPrivileges(hToken, FALSE, tkp, sizeof(tkp), tpko, buffer) then begin exit; end; end; DateTimeToSystemTime(Value, dSysTime); Result := Windows.SetLocalTime(dSysTime); {Undo the Process Privillage change we had done for the set time and close the handle that was allocated} if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then begin Windows.AdjustTokenPrivileges(hToken, FALSE,tpko, sizeOf(tpko), tkp, Buffer); Windows.CloseHandle(hToken); end; end;
设置系统时间:uses ShellAPI; function SetSystemDateTime(Year, Month, Day, Hour, Minute, Second: word): integer; export; procedure SetDate(Year, Month, Day: Word); assembler; asm MOV CX,Year MOV DH,BYTE PTR Month MOV DL,BYTE PTR Day MOV AH,2BH INT 21H end; procedure SetTime(Hour, Minute, Second, Sec100: Word); assembler; asm MOV CH,BYTE PTR Hour MOV CL,BYTE PTR Minute MOV DH,BYTE PTR Second MOV DL,BYTE PTR Sec100 MOV AH,2DH INT 21H end;begin SetDate(Year, Month, Day); SetTime(Hour, Minute + 1, Second, 0); result := 1; end;procedure TForm1.Button1Click(Sender: TObject); var st : TSYSTEMTIME; begin //得到系统时间 GetSystemTime(st); //显示系统时间 showmessage('系统时间 = ' + IntToStr(st.wmonth) + '/' + IntToStr(st.wDay) + '/' + IntToStr(st.wYear) + ' ' + IntToStr(st.wHour) + ':' + IntToStr(st.wMinute) + ':' + IntToStr(st.wSecond)); end; procedure TForm1.Button2Click(Sender: TObject);//设置系统时间 var st: TSYSTEMTIME; begin DateTimeToSystemTime(StrToDatetime('2002-06-23 15:39:46' ),st); SetSystemTime(st); end; 网络时间同步: unit DTDem;interfaceuses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, Psock, NMDayTim, ExtCtrls, ComCtrls, ScktComp;type TForm1 = class(TForm) Panel1: TPanel; Edit1: TEdit; Edit2: TEdit; Label1: TLabel; Label2: TLabel; NMDayTime1: TNMDayTime; Button1: TButton; Label3: TLabel; StatusBar1: TStatusBar; procedure Button1Click(Sender: TObject); procedure NMDayTime1Connect(Sender: TObject); procedure NMDayTime1Disconnect(Sender: TObject); procedure NMDayTime1HostResolved(Sender: TComponent); procedure NMDayTime1Status(Sender: TComponent; Status: String); procedure NMDayTime1ConnectionFailed(Sender: TObject); procedure NMDayTime1InvalidHost(var handled: Boolean); private { Private declarations } public { Public declarations } end;const StdCap = 'The Current Date and Time is : ';var Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject); begin NMDayTime1.ReportLevel := Status_Basic; // NMDayTime1.TimeOut := 300; NMDayTime1.Host := Edit1.Text; NMDayTime1.Port := StrToInt(Edit2.Text); Label3.Caption := StdCap+NMDayTime1.DayTimeStr; end;procedure TForm1.NMDayTime1Connect(Sender: TObject); begin StatusBar1.SimpleText := 'Connected'; end;procedure TForm1.NMDayTime1Disconnect(Sender: TObject); begin If StatusBar1 <> nil then StatusBar1.SimpleText := 'disconnected'; end;procedure TForm1.NMDayTime1HostResolved(Sender: TComponent); begin StatusBar1.SimpleText := 'Host resolved'; end;procedure TForm1.NMDayTime1Status(Sender: TComponent; Status: String); begin If StatusBar1 <> nil then StatusBar1.SimpleText := status; end;procedure TForm1.NMDayTime1ConnectionFailed(Sender: TObject); begin ShowMessage('Connection Failed'); end;procedure TForm1.NMDayTime1InvalidHost(var handled: Boolean); var TmpStr: String; begin If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then Begin NMDayTime1.Host := TmpStr; Handled := TRUE; End; end;end. //这个是利用DELPHI6自身带的一个一个控件
运行dos命令: net time \\computer /set
通过网络获得服务器的时间和日期? 若为Oracle数据库则可以加入一个 Query加入 select sysdate from dual 可以得出时间!
我用的是microsoft SQL2000 .
procedure TForm1.Button1Click(Sender: TObject); var spDate:PSystemTime; begin new(spdate) ; with spDate^ do begin wYear:=2004; wMonth:=8; wDay:=8; wHour:=4; wMinute:=00; end; setsystemtime(spDate^); Dispose(spdate); end;
IdTime;procedure SyncTime;
begin
with TIDTime.Create(nil) do
try
try
Host := 校时服务器;
if SyncTime then 成功
except
失败
end;
finally
Free;
end;
end;function SetLocalTime(Value: TDateTime): boolean;
{I admit that this routine is a little more complicated than the one
in Indy 8.0. However, this routine does support Windows NT privillages
meaning it will work if you have administrative rights under that OSOriginal author Kerry G. Neighbour with modifications and testing
from J. Peter Mugaas}
var
dSysTime: TSystemTime;
buffer: DWord;
tkp, tpko: TTokenPrivileges;
hToken: THandle;
begin
Result := False;
if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then
begin
if not Windows.OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY,
hToken) then
begin
exit;
end;
Windows.LookupPrivilegeValue(nil, 'SE_SYSTEMTIME_NAME', tkp.Privileges[0].Luid); {Do not Localize}
tkp.PrivilegeCount := 1;
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
if not Windows.AdjustTokenPrivileges(hToken, FALSE, tkp, sizeof(tkp), tpko, buffer) then
begin
exit;
end;
end;
DateTimeToSystemTime(Value, dSysTime);
Result := Windows.SetLocalTime(dSysTime);
{Undo the Process Privillage change we had done for the set time
and close the handle that was allocated}
if SysUtils.Win32Platform = VER_PLATFORM_WIN32_NT then
begin
Windows.AdjustTokenPrivileges(hToken, FALSE,tpko, sizeOf(tpko), tkp, Buffer);
Windows.CloseHandle(hToken);
end;
end;
function SetSystemDateTime(Year, Month, Day, Hour, Minute, Second: word): integer; export; procedure SetDate(Year, Month, Day: Word); assembler;
asm
MOV CX,Year
MOV DH,BYTE PTR Month
MOV DL,BYTE PTR Day
MOV AH,2BH
INT 21H
end; procedure SetTime(Hour, Minute, Second, Sec100: Word); assembler;
asm
MOV CH,BYTE PTR Hour
MOV CL,BYTE PTR Minute
MOV DH,BYTE PTR Second
MOV DL,BYTE PTR Sec100
MOV AH,2DH
INT 21H
end;begin
SetDate(Year, Month, Day);
SetTime(Hour, Minute + 1, Second, 0);
result := 1;
end;procedure TForm1.Button1Click(Sender: TObject);
var
st : TSYSTEMTIME;
begin
//得到系统时间
GetSystemTime(st);
//显示系统时间
showmessage('系统时间 = ' +
IntToStr(st.wmonth) + '/' +
IntToStr(st.wDay) + '/' +
IntToStr(st.wYear) + ' ' +
IntToStr(st.wHour) + ':' +
IntToStr(st.wMinute) + ':' +
IntToStr(st.wSecond));
end;
procedure TForm1.Button2Click(Sender: TObject);//设置系统时间
var
st: TSYSTEMTIME;
begin
DateTimeToSystemTime(StrToDatetime('2002-06-23 15:39:46' ),st);
SetSystemTime(st);
end;
网络时间同步:
unit DTDem;interfaceuses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls, Psock, NMDayTim, ExtCtrls, ComCtrls, ScktComp;type
TForm1 = class(TForm)
Panel1: TPanel;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
NMDayTime1: TNMDayTime;
Button1: TButton;
Label3: TLabel;
StatusBar1: TStatusBar;
procedure Button1Click(Sender: TObject);
procedure NMDayTime1Connect(Sender: TObject);
procedure NMDayTime1Disconnect(Sender: TObject);
procedure NMDayTime1HostResolved(Sender: TComponent);
procedure NMDayTime1Status(Sender: TComponent; Status: String);
procedure NMDayTime1ConnectionFailed(Sender: TObject);
procedure NMDayTime1InvalidHost(var handled: Boolean);
private
{ Private declarations }
public
{ Public declarations }
end;const
StdCap = 'The Current Date and Time is : ';var
Form1: TForm1;implementation{$R *.DFM}procedure TForm1.Button1Click(Sender: TObject);
begin
NMDayTime1.ReportLevel := Status_Basic;
// NMDayTime1.TimeOut := 300;
NMDayTime1.Host := Edit1.Text;
NMDayTime1.Port := StrToInt(Edit2.Text);
Label3.Caption := StdCap+NMDayTime1.DayTimeStr;
end;procedure TForm1.NMDayTime1Connect(Sender: TObject);
begin
StatusBar1.SimpleText := 'Connected';
end;procedure TForm1.NMDayTime1Disconnect(Sender: TObject);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := 'disconnected';
end;procedure TForm1.NMDayTime1HostResolved(Sender: TComponent);
begin
StatusBar1.SimpleText := 'Host resolved';
end;procedure TForm1.NMDayTime1Status(Sender: TComponent; Status: String);
begin
If StatusBar1 <> nil then
StatusBar1.SimpleText := status;
end;procedure TForm1.NMDayTime1ConnectionFailed(Sender: TObject);
begin
ShowMessage('Connection Failed');
end;procedure TForm1.NMDayTime1InvalidHost(var handled: Boolean);
var
TmpStr: String;
begin
If InputQuery('Invalid Host!', 'Specify a new host:', TmpStr) then
Begin
NMDayTime1.Host := TmpStr;
Handled := TRUE;
End;
end;end. //这个是利用DELPHI6自身带的一个一个控件
net time \\computer /set
若为Oracle数据库则可以加入一个 Query加入 select sysdate from dual 可以得出时间!
var
spDate:PSystemTime;
begin
new(spdate) ;
with spDate^ do
begin
wYear:=2004;
wMonth:=8;
wDay:=8;
wHour:=4;
wMinute:=00;
end;
setsystemtime(spDate^);
Dispose(spdate);
end;
select getdate()
SetSystemTime举例如下:
1、定义变量
var SystemTime: TSystemTime;
2、转换日期
DateTimeToSystemTime(StrToDatetime('1999-09-01 11:12:12' ),SystemTime);
3、改变系统日期
SetSystemTime(SystemTime);
到此系统日期已经改变,可是由于API函数SetSystemTime()本身存在的BUG,在你改变系统日期以后,等待
一会,你会看到系统的日期是对的,可是时间却错了
ServerTime: TDateTime;
Tmp: SYSTEMTIME;
……ServerTime := TBSJ(SQLDBStr);//在这儿请通过SQL来写TBSJ函数以得到SQL主机时间。DateTimeToSystemTime(ServerTime,Tmp);
if(!SetLocalTime(&Tmp))then ShowMessage("系统时间设置错误!");
select getdate()
就可以得到了。
此外,DOS命令:“net time \\computer /set /yes”可以完成你的工作,效果不错的,所以,如果不是非常必要,你也可以用DOS命令来实现此工作。此DOS命令要求客机能正常登录主机的共享目录($ICP或其它)。
........
Sdatatime: TADOQuery;
.....
...procedure TfrmMain.SetNewTime;
var
spDate:PSystemTime;
Year,Month,Day,hour,minutes,Second:WORD;
begin
Sdatatime.Close;
Sdatatime.sql.SQL.Clear;
Sdatatime.sql.add('select getdate()');
Sdatatime.Open;
Year:=strtoint(FormatDateTime('YYYY',Sdatatime.FieldByName('COLUMN1').AsDateTime));
Month:=StrToint(FormatDateTime('MM',Sdatatime.FieldByName('COLUMN1').AsDateTime));
Day:=StrToint(FormatDateTime('DD',Sdatatime.FieldByName('COLUMN1').AsDateTime));
hour:=StrToint(FormatDateTime('HH',Sdatatime.FieldByName('COLUMN1').AsDateTime));
minutes:=StrToint(copy(FormatDateTime('hh:mm:ss',Sdatatime.FieldByName('COLUMN1').AsDateTime),4,2));
Second:=StrToint(FormatDateTime('ss',Sdatatime.FieldByName('COLUMN1').AsDateTime));
Sdatatime.Close;
new(spdate) ;
if (hour<8)or(hour=24) then Day:=Day-1;
case hour of
0: hour :=16;
1: hour :=17;
2: hour :=18;
3: hour :=19;
4: hour :=20;
5: hour :=21;
6: hour :=22;
7: hour :=23;
8: hour :=0;
9: hour :=1;
10: hour :=2;
11: hour :=3;
12: hour :=4;
13: hour :=5;
14: hour :=6;
15: hour :=7;
16: hour :=8;
17: hour :=9;
18: hour :=10;
19: hour :=11;
20: hour :=12;
21: hour :=13;
22: hour :=14;
23: hour :=15;
24: hour :=16;
end;
spDate.wYear:=Year;
spDate.wMonth:=Month;
spDate.wDayOfWeek:=0;
spDate.wDay:=Day;
spDate.wHour:=hour;
spDate.wMinute:=minutes;
spDate.wSecond:=Second;
spDate.wMilliseconds:=0;
setsystemtime(spDate^);
Dispose(spdate);
end;