-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cn.hutool.core.date.LocalDateTimeUtil#isOverlap 重叠定义有误 #3215
Labels
Comments
所以这个是不完全实现,只能在一定程度上对这个做解释,具体原因见,#2725 |
v6会添加以下方法 class AllenRelation{
enum AllenIntervalRelation {
STARTS((i1, i2) -> i1.start.equals(i2.start) && i1.end.isBefore(i2.end)),
STARTED_BY((i1, i2) -> i1.start.equals(i2.start) && i1.end.isAfter(i2.end)),
DURING((i1, i2) -> i1.start.isAfter(i2.start) && i1.end.isBefore(i2.end)),
CONTAINS((i1, i2) -> i1.start.isBefore(i2.start) && i1.end.isAfter(i2.end)),
FINISHES((i1, i2) -> i1.end.equals(i2.end) && i1.start.isAfter(i2.start)),
FINISHED_BY((i1, i2) -> i1.end.equals(i2.end) && i1.start.isBefore(i2.start)),
PRECEDES((i1, i2) -> i1.end.isBefore(i2.start)),
PRECEDED_BY((i1, i2) -> i1.start.isAfter(i2.end)),
MEETS((i1, i2) -> i1.end.equals(i2.start)),
MET_BY((i1, i2) -> i1.start.equals(i2.end)),
OVERLAPS((i1, i2) -> i1.start.isBefore(i2.start) && i1.end.isAfter(i2.start) && i1.end.isBefore(i2.end)),
OVERLAPPED_BY((i1, i2) -> i1.start.isAfter(i2.start) && i1.start.isBefore(i2.end) && i1.end.isAfter(i2.end)),
EQUALS((i1, i2) -> i1.start.equals(i2.start) && i1.end.equals(i2.end));
private final BiPredicate<Interval, Interval> predicate;
AllenIntervalRelation(BiPredicate<Interval, Interval> predicate) {
this.predicate = predicate;
}
public static AllenIntervalRelation getRelation(Interval i1, Interval i2) {
for (AllenIntervalRelation relation : values()) {
if (relation.predicate.test(i1, i2)) {
return relation;
}
}
return null;
}
public static class Interval {
LocalDateTime start;
LocalDateTime end;
Interval(LocalDateTime start, LocalDateTime end) {
this.start = start;
this.end = end;
}
}
}
public static void main(String[] args) {
LocalDateTime now = LocalDateTime.now();
// For the EQUALS relation
AllenIntervalRelation.Interval i1 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
AllenIntervalRelation.Interval i2 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
System.out.println("Expected EQUALS: " + AllenIntervalRelation.getRelation(i1, i2));
// For the STARTS relation
i1 = new AllenIntervalRelation.Interval(now, now.plusHours(2));
i2 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
System.out.println("Expected STARTS: " + AllenIntervalRelation.getRelation(i1, i2));
// For the STARTED_BY relation
i1 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
i2 = new AllenIntervalRelation.Interval(now, now.plusHours(2));
System.out.println("Expected STARTED_BY: " + AllenIntervalRelation.getRelation(i1, i2));
// For the DURING relation
i1 = new AllenIntervalRelation.Interval(now.plusHours(1), now.plusHours(2));
i2 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
System.out.println("Expected DURING: " + AllenIntervalRelation.getRelation(i1, i2));
// For the CONTAINS relation
i1 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
i2 = new AllenIntervalRelation.Interval(now.plusHours(1), now.plusHours(2));
System.out.println("Expected CONTAINS: " + AllenIntervalRelation.getRelation(i1, i2));
// For the FINISHES relation
i1 = new AllenIntervalRelation.Interval(now.plusHours(2), now.plusDays(1));
i2 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
System.out.println("Expected FINISHES: " + AllenIntervalRelation.getRelation(i1, i2));
// For the FINISHED_BY relation
i1 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
i2 = new AllenIntervalRelation.Interval(now.plusHours(2), now.plusDays(1));
System.out.println("Expected FINISHED_BY: " + AllenIntervalRelation.getRelation(i1, i2));
// For the PRECEDES relation
i1 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
i2 = new AllenIntervalRelation.Interval(now.plusDays(2), now.plusDays(3));
System.out.println("Expected PRECEDES: " + AllenIntervalRelation.getRelation(i1, i2));
// For the PRECEDED_BY relation
i1 = new AllenIntervalRelation.Interval(now.plusDays(2), now.plusDays(3));
i2 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
System.out.println("Expected PRECEDED_BY: " + AllenIntervalRelation.getRelation(i1, i2));
// For the MEETS relation
i1 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
i2 = new AllenIntervalRelation.Interval(now.plusDays(1), now.plusDays(2));
System.out.println("Expected MEETS: " + AllenIntervalRelation.getRelation(i1, i2));
// For the MET_BY relation
i1 = new AllenIntervalRelation.Interval(now.plusDays(1), now.plusDays(2));
i2 = new AllenIntervalRelation.Interval(now, now.plusDays(1));
System.out.println("Expected MET_BY: " + AllenIntervalRelation.getRelation(i1, i2));
// For the OVERLAPS relation
i1 = new AllenIntervalRelation.Interval(now, now.plusDays(2));
i2 = new AllenIntervalRelation.Interval(now.plusDays(1), now.plusDays(3));
System.out.println("Expected OVERLAPS: " + AllenIntervalRelation.getRelation(i1, i2));
// For the OVERLAPPED_BY relation
i1 = new AllenIntervalRelation.Interval(now.plusDays(1), now.plusDays(3));
i2 = new AllenIntervalRelation.Interval(now, now.plusDays(2));
System.out.println("Expected OVERLAPPED_BY: " + AllenIntervalRelation.getRelation(i1, i2));
}
} |
@CherryRum 嗯……搞这么复杂么,期待PR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
版本情况
JDK版本: openjdk_8_201
hutool版本: 5.X.X(请确保最新尝试是否还有问题)
问题描述(包括截图)
检查两个时间段是否有时间重叠
https://www.ics.uci.edu/~alspaugh/cls/shr/allen.html
对于艾伦代数区间的定义,重叠仅指的是一个活动的时间段完全或部分包含在另一个活动的时间段内才存在重叠,而如果两个活动仅在开始或结束时间点上相连,则不视为存在重叠,即使在时间轴上它们是连续的。
对于该代码中的实现
理论上应该只表达 重叠(overlap)的逻辑 而不是 重叠(overlap)+遇见(meet)
代码示例:
验证代码
输出结果
cn.hutool.core.date.LocalDateTimeUtil#isOverlap
堆栈信息
测试涉及到的文件(注意脱密)
比如报错的Excel文件,有问题的图片等。
The text was updated successfully, but these errors were encountered: