我已经来检测,如果两个时间段是重叠的。
I've to detect if two time periods are overlapping.
每一个时期都有一个开始日期和结束日期。
Every period has a start date and an end date.
我需要检测,如果我的第一时段(A)与另一个(B / C)重叠。
I need to detect if my first time period (A) is overlapping with another one(B/C).
在我的情况下,如果B的开始等于A的结束,他们不重叠(逆太)
In my case, if the start of B is equal to the end of A, they are not overlapping(the inverse too)
我发现了以下情况:
所以,其实我这样做是这样的:
So actually I'm doing this like this:
tStartA < tStartB && tStartB < tEndA //For case 1
OR
tStartA < tEndB && tEndB <= tEndA //For case 2
OR
tStartB < tStartA && tEndB > tEndA //For case 3
(该壳体4被取入帐户而在壳体1或情况2)
(The case 4 is taken in account either in case 1 or in case 2)
据工作,但它似乎不是很有效。
It works, but it seems not very efficient.
因此,首先是有一个现有的类在C#中,可以modelize这个(时间),有点像timepsan,但有一个固定的起始日期。
So, first is there an existing class in c# that can modelize this(a time period), something like a timepsan, but with a fixed start date.
二:有没有已经交流#code(如在DateTime类),它可以处理这个
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?
简单的检查,看看两个时间段重叠的:
Simple check to see if two time periods overlap:
bool overlap = a.start < b.end && b.start < a.end;
,或在code:
or in your code:
bool overlap = tStartA < tEndB && tStartB < tEndA;
(使用&LT; =
而不是&LT;
如果您改变了主意,想要说,两个时期只是互相接触重叠。)
(Use <=
instead of <
if you change your mind about wanting to say that two periods that just touch each other overlap.)