/// <summary> /// Check if this new shift occurs the next morning after a night shift or night before a morning shift /// </summary> /// <param name="newShift">The new shift</param> /// <returns>true if it is consecutive overnight, false otherwise</returns> public Shift ReturnConsecutiveOpenCloseShifts(Shift newShift) { bool checkYesterday = false; // check if this is even a morning or night shift if (!(newShift.StartTime <= TimeUtilities.ConvertToDateTime("0900") || newShift.EndTime >= TimeUtilities.ConvertToDateTime("2100"))) { // not an opening or closing shift - return false return(null); } // holds the target start and ened times DateTime targetStart = newShift.StartTime; DateTime targetEnd = newShift.EndTime; // get the index of this shifts day int index = TimeUtilities.daysOfWeek.IndexOf(newShift.Day); int target = 0; // get the target index of the day we wish to check if (newShift.StartTime <= TimeUtilities.ConvertToDateTime("0900")) { // this shift is an opening shift // we will need to check previous day checkYesterday = true; // increment target times by 1 day targetStart = targetStart.AddDays(1); targetEnd = targetEnd.AddDays(1); // check if current index is monday if (index == 0) { // set target to sunday target = TimeUtilities.daysOfWeek.Count - 1; } else { // otherwise subtract 1 target = index - 1; } } else if (newShift.EndTime >= TimeUtilities.ConvertToDateTime("2100")) {// this shift is an opening shift // we will check next day's shift checkYesterday = false; // increment target times by 1 day targetStart.AddDays(-1); targetEnd.AddDays(-1); // check if current index is sunday if (index == TimeUtilities.daysOfWeek.Count - 1) { // set target to monday target = 0; } else { // otherwise add 1 target = index + 1; } } else { // should be impossible to reach this point due to first if statement at top of method throw new Exception("WTF?! How'd you get here?!?"); } // do the actual checking foreach (Shift shift in this._shiftsWorking._shifts[TimeUtilities.daysOfWeek[target]]) { // if we're checking yesterday if (checkYesterday) { TimeSpan difference = (targetStart - shift.EndTime); // get time difference and see if it is at least or less than expected amount if (difference <= new TimeSpan(11, 30, 0)) { // these are consecutive shifts, return true return(shift); } } else { // we are checking tomorrow // get time difference and see if it is at least or less than expected amount if ((targetEnd - shift.StartTime) <= new TimeSpan(11, 30, 0)) { // these are consecutive shifts, return true return(shift); } } } // never found a conflicting shift return(null); }