Recent

Author Topic: Timers and (TDate)Time...  (Read 1027 times)

Birger52

  • Sr. Member
  • ****
  • Posts: 309
Timers and (TDate)Time...
« on: September 23, 2019, 03:21:03 pm »
I have a problem.

I want a certain thing to happen at a given time.
So I calculate how long there is to this this time like
ActionTime - Now.
This will return a TDateTime, that is actually a double.
I need to set this into the TTimers attribute Interval - which is a Cardinal.

But there is no way to convert the double to cardinal (or LongWord).
So how do I get the number to assign Interval, to get my timer to fire at he given time?
Lazarus 2.0.8 FPC 3.0.4
Win7 64bit
Playing and learning - strictly for my own pleasure.

RayoGlauco

  • Full Member
  • ***
  • Posts: 179
  • Beers: 1567
Re: Timers and (TDate)Time...
« Reply #1 on: September 23, 2019, 03:31:21 pm »
This code should work:

Code: Pascal  [Select][+][-]
  1. Timer1.Interval := Round((ActionTime - Now) * 24 * 3600 * 1000);  // convert a day fraction to miliseconds
  2.  

Just be careful not to exceed the maximum value for Timer1.Interval
« Last Edit: September 23, 2019, 03:35:11 pm by RayoGlauco »
To err is human, but to really mess things up, you need a computer.

Birger52

  • Sr. Member
  • ****
  • Posts: 309
Re: Timers and (TDate)Time...
« Reply #2 on: September 23, 2019, 03:40:48 pm »
Thx - that does the trick.

Had figured I would need to calculate the millisecond manually...
tDiff := trunc((ActionTime - now) * SecsPerDay * 1000);

(SecsPerDay defined in SysUtils)
Lazarus 2.0.8 FPC 3.0.4
Win7 64bit
Playing and learning - strictly for my own pleasure.

rvk

  • Hero Member
  • *****
  • Posts: 6169
Re: Timers and (TDate)Time...
« Reply #3 on: September 23, 2019, 03:43:02 pm »
Had figured I would need to calculate the millisecond manually...
tDiff := trunc((ActionTime - now) * SecsPerDay * 1000);

(SecsPerDay defined in SysUtils)
No need to do SecsPerDay * 1000, You can also use MSecsPerDay  :D

Code: Pascal  [Select][+][-]
  1. const
  2.    HoursPerDay = 24;
  3.    MinsPerHour = 60;
  4.    SecsPerMin  = 60;
  5.    MSecsPerSec = 1000;
  6.    MinsPerDay  = HoursPerDay * MinsPerHour;
  7.    SecsPerDay  = MinsPerDay * SecsPerMin;
  8.    MSecsPerDay = SecsPerDay * MSecsPerSec;

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: Timers and (TDate)Time...
« Reply #4 on: September 23, 2019, 06:09:45 pm »
Hi!

Little bit simpler, let sysutils do the computing. Use the TTimeStamp:

Code: Pascal  [Select][+][-]
  1. TTimeStamp = record
  2.     Time: Integer;      { Number of milliseconds since midnight }
  3.     Date: Integer;      { One plus number of days since 1/1/0001 }
  4.   end;
  5.  

So you just have to do
Code: Pascal  [Select][+][-]
  1. var T: TTimeStamp;
  2.  
  3. begin
  4. T := DateTimeToTimeStamp(ActionTime - Now);
  5. Timer1.Interval := T.Time;
  6.  
  7.  

Caveat: ActionTime must have the same date as Now!
OR
In both values you work only with the time:

Code: Pascal  [Select][+][-]
  1. T := DateTimeToTimeStamp(ActionTime - Time);


Winni

 

TinyPortal © 2005-2018