type
TForm11 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FAppStartTick: Cardinal; // or even better GetTickCount64
FAppStartTime: TDateTime;
function TimeChangeCheck(Silent: Boolean = False): Boolean;
procedure TimeChangeReset;
public
{ Public declarations }
end;
var
Form11: TForm11;
implementation
uses
System.DateUtils;
{$R *.dfm}
procedure TForm11.FormCreate(Sender: TObject);
begin
TimeChangeReset;
end;
procedure TForm11.Timer1Timer(Sender: TObject);
begin
TimeChangeCheck;
end;
function TForm11.TimeChangeCheck(Silent: Boolean = False): Boolean;
const
ALLOWED_DATETIME_DEVIATION_SEC = 2;
var
LDeltaTicks: Cardinal; // in case GettickCount instead of GetTickCount64
LDeltaSeconds: Integer;
LExpectedTime: TDateTime;
begin
LDeltaTicks := GetTickCount - FAppStartTick;
LExpectedTime := IncMilliSecond(FAppStartTime, LDeltaTicks);
LDeltaSeconds := SecondsBetween(LExpectedTime, Now);
Result := LDeltaSeconds > ALLOWED_DATETIME_DEVIATION_SEC;
if Result and not Silent then
raise
Exception.Create('System Time has been changed form the expected time by ' + IntToStr(LDeltaSeconds) + ' seconds');
end;
procedure TForm11.TimeChangeReset;
begin
FAppStartTick := GetTickCount; // or even better GetTickCount64
FAppStartTime := Now;
end;