public void CalculateNextExecution() { NextExecution = Start; switch (Type) { case TriggerType.Time: if (RepeatInterval != TimeSpan.Zero) { //Handle repeat var endRepeatDate = (RepeatDuration != TimeSpan.Zero ? NextExecution + RepeatDuration : DateTime.MaxValue); if (endRepeatDate < DateTime.Now) { break; } while (true) { if (NextExecution > DateTime.Now || NextExecution > EndFinal || NextExecution > endRepeatDate) { //Found within repeat break; } NextExecution += RepeatInterval; } } break; case TriggerType.Daily: while (true) { if (NextExecution > DateTime.Now || NextExecution > EndFinal) { //Found break; } if (RepeatInterval != TimeSpan.Zero) { //Handle repeat var nextRepeatExecution = NextExecution; var endRepeatDate = (RepeatDuration != TimeSpan.Zero ? nextRepeatExecution + RepeatDuration : DateTime.MaxValue); while (endRepeatDate > DateTime.Now) { if (nextRepeatExecution > DateTime.Now || nextRepeatExecution > EndFinal || nextRepeatExecution > endRepeatDate) { break; } nextRepeatExecution += RepeatInterval; } if (nextRepeatExecution > DateTime.Now || nextRepeatExecution > EndFinal) { //Found within repeat NextExecution = nextRepeatExecution; break; } } NextExecution = NextExecution.AddDays(DaysInterval); } break; case TriggerType.Weekly: while (true) { if (Weekdays.Length == 0) { break; } //Check day of week for (int i = 0; i < 6; i++) { var nextWeekExecution = NextExecution.AddDays(i); if (!Weekdays.Contains((int)nextWeekExecution.DayOfWeek)) { continue; } if (nextWeekExecution > DateTime.Now || nextWeekExecution > EndFinal) { //Found NextExecution = nextWeekExecution; break; } if (RepeatInterval != TimeSpan.Zero) { //Handle repeat var nextRepeatExecution = nextWeekExecution; var endRepeatDate = (RepeatDuration != TimeSpan.Zero ? nextRepeatExecution + RepeatDuration : DateTime.MaxValue); while (endRepeatDate > DateTime.Now) { if (nextRepeatExecution > DateTime.Now || nextRepeatExecution > EndFinal || nextRepeatExecution > endRepeatDate) { break; } nextRepeatExecution += RepeatInterval; } if (nextRepeatExecution > DateTime.Now || nextRepeatExecution > EndFinal) { //Found within repeat NextExecution = nextRepeatExecution; break; } } } if (NextExecution > DateTime.Now || NextExecution > EndFinal) { //Found break; } NextExecution = NextExecution.AddDays(7 * WeeksInterval); } break; case TriggerType.Monthly: while (true) { if (Months.Length == 0) { break; } if (Days.Length == 0) { break; } //Check days and months bool isLastDayOfMonth = (Days.Contains(32) && Months.Contains(NextExecution.Month) && NextExecution.Day == DateTime.DaysInMonth(NextExecution.Year, NextExecution.Month)); if (!isLastDayOfMonth) { if (!Days.Contains(NextExecution.Day) || !Months.Contains(NextExecution.Month)) { NextExecution = NextExecution.AddDays(1); continue; } } if (NextExecution > DateTime.Now || NextExecution > EndFinal) { //Found break; } if (RepeatInterval != TimeSpan.Zero) { //Handle repeat var nextRepeatExecution = NextExecution; var endRepeatDate = (RepeatDuration != TimeSpan.Zero ? nextRepeatExecution + RepeatDuration : DateTime.MaxValue); while (endRepeatDate > DateTime.Now) { if (nextRepeatExecution > DateTime.Now || nextRepeatExecution > EndFinal || nextRepeatExecution > endRepeatDate) { break; } nextRepeatExecution += RepeatInterval; } if (nextRepeatExecution > DateTime.Now || nextRepeatExecution > EndFinal) { //Found within repeat NextExecution = nextRepeatExecution; break; } } //Check next day NextExecution = NextExecution.AddDays(1); } break; } if (NextExecution < DateTime.Now) { NextExecution = DateTime.MaxValue; } }
public bool this[int day] => Weekdays.Contains((Weekday)day);