Author: Tomas Rutkauskas
I want to assign a new time to the system time. How would I do this? I tried to get
the system time through GetSystemTime() and set the new time to SetSystemTime(),
but I get the wrong results.
Answer:
Solve 1:
This sets the time to the same time in year 1999:
1 2 procedure TForm1.Set1999(Sender: TObject);
3 var4 stSystemTime: TSystemTime;
5 begin6 Windows.GetLocalTime(stSystemTime);
7 stSystemTime.wYear := 1999;
8 Windows.SetLocalTime(stSystemTime);
9 end;
Solve 2:
By using the following call (example is a method of a button click) you may
directly modify the date and time of Windows.
10 11 procedure TForm1.Button1Click(Sender: TObject);
12 var13 NewTime: TSystemTime;
14 begin15 FillChar(NewTime, sizeof(NewTime), #0);
16 NewTime.wYear := 2000;
17 NewTime.wMonth := 8;
18 NewTime.wDay := 28;
19 NewTime.wHour := 0;
20 NewTime.wMinute := 0;
21 NewTime.wSecond := 0;
22 NewTime.wMilliseconds := 0;
23 24 SetLocalTime(NewTime);
25 end;
Please note that when you do chage the system time, send a WM_TIMECHANGE message to
all toplevel windows so that they can detect the time change... You should do this
for all version of windows but 2000 since it does it by its self. Bellow is the
code:
26 27 if IsWin2000 = False then28 SendMessage(HWND_TOPMOST, WM_TIMECHANGE, 0, 0);
29 30 function IsWin2000(): Boolean;
31 var32 OSInfo: TOSVersionInfo;
33 begin34 Result := False;
35 OSInfo.dwOSVersionInfoSize := SizeOf(OsInfo);
36 ifnot GetVersionEx(OSInfo) then37 begin38 Exit;
39 end;
40 if OsInfo.dwPlatformID = VER_PLATFORM_WIN32_NT then41 begin42 if OSInfo.dwMajorVersion >= 5 then43 Result := True;
44 end;
45 end;