patterncsharpCritical
Algorithm to detect overlapping periods
Viewed 0 times
overlappingperiodsdetectalgorithm
Problem
I've to detect if two time periods are overlapping.
Every period has a start date and an end date.
I need to detect if my first time period (A) is overlapping with another one(B/C).
In my case, if the start of B is equal to the end of A, they are not overlapping(the inverse too)
I found the following cases:
So actually I'm doing this like this:
(The case 4 is taken in the account either in case 1 or in case 2)
It works, but it seems not very efficient.
So, first is there an existing class in c# that can modelize this(a time period), something like a timespan, but with a fixed start date.
Secondly: Is there already a c# code(like in the
Third: if no, what would be your approach to make this comparison the most fast?
Every period has a start date and an end date.
I need to detect if my first time period (A) is overlapping with another one(B/C).
In my case, if the start of B is equal to the end of A, they are not overlapping(the inverse too)
I found the following cases:
So actually I'm doing this like this:
tStartA tEndA //For case 3(The case 4 is taken in the account either in case 1 or in case 2)
It works, but it seems not very efficient.
So, first is there an existing class in c# that can modelize this(a time period), something like a timespan, but with a fixed start date.
Secondly: Is there already a c# code(like in the
DateTime class) which can handle this?Third: if no, what would be your approach to make this comparison the most fast?
Solution
Simple check to see if two time periods overlap:
or in your code:
(Use
bool overlap = a.start < b.end && b.start < a.end;or in your code:
bool overlap = tStartA < tEndB && tStartB < tEndA;(Use
<= instead of < if you change your mind about wanting to say that two periods that just touch each other overlap.)Code Snippets
bool overlap = a.start < b.end && b.start < a.end;bool overlap = tStartA < tEndB && tStartB < tEndA;Context
Stack Overflow Q#13513932, score: 1042
Revisions (0)
No revisions yet.