Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
523 views
in Technique[技术] by (71.8m points)

datetime - 确定两个日期范围是否重叠(Determine Whether Two Date Ranges Overlap)

Given two date ranges, what is the simplest or most efficient way to determine whether the two date ranges overlap?

(给定两个日期范围,确定两个日期范围是否重叠的最简单或最有效的方法是什么?)

As an example, suppose we have ranges denoted by DateTime variables StartDate1 to EndDate1 and StartDate2 to EndDate2 .

(例如,假设我们有一个用DateTime变量StartDate1EndDate1 以及 StartDate2EndDate2表示的范围。)

  ask by Ian Nelson translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

(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-startAendA - startBendB-startAendB - 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 > endAstartB > 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)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...