DateTime overlap - see if new period overlaps

  • Thread starter Thread starter ApeX
  • Start date Start date
A

ApeX

I got a question about checking if the periods overlap when inserting
new row with two datetime objects TS and TE :

BS - Existing period start
BE - Existing period end
TS - New period start
TE - New period end

i know i could just use
****************************
return !((TS < BS && TE < BS) || (TS > BE && TE > BE));
*********************************

to see if they overlap and return if they don't or do, but the problem
is that the TE period can be NULL,
and when inserting new TS when there already exists a BS with a NULL
BE, the code breaks because of the NULL date in the IF clause.

Thanx!
 
I got a question about checking if the periods overlap when inserting
new row with two datetime objects TS and TE :

BS - Existing period start
BE - Existing period end
TS - New period start
TE - New period end

i know i could just use
****************************
return !((TS < BS && TE < BS) || (TS > BE && TE > BE));
*********************************

to see if they overlap and return if they don't or do, but the problem
is that the TE period can be NULL,
and when inserting new TS when there already exists a BS with a NULL
BE, the code breaks because of the NULL date in the IF clause.

A couple of comments:

C#, like many other languages, will short-circuit logic.

If you enforce the rule that S<=E when E is not null, the above
logic simplifies to:

return TS <= BE && TE >= BS;

which doesn't really feel correct. If something ends at 4 and
something else starts at 4, is that really an overlap?

The correct logic for nulls depends on what having a null
as the period end actually means.

__
Geoff
 
Back
Top