如何检查DateTime周期的交点 [英] How check intersection of DateTime periods
问题描述
A1,A2和B1,B2。
我需要知道A1-A2周期与周期B1-B2不相交。但是我不想写脏的代码,像许多块,如果(A1< B1&&&&&&& A2> B1)
{
return false;
}
....
etc。
EDITED
我尝试使用这个:比较范围
DateTime A1 = DateTime.MinValue.AddMinutes(61);
DateTime A2 = DateTime.MinValue.AddHours(1.2);
DateTime B1 = DateTime.MinValue.AddMinutes(5);
DateTime B2 = DateTime.MinValue.AddHours(1);
Console.WriteLine(Range.Overlap(
new Range< DateTime>(A1,A2),
new Range< DateTime>(B1,B2)
) );
它返回 true ,但我预计 false 。
因为此代码总是返回true
if(left.Start.CompareTo(left.Start)== 0)
{
return true;
}
我不相信有要写任何方式的简单代码;你必须占用4个不同的用例。如果你需要这样做很多,我会写一个扩展方法。否则,您只需要检查这些条件:
| ---日期1 --- |
| ---日期2 --- |
| ---日期1 --- |
| ---日期2 ---- |
| --------日期1 -------- |
| ---日期2 --- |
| ---日期1 --- |
| --------日期2 -------- |
编辑:提供实际代码:
public class DateTimeRange
{
public DateTime Start {get;组; }
public DateTime End {get;组;
public bool Intersects(DateTimeRange test)
{
if(this.Start> this.End || test.Start> test.End)
抛出新的InvalidDateRangeException();
if(this.Start == this.End || test.Start == test.End)
return false; //没有实际的日期范围
if(this.Start == test.Start || this.End == test.End)
return true; //如果任何集合是相同的时间,那么默认情况下必须有一些重叠。
if(this.Start< test.Start)
{
if(this.End> test.Start&& this.End< test.End )
返回true; //条件1
if(this.End> test.End)
return true; //条件3
}
else
{
if(test.End> this.Start&& test.End< this.End)
返回真//条件2
if(test.End> this.End)
return true; //条件4
}
return false;
}
}
这应该涵盖用例。
I have four DateTime objects. A1, A2 and B1, B2.
I need to know that the period A1-A2 doesn't intersect with period B1-B2. But I don`t want to write dirty code, like many if blocks.
if (A1 < B1 && A2 > B1)
{
return false;
}
.... etc.
EDITED
I tried to use this one: Comparing ranges
DateTime A1 = DateTime.MinValue.AddMinutes(61);
DateTime A2 = DateTime.MinValue.AddHours(1.2);
DateTime B1 = DateTime.MinValue.AddMinutes(5);
DateTime B2 = DateTime.MinValue.AddHours(1);
Console.WriteLine(Range.Overlap(
new Range<DateTime>(A1, A2),
new Range<DateTime>(B1, B2)
));
It returned true but I expected false. Because this code always returns true
if (left.Start.CompareTo(left.Start) == 0)
{
return true;
}
I dont believe there is going to be any manner of 'easy' code to write; you have to account for 4 distinct use cases. If you need to do this kind of check a lot, I'd write an extension method. Otherwise, you just need to check these conditions:
|--- Date 1 ---|
| --- Date 2 --- |
| --- Date 1 --- |
| --- Date 2 ---- |
| -------- Date 1 -------- |
| --- Date 2 --- |
| --- Date 1 --- |
| -------- Date 2 -------- |
EDIT: To provide actual code:
public class DateTimeRange
{
public DateTime Start { get; set; }
public DateTime End { get; set; }
public bool Intersects(DateTimeRange test)
{
if(this.Start > this.End || test.Start > test.End)
throw new InvalidDateRangeException();
if(this.Start == this.End || test.Start == test.End)
return false; // No actual date range
if(this.Start == test.Start || this.End == test.End)
return true; // If any set is the same time, then by default there must be some overlap.
if(this.Start < test.Start)
{
if(this.End > test.Start && this.End < test.End)
return true; // Condition 1
if(this.End > test.End)
return true; // Condition 3
}
else
{
if(test.End > this.Start && test.End < this.End)
return true; // Condition 2
if(test.End > this.End)
return true; // Condition 4
}
return false;
}
}
That should cover the use cases.
这篇关于如何检查DateTime周期的交点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!