Recent

Author Topic: Comparing a time in a list with now()  (Read 4770 times)

PeterATC

  • New Member
  • *
  • Posts: 12
Comparing a time in a list with now()
« on: January 19, 2011, 01:06:50 pm »
Hi all,
i am trying to rewrite an alarm clock now I have most of it completed but i am have issues with the code to check in the first item in the list is earlier or equal to the current time. The list is sorted in time orded. with the earliest time at the top of the list. Here is the code.

procedure TfrmMain.tmrAlarmTimer(Sender: TObject);
begin
// check top time against current time and if it is less then
  begin
       if StrToTime(lstTimes.Items[0]) = Now() then
       begin
           MessageDlg('Times up',mtConfirmation,[mbok],0);
       end;
  end;
end;


Now at present all I am do is displaying a message. Eventually it will paly a alarm.


Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Comparing a time in a list with now()
« Reply #1 on: January 19, 2011, 05:23:51 pm »
How accurate do you want it? Seconds? Then try this:
Code: [Select]
if Trunc(SecondSpan(StrToTime(lstTimes.Items[0]),TimeOf(Now))) = 0 then

PeterATC

  • New Member
  • *
  • Posts: 12
Re: Comparing a time in a list with now()
« Reply #2 on: January 25, 2011, 08:19:53 pm »
Thank LeloDumbo works like a charm. Not sure if this was the same orginal code I used when I wrote it in Delphi but it works now...

Leledumbo

  • Hero Member
  • *****
  • Posts: 8747
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Comparing a time in a list with now()
« Reply #3 on: January 26, 2011, 01:53:53 am »
Actually, if you want it to be exactly the same, there's a SameDateTime function in DateUtils unit. I just browse my API docs and I don't remember seeing this one before.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Comparing a time in a list with now()
« Reply #4 on: January 26, 2011, 12:03:13 pm »
A little lagspike in operating system can cause the timer to not activate on that specific second. Therefore the comparison should always be "Current time greater than event time" and not equal, then setting that "event executed = true" and that disables it from triggering twice.

 

TinyPortal © 2005-2018