根据DateTime.Now和预定义的小时数检测工作班次 C#

问题描述

我希望我的程序在新班次开始时重置统计信息。 我的轮班时间预定义为:

  • 白班4:00-周一至周四(4:00-15:55周五至周日)
  • 周一至周四凌晨16:25-4:00(周五至周日15:55-4:00)

这是我目前正在做的事情,它可以工作,但是我无法弄清楚在班次结束时在AM中如何进行时间比较,因此现在当设置班次结束时它可以工作到23:59:59,但是当我将其更改为凌晨4点时,它掉在了屁股上...

        DateTime d1 = new DateTime(2020,01,16,25,00); //Mon-Thur shift change
        DateTime d2 = new DateTime(2020,15,55,00); //Fri-Sun shift change
        DateTime d3 = new DateTime(2020,23,59,59); //Evening shift end
        DateTime d4 = new DateTime(2020,04,00,00); //Morning shift start
        DateTime d5 = DateTime.Now;


        if (d5.DayOfWeek == DayOfWeek.Monday || d5.DayOfWeek == DayOfWeek.Tuesday || d5.DayOfWeek == DayOfWeek.Wednesday || d5.DayOfWeek == DayOfWeek.Thursday)
        {
            if (d5.TimeOfDay >= d4.TimeOfDay && d5.TimeOfDay < d1.TimeOfDay)
            {
                dayShift = true;
                eveShift = false;
            }
        }
        if (d5.DayOfWeek == DayOfWeek.Friday || d5.DayOfWeek == DayOfWeek.Saturday || d5.DayOfWeek == DayOfWeek.Sunday)
        {
            if (d5.TimeOfDay >= d4.TimeOfDay && d5.TimeOfDay < d2.TimeOfDay)
            {
                dayShift = true;
                eveShift = false;
            }
        }
        if (d5.DayOfWeek == DayOfWeek.Monday || d5.DayOfWeek == DayOfWeek.Tuesday || d5.DayOfWeek == DayOfWeek.Wednesday || d5.DayOfWeek == DayOfWeek.Thursday)
        {
            if (d5.TimeOfDay >= d1.TimeOfDay && d5.TimeOfDay < d3.TimeOfDay)
            {
                dayShift = false;
                eveShift = true;
            }
        }
        if (d5.DayOfWeek == DayOfWeek.Friday || d5.DayOfWeek == DayOfWeek.Saturday || d5.DayOfWeek == DayOfWeek.Sunday)
        {
            if (d5.TimeOfDay >= d2.TimeOfDay && d5.TimeOfDay < d3.TimeOfDay)
            {
                dayShift = false;
                eveShift = true;
            }
        }

我该怎么做才能将TimeOfDay与AM小时进行比较?

我想检查时间是否在d1和d4之间。有什么想法吗?

解决方法

我将在这里更改方法。而不是一组时间,我将定义一个对象中构成“移位”定义的内容,并列出这些对象,然后对其进行遍历并针对它们测试当前日期/时间...

最终结果比我乍看之下要复杂得多,因为实际上星期二2:33 AM会实际上到达星期一的夜间班次I猜...

但是我会这样做:

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,4,0),//4AM
            End = new DateTime(2000,16,25,//4:25PM
            Type=ShiftType.Day
        },new DailyShiftDetail
        {
            //Fri-Sun daytime shift
            DaysOfWeek=new[]
            {
                DayOfWeek.Friday,DayOfWeek.Saturday,DayOfWeek.Sunday 
            },04,15,55,//3:55PM
            Type=ShiftType.Day
        },new DailyShiftDetail
        {
            //Mon-Thu Evening shift
            DaysOfWeek=new[]
            {
                DayOfWeek.Monday,End = new DateTime(2000,Type=ShiftType.Night
        },new DailyShiftDetail
        {
            //Fri-Sun Evening shift
            DaysOfWeek=new[]
            {
                DayOfWeek.Friday,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,0));
//is ShiftType.Night

//today,tuesday 22nd @ 6AM... would be a day shift?
var dayShift = ShiftCheck.GetShiftTypeForDate(
    new DateTime(2020,6,0));
//is ShiftType.Day

//a friday at 15:57 would be a night shift
var friNightShift = ShiftCheck.GetShiftTypeForDate(
    new DateTime(2020,18,57,0));
//is ShiftType.Night

//a Wednesday at 15:57 would be a DAY shift though
var wedDayShift = ShiftCheck.GetShiftTypeForDate(
    new DateTime(2020,0));
//is ShiftType.Day.

我只是简短地测试过,所以可能在某处有一些逻辑错误,但应该相当容易调试...。