具有可空日期的日期范围重叠 [英] Date Range Overlap with Nullable Dates

查看:78
本文介绍了具有可空日期的日期范围重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找此处提出的问题的扩展答案:

I'm looking for an extended answer to the question asked here:

确定两个日期范围是否重叠

,其中任一日期范围内的任何日期都可以为null.我已经提出了以下解决方案,但不确定是否可以进一步简化.

where any of the dates in either date range can be null. I've come up with the following solution, but I'm not sure if it can be simplified further.

(StartA == NULL || StartA <= EndB) &&
(EndA == NULL || EndA >= StartB) &&
(StartB == NULL || StartB <= EndA) &&
(EndB == NULL || EndB >= StartA)

假设:

从StartA到EndA的日期时间范围,从StartB到EndB的日期时间

DateTime ranges of StartA to EndA and StartB to EndB

抱歉,我很快将以上逻辑放在一起,当任一范围的开始日期和结束日期均为NULL时,这似乎失败了.有关详情,请参见下面的David解决方案.解释清楚的方法.

Sorry I quickly threw the above logic together, which seems to fail when either range's start and end dates are NULL. See David's solution below for a better & well-explained approach.

推荐答案

这种情况可以通过

让CondA平均DateRange A完全在DateRange B之后(如果StartA> EndB,则为True) 让CondB表示DateRange A完全早于DateRange B(如果EndA< StartB,则为真)

Let CondA Mean DateRange A Completely After DateRange B (True if StartA > EndB) Let CondB Mean DateRange A Completely Before DateRange B (True if EndA < StartB)

在这种情况下,假设您想要一个空日期来表示无开始/结束界限",则条件将被修改.例如,对于CondA,为了使DateRange A完全在DateRange B之后,DateRange A必须具有定义的开始时间,DateRange B必须具有定义的结束时间 A必须在B的结束时间之后:

In this case, assuming you want a null date to represent "no starting/ending bound," the conditions are modified. For CondA, for instance, in order for DateRange A to be completely after DateRange B, DateRange A must have a defined starting time, DateRange B must have a defined ending time, and the starting time of A must be after the ending time of B:

CondA := (StartA != null) && (EndB != null) && (StartA > EndB)

CondB与A和B切换相同:

CondB := (StartB != null) && (EndA != null) && (StartB > EndA)

继续

如果A或B都不为真,则存在重叠

Then Overlap exists if Neither A Nor B is true

Overlap := !(CondA || CondB)

现在我认为是德摩根定律

Now deMorgan's law, I think it is, says that

不是(A或B)< =>不是A而不是B

Not (A Or B) <=> Not A And Not B

Overlap == !CondA && !CondB
        == ![(StartA != null) && (EndB != null) && (StartA > EndB)] &&
           ![(StartB != null) && (EndA != null) && (StartB > EndA)]
        == [(StartA == null) || (EndB == null) || (StartA <= EndB)] &&
           [(StartB == null) || (EndA == null) || (StartB <= EndA)]

我认为这实际上比您开发的解决方案要健壮得多,因为如果EndB == NULLStartA不为null,则比较StartA <= NULL时您的第一个条件就会结束.在我熟悉的大多数语言中,这是一种错误情况.

I think this is actually a bit more robust than the solution you developed, because if EndB == NULL but StartA is not null, your first condition will wind up comparing StartA <= NULL. In most languages I'm familiar with, that's an error condition.

这篇关于具有可空日期的日期范围重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆