如何检查日期时间段相交 [英] How check intersection of DateTime periods
问题描述
我有四个datetime对象。
A1,A2和B1,B2。
我需要知道周期A1-A2不与周期B1-B2相交。但我我想知道我写的脏code,像许多if块。
如果(A1中B1和放大器;&放大器; A2> B1)
{
返回false;
}
....
等等。
EDITED
我试图用这一个:比较范围
日期时间A1 = DateTime.MinValue.AddMinutes(61);
日期时间A2 = DateTime.MinValue.AddHours(1.2);
日期时间B1 = DateTime.MinValue.AddMinutes(5);
日期时间B2 = DateTime.MinValue.AddHours(1);Console.WriteLine(Range.Overlap(
新的范围和LT; DateTime的>(A1,A2)
新的范围和LT; DateTime的>(B1,B2)
));
它返回的真正但是我预期的假。
因为这code总是返回true
如果(left.Start.CompareTo(left.Start)== 0)
{
返回true;
}
我不相信有将是'容易'code写的任何方式;你必须考虑到4个不同的用例。如果你需要做这种查了很多,我会写一个扩展方法。否则,你只需要检查这些条件:
| ---日期1 --- |
| ---日期2 --- |
| ---时间1 --- |
| ---日期2 ---- |
| --------日期1 -------- |
| ---日期2 --- | | ---时间1 --- |
| --------日期2 -------- |
编辑:提供实际的code:
公共类DateTimeRange
{
公开日期时间启动{搞定;组; }
公众的DateTime结束{搞定;组; } 公共BOOL相交(DateTimeRange测试)
{
如果(this.Start> this.End || test.Start> test.End)
抛出新InvalidDateRangeException(); 如果(this.Start == || this.End == test.Start test.End)
返回false; //没有实际的日期范围 如果(this.Start == || test.Start == this.End test.End)
返回true; //如果任何一组是相同的时间,然后通过默认,必须有一些重叠。 如果(this.Start< test.Start)
{
如果(this.End> test.Start和放大器;&安培; this.End< test.End)
返回true; //条件1 如果(this.End> test.End)
返回true; //条件3
}
其他
{
如果(test.End> this.Start和放大器;&安培; test.End< this.End)
返回true; //条件2 如果(test.End> this.End)
返回true; //条件4
} 返回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.
这篇关于如何检查日期时间段相交的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!