Jump to content
Sign in to follow this  
Henry Olive

Time Between

Recommended Posts

Good Day,

 

procedure TDm.BackUpTimerTimer(Sender: TObject);
  var
  MyTime, BackUpStart,BackUpEnd : TTime;
begin
  MyTime := Now;
  BackUpStart := dm.SettingBACKUPHOUR.AsDateTime; // which is 20:30
  BackUpEnd := BackUpStart + EncodeTime(0,30,0,0); // which is 21:00

 

  if ((MyTime >= BackUpStart)  and (MyTime <= BackUpEnd)) then  // this code doesnt work
     BackUp;
end;
 

Can somebody please help

Thank You
 

 

Share this post


Link to post
Posted (edited)

Hello,

 

If you made the code:

 MyTime := EncodeTime(20,45,0,0);
 BackUpStart := EncodeTime(20,30,0,0); // which is 20:30
 BackUpEnd := BackUpStart + EncodeTime(0,30,0,0); // which is 21:00

The "if" works so something in the first three lines is amiss.

I suspect "dm.SettingBACKUPHOUR.AsDateTime" is returning a full date AND time. TTime is a TDateTime. Remove the date portion if present.

 

Good luck,

 

Mark

 

Edited by Mark-

Share this post


Link to post
Posted (edited)

Thank You Mark,

dm.SettingBACKUPHOUR = Varchar(5)  and the value = '20:30'

 

Edited by Henry Olive

Share this post


Link to post
Posted (edited)

OK, so what is the problem?

Did you examine the value of "BackUpStart" in the debugger?

Edited by Mark-

Share this post


Link to post

Eventhough the time is between 20:30  21:00 below code doesnt work 

  if ((MyTime >= BackUpStart)  and (MyTime <= BackUpEnd)) then  

i check MyTime, BackUpStart, BackUpEnd all of them are correct when debugging

  if ((MyTime >= BackUpStart) then // this works but both with BackUpEnd doesnt work

Share this post


Link to post
Posted (edited)

If you set the time values, per the code I used, the "if" does work.

Which leads back to "BackUpStart := dm.SettingBACKUPHOUR.AsDateTime; // which is 20:30".

If you replace it with:

BackUpStart := EncodeTime(20,30,0,0); // which is 20:30

"if" does work. Right? Back to "BackUpStart := dm.SettingBACKUPHOUR.AsDateTime;".

 

Later;

I am wondering if the debugger inspector knows it is defined as TTime and truncates the date portion for display. Not sure that is possible.

If you change BackUpStart to TDateTime, does it display the same value in the debugger?

Edited by Mark-

Share this post


Link to post
1 hour ago, Henry Olive said:

Eventhough the time is between 20:30  21:00 below code doesnt work 

Probably because Now contains the date and the time, while EncodeTime uses an implicit date of 0.

 

You may have better results with

  MyTime := Time; // uses the time part only
  BackUpStart := dm.SettingBACKUPHOUR.AsDateTime; // which is 20:30
  BackUpEnd := BackUpStart + EncodeTime(0,30,0,0); // which is 21:00

 

 

Share this post


Link to post
Posted (edited)
15 minutes ago, Uwe Raabe said:

Probably because Now contains the date and the time, while EncodeTime uses an implicit date of 0. 

I wondered that also. Time and Now put the same value in MyTime, according to the debugger inspector. I "thought" the compiler would remove the date porition of Now because MyTime is TTime not TDateTime.

 

But if I turn off the "Visualizer" in the watch properties and look at MyTime (Now) as a float it is 44689.4217036343.

And MyTime (Time) as a float it is 0.422856287...

 

So for me, use Time and for safety "Frac" the result of "dm.SettingBACKUPHOUR.AsDateTime;"

BackUpStart := Frac(dm.SettingBACKUPHOUR.AsDateTime);

Edited by Mark-

Share this post


Link to post
6 hours ago, Mark- said:

and for safety "Frac"

There is already TimeOf for that.

Share this post


Link to post
1 hour ago, Uwe Raabe said:

There is already TimeOf for that.

Yes, which is:

  Result := Frac(AValue);

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×