private static DateTime CalculateMonthlyRelative(this Schedule schedule, DateTime lastRunDateTime) { var intrval = schedule.Interval; var endtime = schedule.EndTime; var subdayType = schedule.SubdayType; var subdayInterval = schedule.SubdayInterval; var dt = schedule.CalculateSubDayInterval(lastRunDateTime); var rst = dt.TimeOfDay; // Check to see if we need to roll to the next interval day if (rst > endtime || dt.Day != lastRunDateTime.Day) { // Set the Next Run Time to the Active Start Time Value lastRunDateTime = lastRunDateTime + schedule.StartTime; // How many months should be skipped var nmonths = schedule.RecurrenceFactor; var i = 0; while (i < nmonths) { // Calculate the number of days in a given month and increment the current dateTime object dt = dt.AddDays(((dt.Day - DateTime.DaysInMonth(dt.Year, dt.Month)) * -1) + 1); i++; } bool resultfound = false; // Create Date Time Array to hold first/second/third/forth day of the week List <DateTime> daysofmonth = new List <DateTime>(); // Calculate the Monthly Interval DateTime _smp = dt; int s = 0; while (s < DateTime.DaysInMonth(_smp.Year, _smp.Month)) { WeeklyInterval wd = (WeeklyInterval)Enum.Parse(typeof(WeeklyInterval), _smp.DayOfWeek.ToString(), true); if ((intrval & (int)wd) != 0) // Non Zero results in a positive match { daysofmonth.Add(_smp); switch ((RelativeInterval)schedule.RelativeInterval) { case RelativeInterval.First: if (daysofmonth.Count == 1) { resultfound = true; } break; case RelativeInterval.Second: if (daysofmonth.Count == 2) { resultfound = true; } break; case RelativeInterval.Third: if (daysofmonth.Count == 3) { resultfound = true; } break; case RelativeInterval.Fourth: if (daysofmonth.Count == 4) { resultfound = true; } break; } } if (resultfound == true) { break; } _smp.AddDays(1); s++; } // Calculate the Difference between the two days TimeSpan _datediff = _smp.Subtract(dt); // Add the Difference to the current time object dt = dt.Add(_datediff); // Set the Value lastRunDateTime = dt; } else // We haven't exceeded the end time range yet so increment the time and continue. { lastRunDateTime = lastRunDateTime + rst; } return(lastRunDateTime); }
private static DateTime CalculateWeekly(this Schedule schedule, DateTime lastRunDateTime) { var dt = schedule.CalculateSubDayInterval(lastRunDateTime); // Check to see if we need to roll to the next interval day if (dt.TimeOfDay > schedule.EndTime || dt.Day != lastRunDateTime.Day) { lastRunDateTime = lastRunDateTime.Date + schedule.StartTime; // Assume we'll reach the end of the week without a match bool iswithinWeek = false; // Start iterations at the Current Day of the Week int i = (int)lastRunDateTime.DayOfWeek; // Loop through the days of the Week while (i <= 6) { // Get the WeekDay Interval value from the given Day of the Week var wd = (WeeklyInterval)Enum.Parse(typeof(WeeklyInterval), dt.DayOfWeek.ToString(), true); // use Bit wise & operator to determine of the weekday is within the interval if ((schedule.Interval & (int)wd) == 0) { // No Match was found so Increment the interval to the next day. dt = dt.Add(TimeSpan.FromDays(1)); } else { // Match was found and we're still within this week, so no need to increment Week Interval iswithinWeek = true; break; } i++; } if (schedule.RecurrenceFactor != 0 && iswithinWeek == false) { // Increment the current date object by the number of weeks (7 days) until the first interval day is reached WeeklyInterval fid = GetFirstWeekDayDay(schedule.Interval); int wi = schedule.RecurrenceFactor * 7; // Increment the Current date object until we reach the end of the week (Sunday) // Add Week Interval value // Increment the Days until we reach the First selected Day of the week int dw = (((int)dt.DayOfWeek - 6) * -1) + wi; dt = dt.Add(TimeSpan.FromDays(dw)); int j = 0; while (j < 6) { if (fid == (WeeklyInterval)Enum.Parse(typeof(WeeklyInterval), dt.DayOfWeek.ToString(), true)) { break; } else { dt = dt.Add(TimeSpan.FromDays(1)); } j++; } } lastRunDateTime = dt; } else // We haven't exceeded the end time range yet so increment the time and continue. { lastRunDateTime = lastRunDateTime + dt.TimeOfDay; } return(lastRunDateTime); }