Tim*_*nes 1 .net c# datetime timespan winforms
我正在使用算法编写调度程序。在算法的最后阶段,我需要查看时间表(创建的时间表)以查看学生当时是否已被分配到班级。
因此,我们有:
Current Class Start Time: (2017, 02, 09, 10, 00, 00)
Current Class Finish Time: (2017, 02, 09, 11, 00, 00)
Run Code Online (Sandbox Code Playgroud)
此时我们就通过查询时间表来查看A同学被分配到的其他班级:
例如,假设在他们已被分配的同一日期:
Class 'Z' Start Time: (2017, 02, 09, 09, 00, 00)
Class 'Z' Finish Time: (2017, 02, 09, 12, 00, 00)
Run Code Online (Sandbox Code Playgroud)
现在我想找到'Z'班级的时间范围并将其与当前班级的时间范围进行比较。
DateTime startClassZ = new DateTime(2017, 02, 09, 09, 00, 00);
DateTime endClassZ = new DateTime(2017, 02, 09, 12, 00, 00);
DateTime StartCurrent = new DateTime(2017, 02, 09, 10, 00, 00);
DateTime StartCurrent = new DateTime(2017, 02, 09, 11, 00, 00);
if (They do not clash)
{
Assign
}
if (Clash)
{
Select Another Student
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我解决我的“IF 语句”以及如何解决这个问题。
按照我的想法,有以下三种可能:
谢谢
这是最简单的方法:
public static bool HasOverlap(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
{
return start1 < end2 && end1 > start2;
}
Run Code Online (Sandbox Code Playgroud)
或者,如果日期不一定是正确的开始/结束顺序:
public static bool HasOverlap(DateTime start1, DateTime end1, DateTime start2, DateTime end2)
{
return Min(start1, end1) < Max(start2, end2) && Max(start1, end1) > Min(start2, end2);
}
public static DateTime Max(DateTime d1, DateTime d2)
{
return d1 > d2 ? d1 : d2;
}
public static DateTime Min(DateTime d1, DateTime d2)
{
return d2 > d1 ? d1: d2;
}
Run Code Online (Sandbox Code Playgroud)
请注意,如果一堂课以 2 结束,下一堂课以 2 开始,则不会有重叠。既然你在谈论课程,我假设这就是你想要的。
测试你的例子:
static void Main(string[] args)
{
DateTime startClassZ = new DateTime(2017, 02, 09, 09, 00, 00);
DateTime endClassZ = new DateTime(2017, 02, 09, 12, 00, 00);
DateTime StartCurrent = new DateTime(2017, 02, 09, 10, 00, 00);
DateTime EndCurrent = new DateTime(2017, 02, 09, 11, 00, 00);
if(HasOverlap(startClassZ, endClassZ, StartCurrent, EndCurrent))
{
Console.WriteLine("clash");
}
else
{
Console.WriteLine("yay");
}
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
我为您添加了一些快速测试:
public static void Test1()
{
// Class A overlaps class B
DateTime aStart = DateTime.Parse("2017-01-01T09:00:00");
DateTime aEnd = DateTime.Parse("2017-01-01T10:00:00");
DateTime bStart = DateTime.Parse("2017-01-01T09:30:00");
DateTime bEnd = DateTime.Parse("2017-01-01T11:00:00");
bool isCorrect = HasOverlap(aStart, aEnd, bStart, bEnd) == true;
Console.WriteLine($"1: {isCorrect}");
}
public static void Test2()
{
// Class A "surrounds" class B
DateTime aStart = DateTime.Parse("2017-01-01T09:00:00");
DateTime aEnd = DateTime.Parse("2017-01-01T15:00:00");
DateTime bStart = DateTime.Parse("2017-01-01T09:30:00");
DateTime bEnd = DateTime.Parse("2017-01-01T11:00:00");
bool isCorrect = HasOverlap(aStart, aEnd, bStart, bEnd) == true;
Console.WriteLine($"2: {isCorrect}");
}
public static void Test3()
{
// Class B "surrounds" class A
DateTime aStart = DateTime.Parse("2017-01-01T09:30:00");
DateTime aEnd = DateTime.Parse("2017-01-01T11:00:00");
DateTime bStart = DateTime.Parse("2017-01-01T09:00:00");
DateTime bEnd = DateTime.Parse("2017-01-01T15:00:00");
bool isCorrect = HasOverlap(aStart, aEnd, bStart, bEnd) == true;
Console.WriteLine($"3: {isCorrect}");
}
public static void Test4()
{
// Class A is before Class B
DateTime aStart = DateTime.Parse("2017-01-01T09:00:00");
DateTime aEnd = DateTime.Parse("2017-01-01T11:00:00");
DateTime bStart = DateTime.Parse("2017-01-01T11:00:00");
DateTime bEnd = DateTime.Parse("2017-01-01T12:00:00");
bool isCorrect = HasOverlap(aStart, aEnd, bStart, bEnd) == false;
Console.WriteLine($"4: {isCorrect}");
}
public static void Test5()
{
// Class A is after Class B
DateTime aStart = DateTime.Parse("2017-01-01T12:00:00");
DateTime aEnd = DateTime.Parse("2017-01-01T14:00:00");
DateTime bStart = DateTime.Parse("2017-01-01T11:00:00");
DateTime bEnd = DateTime.Parse("2017-01-01T12:00:00");
bool isCorrect = HasOverlap(aStart, aEnd, bStart, bEnd) == false;
Console.WriteLine($"5: {isCorrect}");
}
public static void Test6()
{
// Class B overlaps class A
DateTime bStart = DateTime.Parse("2017-01-01T09:00:00");
DateTime bEnd = DateTime.Parse("2017-01-01T10:00:00");
DateTime aStart = DateTime.Parse("2017-01-01T09:30:00");
DateTime aEnd = DateTime.Parse("2017-01-01T11:00:00");
bool isCorrect = HasOverlap(aStart, aEnd, bStart, bEnd) == true;
Console.WriteLine($"6: {isCorrect}");
}
static void Main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Console.Read();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6809 次 |
| 最近记录: |