private Shift GetShift(DayOfWeek day, string strShift) { if (!Shifts.Any(x => x.Start.DayOfWeek == day && x.ShiftName.Trim() == strShift)) { return(null); } Shift shift = Shifts.First(x => x.Start.DayOfWeek == day && x.ShiftName.Trim() == strShift); return(shift); }
/// <summary> /// Returns the next available <see cref="TimeBlock"/> at the specified <see cref="DateTime"/>. /// </summary> public TimeBlock GetNextBlock(DateTime time) { DayOfWeek day = time.DayOfWeek; if (Shifts.Count == 0) { throw new Exception("There must at least be one shift specified."); } int dayOffset = 0; // force offset if the shift is on the same day, but it already ended. if (Shifts.Any(x => x.Day == day)) { Shift last = Shifts.First(x => x.Day == day); DateTime lastFrom = time.Date.AddHours(last.StartingHour).AddMinutes(last.StartingMinute); DateTime lastTo = lastFrom.Add(last.Length); TimeBlock lastBlock = new TimeBlock(lastFrom, lastTo); // if the last shift is out of range, force another day so it can read the next upcoming shifts. if (!lastBlock.IsBefore(time) && !lastBlock.Contains(time)) { day = (DayOfWeek)((int)(day + 1) % 7); dayOffset++; } else { return(lastBlock); } } while (!Shifts.Any(x => x.Day == day)) { day = (DayOfWeek)((int)(day + 1) % 7); dayOffset++; } Shift next = Shifts.First(x => x.Day == day); DateTime from = time.Date.AddDays(dayOffset) .AddHours(next.StartingHour).AddMinutes(next.StartingMinute); DateTime to = from.Add(next.Length); Console.WriteLine(from.ToLongTimeString()); Console.WriteLine(to.ToLongTimeString()); return(new TimeBlock(from, to)); }