(StartA <= EndB) and (EndA >= StartB)
((StartA <= EndB)和(EndA> = StartB))
Proof:
(证明:)
Let ConditionA Mean that DateRange A Completely After DateRange B
(让ConditionA表示DateRange A完全在DateRange B之后)
_ |---- DateRange A ------| |---Date Range B -----| _
(True if StartA > EndB
)
((如果StartA > EndB
真))
Let ConditionB Mean that DateRange A is Completely Before DateRange B
(让ConditionB表示DateRange A完全早于DateRange B)
|---- DateRange A -----| _ _ |---Date Range B ----|
(True if EndA < StartB
)
((如果EndA < StartB
真))
Then Overlap exists if Neither A Nor B is true -
(如果A和B都不为真,则存在重叠-)
(If one range is neither completely after the other,
((如果一个范围不完全在另一个范围之后,)
nor completely before the other, then they must overlap.)
(也不完全在另一个之前,那么它们必须重叠。))
Now one of De Morgan's laws says that:
(现在, De Morgan的一项法律规定 :)
Not (A Or B)
<=> Not A And Not B
(Not (A Or B)
<=> Not A And Not B
)
Which translates to: (StartA <= EndB) and (EndA >= StartB)
(转换为: (StartA <= EndB) and (EndA >= StartB)
)
NOTE: This includes conditions where the edges overlap exactly.
(注意:这包括边缘完全重叠的条件。)
If you wish to exclude that, (如果您希望排除在外,)
change the >=
operators to >
, and <=
to <
(将>=
运算符更改为>
,将<=
更改为<
)
NOTE2.
(笔记2。)
Thanks to @Baodad, see this blog , the actual overlap is least of: (感谢@Baodad,请参阅此博客 ,实际重叠至少是:)
{ endA-startA
, endA - startB
, endB-startA
, endB - startB
}
({ endA-startA
, endA - startB
, endB-startA
, endB - startB
})
(StartA <= EndB) and (EndA >= StartB)
(StartA <= EndB) and (StartB <= EndA)
((StartA <= EndB) and (EndA >= StartB)
(StartA <= EndB) and (StartB <= EndA)
)
NOTE3.
(注意3。)
Thanks to @tomosius, a shorter version reads: (感谢@tomosius,一个简短的版本显示为:)
DateRangesOverlap = max(start1, start2) < min(end1, end2)
This is actually a syntactical shortcut for what is a longer implementation, which includes extra checks to verify that the start dates are on or before the endDates.
(这实际上是较长实现的语法快捷方式,其中包括额外的检查,以验证开始日期在endDates或之前。)
Deriving this from above: (从上面导出:)
If start and end dates can be out of order, ie, if it is possible that startA > endA
or startB > endB
, then you also have to check that they are in order, so that means you have to add two additional validity rules:
(如果开始日期和结束日期可能不正确,即,如果startA > endA
或startB > endB
,那么您还必须检查它们是否顺序正确,这意味着您必须添加两个其他有效性规则:)
(StartA <= EndB) and (StartB <= EndA) and (StartA <= EndA) and (StartB <= EndB)
or:
((StartA <= EndB) and (StartB <= EndA) and (StartA <= EndA) and (StartB <= EndB)
或:)
(StartA <= EndB) and (StartA <= EndA) and (StartB <= EndA) and (StartB <= EndB)
or,
((StartA <= EndB) and (StartA <= EndA) and (StartB <= EndA) and (StartB <= EndB)
或)
(StartA <= Min(EndA, EndB) and (StartB <= Min(EndA, EndB))
or:
((StartA <= Min(EndA, EndB) and (StartB <= Min(EndA, EndB))
或:)
(Max(StartA, StartB) <= Min(EndA, EndB)
But to implement Min()
and Max()
, you have to code, (using C ternary for terseness),:
(但是要实现Min()
和Max()
,您必须进行编码(使用C三进制来简洁):)
(StartA > StartB? Start A: StartB) <= (EndA < EndB? EndA: EndB)