user1937012
user1937012

Reputation: 385

Check if Time Period Overlaps

i have 3 Configurable Time Intervals , right now set to :

1 06:00 - 14:30
2 14:30 - 23:00
3 23:00 - 06:00

I would like to check if the Time Intervals overlap if the user changes it , and then prevent the change .

But I am not sure how to do this , I googled, I used a bit of IRC , they told me I could do :

Start1 < Stop3
Start2 < Stop1
Start3 < Stop2

This would work fine until I've set

Stop3 = 23:00
Start1 = 00:00

Any tips perhaps ?

Thank you

Upvotes: 1

Views: 423

Answers (1)

LU RD
LU RD

Reputation: 34929

Since the time periods could extend to the next day, you must add a fictive date.

If your time values are in a TTime type, and the testing variables are in a TDateTime type:

if (StartTime1 > StopTime1) then begin // Extend time to next day
  StartDT1 := StartTime1;
  StopDT1 := DateUtils.IncDay(StopTime1,1);
end
else begin
  StartDT1 := StartTime1;
  StopDT1 := StopTime1;      
end;  

Do this for all three intervals.

Then it is easy to check for overlap with this test:

overlap := (StartDT1 < StopDT2) and (StartDT2 < StopDT1) or
           (StartDT1 < StopDT3) and (StartDT3 < StopDT1) or
           (StartDT2 < StopDT3) and (StartDT3 < StopDT2);

Upvotes: 2

Related Questions