# Lazarus

## Free Pascal => Beginners => Topic started by: Birger52 on September 23, 2019, 03:21:03 pm

Title: Timers and (TDate)Time...
Post by: Birger52 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?
Title: Re: Timers and (TDate)Time...
Post by: RayoGlauco 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
Title: Re: Timers and (TDate)Time...
Post by: Birger52 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)
Title: Re: Timers and (TDate)Time...
Post by: rvk 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;
Title: Re: Timers and (TDate)Time...
Post by: winni 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