我会在这里改变方法。而不是一组时间,而是定义什么构成对象中的“移位”定义,拥有这些对象的列表,然后遍历它们并针对它们测试当前日期/时间......
这最终比我乍一看要复杂得多,因为我猜周二凌晨 2 点 33 分实际上是在周一晚班结束时...
但我会这样做:
public enum ShiftType
{
Unknown, //default..
Day,
Night
}
public class DailyShiftDetail
{
public IEnumerable<DayOfWeek> DaysOfWeek {get;set;}
public DateTime Start { get; set; }
public DateTime End { get; set; }
public ShiftType Type { get; set; }
}
public class ShiftCheck
{
//define shifts on each day. This data could come from a database or something
private static IEnumerable<DailyShiftDetail> shifts = new List<DailyShiftDetail>
{
new DailyShiftDetail
{
//Mon-Thu daytime shift
DaysOfWeek=new[]
{
DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday
},
Start = new DateTime(2000,1,1,4,0,0), //4AM
End = new DateTime(2000,1,1,16,25,0), //4:25PM
Type=ShiftType.Day
},
new DailyShiftDetail
{
//Fri-Sun daytime shift
DaysOfWeek=new[]
{
DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday
},
Start = new DateTime(2000,1,1,04,0,0), //4AM
End = new DateTime(2000,1,1,15,55,0), //3:55PM
Type=ShiftType.Day
},
new DailyShiftDetail
{
//Mon-Thu Evening shift
DaysOfWeek=new[]
{
DayOfWeek.Monday, DayOfWeek.Tuesday, DayOfWeek.Wednesday, DayOfWeek.Thursday
},
Start = new DateTime(2000,1,1,16,25,0),
End = new DateTime(2000,1,1,4,0,0),
Type=ShiftType.Night
},
new DailyShiftDetail
{
//Fri-Sun Evening shift
DaysOfWeek=new[]
{
DayOfWeek.Friday, DayOfWeek.Saturday, DayOfWeek.Sunday
},
Start = new DateTime(2000,1,1,15,55,0),
End = new DateTime(2000,1,1,4,0,0),
Type=ShiftType.Night
}
};
public static ShiftType GetShiftTypeForDate(DateTime testDate)
{
//you could probably do this all with a .Where() or
//.FirstOrDefault, but the logic in the LINQ stuff
//would end up being a bit painful. This is probably
//a little less efficient but will perform fine,
//and the logic is easier to follow.
DailyShiftDetail matchingShift = null;
foreach(var testShift in shifts)
{
var shiftStart = testShift.Start.TimeOfDay;
var shiftEnd = testShift.End.TimeOfDay;
//check logic differently for evening shifts,
//as not only are the times going over midnight,
//but in fact 2AM on a tuesday is a *Monday EVENING* shift...
//So we need to check the following days too..
if (shiftStart > shiftEnd)
{
//if the testing time is earlier than the end of shift, then it's between
//midnight and the shift time.
if (testDate.TimeOfDay < shiftEnd)
{
//we need to work out what day this 'shift' would have started on...
//this will not be the DayOfWeek of the passed-in time, but the day before...
//just cast to int, subtract one, account for rollover and cast back to DayOfWeek...
int dayBefore = (int)testDate.DayOfWeek - 1;
if (dayBefore < 0)
{
dayBefore = 6;
}
var dayOfWeekBefore = (DayOfWeek)dayBefore;
//now we've worked out the theoretical day the shift starts, we can test that.
if (testShift.DaysOfWeek.Contains(dayOfWeekBefore))
{
matchingShift = testShift;
}
}
//otherwise, if the test time of day is after the start of the shift then we can check the
//current day and if that matches the shift we can return it too
else if (
testDate.TimeOfDay > shiftStart
&& testShift.DaysOfWeek.Contains(testDate.DayOfWeek))
{
matchingShift = testShift;
}
}
else
{
//daytime shift.
//simple logic. Day matches and test time between start and end.
//Assume end time is EXCLUSIVE as otherwise 4:00AM EXACTLY is in two different shifts..
if (
testShift.DaysOfWeek.Contains(testDate.DayOfWeek)
&& testShift.Start.TimeOfDay <= testDate.TimeOfDay
&& testShift.End.TimeOfDay > testDate.TimeOfDay
)
{
matchingShift = testShift;
}
}
//if we found a match, stop looping through shifts.
if (matchingShift != null)
{
break;
}
}
if (matchingShift == null)
{
//couldn't work it out, so return this...
return ShiftType.Unknown;
}
//found a shift record, return its type.
return matchingShift.Type;
}
}
要使用,您可以测试任何日期/时间并返回“白天”或“夜晚”
//Today, tuesday 22nd @ 5:55pm.. would be an evening shift?
var nightShift = ShiftCheck.GetShiftTypeForDate(
new DateTime(2020, 9, 22, 17, 55, 0));
//is ShiftType.Night
//today, tuesday 22nd @ 6AM... would be a day shift?
var dayShift = ShiftCheck.GetShiftTypeForDate(
new DateTime(2020, 9, 22, 6, 0, 0));
//is ShiftType.Day
//a friday at 15:57 would be a night shift
var friNightShift = ShiftCheck.GetShiftTypeForDate(
new DateTime(2020, 9, 18, 15, 57, 0));
//is ShiftType.Night
//a Wednesday at 15:57 would be a DAY shift though
var wedDayShift = ShiftCheck.GetShiftTypeForDate(
new DateTime(2020, 9, 16, 15, 57, 0));
//is ShiftType.Day.
我只是简单地测试过,所以可能在某个地方存在一些逻辑错误,但应该很容易调试......