/// <summary> /// Returns the nexts scedule from the specified date time /// </summary> /// <param name="from">From date time</param> /// <returns></returns> DateTime IRecur.Next(DateTime from) { DateTime original = from; while (true) { from = Utils.GetValidMonth(_months, from); ///Save the month int month = from.Month; ///Iterate while the month is same while (month == from.Month) { ///if its a valid day return and not same date if (HasDay(from.Day) && from != original) { return(from); } ///Check the next day from = from.AddDays(1); } //Month changed, so check the last day now, else go for the next valid month if ((_days & LAST) > 0) { return(from.AddDays(-1)); } } }
DateTime IRecur.Next(DateTime from) { DateTime original = from; while (true) { ///Set the valid month from = Utils.GetValidMonth(_months, from); //First week if ((_weeks & Weeks.First) == Weeks.First) { while (from.Day < 8) { if (Utils.HasWeekday(_weekdays, Utils.GetWeekday(from.DayOfWeek)) && from != original) { return(from); } from = from.AddDays(1); } } //Second week if ((_weeks & Weeks.Second) == Weeks.Second) { while (from.Day < 15 && from.Day > 7) { if (Utils.HasWeekday(_weekdays, Utils.GetWeekday(from.DayOfWeek)) && from != original) { return(from); } from = from.AddDays(1); } } //Third week if ((_weeks & Weeks.Third) == Weeks.Third) { while (from.Day < 22 && from.Day > 14) { if (Utils.HasWeekday(_weekdays, Utils.GetWeekday(from.DayOfWeek)) && from != original) { return(from); } from = from.AddDays(1); } } //Both Fourth & Last week if ((_weeks & (Weeks.Fourth | Weeks.Last)) == (Weeks.Fourth | Weeks.Last)) { while (from.Day > 21) { if (Utils.HasWeekday(_weekdays, Utils.GetWeekday(from.DayOfWeek)) && from != original) { return(from); } from = from.AddDays(1); } } else { //Fourth week if ((_weeks & Weeks.Fourth) == Weeks.Fourth) { while (from.Day < 29 && from.Day > 21) { if (Utils.HasWeekday(_weekdays, Utils.GetWeekday(from.DayOfWeek)) && from != original) { return(from); } from = from.AddDays(1); } } //Last if ((_weeks & Weeks.Last) == Weeks.Last) { int lastWeekDay = GetLastWeekStartDay(from); while (from.Day >= lastWeekDay) { if (Utils.HasWeekday(_weekdays, Utils.GetWeekday(from.DayOfWeek)) && from != original) { return(from); } from = from.AddDays(1); } } } from = from.AddDays(1); } }