(************************************************************************
*    This File is Part of the AHM Triton Tools                          *
*    for Borland Delphi and C++ Builder.                                *
*************************************************************************
* Copyright © 1995-1999 Alexander H. Mehlhorn (AHM) and Tritontools.com *
* All Rights reserved.                                                  *
*                                                                       *
*    For more Information contact us at                                 *
*    E-Mail : support@tritontools.com                                   *
*    URL : http://www.tritontools.com                                   *
*                                                                       *
*    AHM is copyright by Alexander H. Mehlhorn                          *
*    All Parts of this Collection were developed,                       *
*    designed and implemented by AHM                                    *
*                                                                       *
*    Under no Circumstances may you distribute this file                *
*    in this or any other format. This File is supplied                 *
*    as Evaluation of Source only. No Delphi Components may be developed*
*    using extracts or subsets of this code without expressed permission*
*    by AHM.                                                            *
*                                                                       *
*************************************************************************)
unit AHMTTimeTrigger;

interface

uses
  SysUtils, Windows, Classes,AHMSystemClass,ExtCtrls;

type
  TAHMTimeTrigger = class(TAHMSystemsComponent)
  private
     FCurrentTimer : TTimer;
     FCurrentTime : String;
     FTriggerTime : String;
     FCurrentDate : String;
     FTriggerDate : String;
     FOnTrigger,FOnNewTime,FOnNewDate : TNotifyevent;
     FOldTime,FOldDate : String;
     FCheckForTrigger : boolean;
     procedure SetCurrentTime(Value : String);
     procedure SetTriggerTime(Value : String);
     procedure SetCurrentDate(Value : String);
     procedure SetTriggerDate(Value : String);
     procedure SetCheckForTrigger(Value : boolean);
  protected
     procedure TimerCheckForTrigger(Sender : TObject);
  public
     constructor Create( AOwner: TComponent ); override;
     destructor Destroy; override;
  published
     Property CurrentTime : String read FCurrentTime write SetCurrentTime;
     Property CurrentDate : String read FCurrentDate write SetCurrentDate;
     Property TriggerDate : String read FTriggerDate write SetTriggerDate;
     Property TriggerTime : String read FTriggerTime write SetTriggerTime;
     Property CheckForTrigger : Boolean read FCheckForTrigger write SetCheckForTrigger default False;
     Property OnTrigger : TNotifyevent read FOnTrigger write FOnTrigger;
     Property OnNewTime : TNotifyevent read FOnNewTime write FOnNewTime;
     Property OnNewDate : TNotifyevent read FOnNewDate write FOnNewDate;
  end;

implementation

constructor TAHMTimeTrigger.Create( AOwner: TComponent );
begin
  inherited Create( AOwner );
  FCheckForTrigger:=False;
  FCurrentTimer:=TTimer.create(self);
  FCurrentTimer.interval:=200;
  FOldTime:='00:00';
  FOldDate:='11/7/73';
  FCurrentTimer.enabled:=true;
  FCurrentTimer.ontimer:=TimerCheckForTrigger;
  FCurrentTime:=timetostr(Time);
  FCurrentDate:=datetostr(date);
end;

destructor TAHMTimeTrigger.Destroy;
begin
  FCurrentTimer.free;
  inherited Destroy;
end;

procedure TAHMTimeTrigger.SetCurrentTime(Value : String);
begin
  FOldTime:=FCurrentTime;
  FCurrentTime:=Timetostr(Time);
end;

procedure TAHMTimeTrigger.SetTriggerTime(Value : String);
begin
  if Value<>'' then
  FTriggerTime:=Timetostr(StrtoTime(Value))
  else FTriggerTime:='';
end;


procedure TAHMTimeTrigger.SetCurrentDate(Value : String);
begin
  FOldDate :=FCurrentDate;
  FCurrentDate:=Datetostr(date);
end;

procedure TAHMTimeTrigger.SetTriggerDate(Value : String);
begin
  if Value<>'' then FTriggerDate:=Datetostr(StrtoDate(Value))
  else FTriggerDate:='';
end;

procedure TAHMTimeTrigger.SetCheckForTrigger(Value : boolean);
begin
  FCheckForTrigger:=Value;
end;


procedure TAHMTimeTrigger.TimerCheckForTrigger(Sender : TObject);
var ax,bx: boolean;
    at,ab : boolean;
begin
  ax:=true;
  bx:=true;
  ab:=false;
  at:=false;
  FCurrentTime:=Timetostr(Time);
  FCurrentDate:=Datetostr(Date);
  if FTriggerDate<>'' then ax:=(FTriggerDate=FCurrentDate);
  if FTriggerTime<>'' then bx:=(FTriggerTime=FCurrentTime);
  if (FTriggerDate<>'') and (FTriggerTime<>'') then
  if ax and bx then ab:=true;
  if (FTriggerDate='') and (FTriggerTime<>'') then
  if bx then at:=true;
  if (at or ab) and FCheckForTrigger then
  begin
    FCheckForTrigger:=false;
    if assigned(FOnTrigger) then FOnTrigger(self);
  end;
  if FOldTime<>FCurrentTime then
  if assigned(FOnNewTime) then FOnNewTime(self);
  if FOldDate<>FCurrentDate then
  if assigned(FOnNewDate) then FOnNewDate(self);
end;

end.
