IEnumerable<DateTime> GetScheduledDateTimes(JobSchedule schedule) { var scheduledDateTimes = new List<DateTime>(); for (var counter = 0; counter > 0 - schedule.AllowedDelayDays; counter--) { ScheduledType type; if (!TryParse(schedule.JobScheduleType.Type, out type)) continue; var date = Time.AddDays(counter); switch (type) { case Daily: scheduledDateTimes.Add(new DateTime(date.Year, date.Month, date.Day, schedule.Hour, schedule.Minute, 0)); break; case Weekly: if (DayOfWeekQualifies(schedule, date)) scheduledDateTimes.Add(new DateTime(date.Year, date.Month, date.Day, schedule.Hour, schedule.Minute, 0)); break; case Monthly: if (MonthQualifies(schedule, date) && DayQualifies(schedule, date)) scheduledDateTimes.Add(new DateTime(date.Year, date.Month, date.Day, schedule.Hour, schedule.Minute, 0)); break; case Minutely: case Hourly: break; default: throw new ArgumentOutOfRangeException(); } } return scheduledDateTimes.Where(dateTime => dateTime <= Time) .ToList(); }
static bool DayQualifies(JobSchedule schedule, DateTime datetime) { if (schedule.LastDayOfMonth) { var endOfMonth = new DateTime(datetime.Year, datetime.Month, DaysInMonth(datetime.Year, datetime.Month)); if (datetime.Day == endOfMonth.Day) return true; } switch (datetime.Day) { case 1: return schedule.One; case 2: return schedule.Two; case 3: return schedule.Three; case 4: return schedule.Four; case 5: return schedule.Five; case 6: return schedule.Six; case 7: return schedule.Seven; case 8: return schedule.Eight; case 9: return schedule.Nine; case 10: return schedule.Ten; case 11: return schedule.Eleven; case 12: return schedule.Twelve; case 13: return schedule.Thirteen; case 14: return schedule.Fourteen; case 15: return schedule.Fifteen; case 16: return schedule.Sixteen; case 17: return schedule.Seventeen; case 18: return schedule.Eighteen; case 19: return schedule.Nineteen; case 20: return schedule.Twenty; case 21: return schedule.TwentyOne; case 22: return schedule.TwentyTwo; case 23: return schedule.TwentyThree; case 24: return schedule.TwentyFour; case 25: return schedule.TwentyFive; case 26: return schedule.TwentySix; case 27: return schedule.TwentySeven; case 28: return schedule.TwentyEight; case 29: return schedule.TwentyNine; case 30: return schedule.Thirty; case 31: return schedule.ThirtyOne; default: return false; } }
static bool MonthQualifies(JobSchedule schedule, DateTime datetime) { switch (datetime.Month) { case 1: return schedule.January; case 2: return schedule.February; case 3: return schedule.March; case 4: return schedule.April; case 5: return schedule.May; case 6: return schedule.June; case 7: return schedule.July; case 8: return schedule.August; case 9: return schedule.September; case 10: return schedule.October; case 11: return schedule.November; case 12: return schedule.December; default: return false; } }
static bool DayOfWeekQualifies(JobSchedule schedule, DateTime datetime) { switch (datetime.DayOfWeek) { case Sunday: return schedule.Sunday; case Monday: return schedule.Monday; case Tuesday: return schedule.Tuesday; case Wednesday: return schedule.Wednesday; case Thursday: return schedule.Thursday; case Friday: return schedule.Friday; case Saturday: return schedule.Saturday; default: return false; } }