private static IEnumerable <DateTime> GetOccurrences(DateInterval interval, DatePeriod period, Calendar calendar, DateTime start, DateTime end, bool skipWeekends = true) { var difference = DateSpan.GetDifference(interval, start, end) / period.Quantifier; if (start.Kind == DateTimeKind.Utc) { start = start.ToLocalTime(); } for (var i = 0; i < difference; i++) { switch (period.Frequency) { case DatePeriodFrequency.Seconds: var seconds = calendar.AddSeconds(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, seconds, skipWeekends)); break; case DatePeriodFrequency.Minutes: var minutes = calendar.AddMinutes(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, minutes, skipWeekends)); break; case DatePeriodFrequency.Hours: var hours = calendar.AddHours(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, hours, skipWeekends)); break; case DatePeriodFrequency.Days: var days = calendar.AddDays(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, days, skipWeekends)); break; case DatePeriodFrequency.Weeks: var weeks = calendar.AddWeeks(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, weeks, skipWeekends)); break; case DatePeriodFrequency.Months: var months = calendar.AddMonths(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, months, skipWeekends)); break; case DatePeriodFrequency.Years: var years = calendar.AddYears(start, period.Quantifier * i); yield return(DeferOccurrenceFallingOnWeekend(calendar, years, skipWeekends)); break; default: throw new ArgumentException("Frequency"); } } }
private IEnumerable<DateTime> GetSeriesOccurrences(DatePeriod? endPeriod = null) { endPeriod = endPeriod.HasValue ? endPeriod.Value : EndPeriod.HasValue ? EndPeriod.Value : (DatePeriod?)null; if (!endPeriod.HasValue) { throw new ArgumentException("You cannot request the occurrences for an infinite series"); } var cacheKey = string.Format( "frequency:{0}_quantifier:{1}_end:{2}_start:{3}_skip:{4}", Period.Frequency, Period.Quantifier, EndPeriod.HasValue ? string.Format("{0}_{1}", EndPeriod.Value.Frequency, EndPeriod.Value.Quantifier) : null, Start, !IncludeWeekends ); IEnumerable<DateTime> occurrences; if(Cache.TryGetValue(cacheKey, out occurrences)) { return occurrences; } var start = Start.HasValue ? Start.Value : DateTime.Now; DateTime end; // Get the last occurrence switch (endPeriod.Value.Frequency) { case DatePeriodFrequency.Years: end = start.AddYears(endPeriod.Value.Quantifier); break; case DatePeriodFrequency.Weeks: end = start.AddDays(endPeriod.Value.Quantifier * 7); break; case DatePeriodFrequency.Days: end = start.AddDays(endPeriod.Value.Quantifier); break; case DatePeriodFrequency.Hours: end = start.AddHours(endPeriod.Value.Quantifier); break; case DatePeriodFrequency.Minutes: end = start.AddHours(endPeriod.Value.Quantifier); break; case DatePeriodFrequency.Seconds: end = start.AddSeconds(endPeriod.Value.Quantifier); break; case DatePeriodFrequency.Months: end = start.AddMonths(endPeriod.Value.Quantifier); break; default: throw new ArgumentException("DatePeriodFrequency"); } occurrences = Period.GetOccurrences(start, end, !IncludeWeekends).ToList(); Cache.Add(cacheKey, occurrences); return occurrences; }
internal RepeatInfo(DateTime? start, DatePeriod period) { Start = start; Period = period; }
private static IEnumerable <DateTimeOffset> GetOccurrences(DateInterval interval, DatePeriod period, Calendar calendar, DateTimeOffset start, DateTimeOffset end, bool skipWeekends = true) { return(GetOccurrences(interval, period, calendar, start.DateTime, end.DateTime, skipWeekends).Select(occurrence => (DateTimeOffset)occurrence)); }
private static IEnumerable<DateTime> GetOccurrences(DateInterval interval, DatePeriod period, Calendar calendar, DateTime start, DateTime end, bool skipWeekends = true) { var difference = DateSpan.GetDifference(interval, start, end)/period.Quantifier; if (start.Kind == DateTimeKind.Utc) { start = start.ToLocalTime(); } for (var i = 0; i < difference; i++) { switch (period.Frequency) { case DatePeriodFrequency.Seconds: var seconds = calendar.AddSeconds(start, period.Quantifier * i); yield return DeferOccurrenceFallingOnWeekend(calendar, seconds, skipWeekends); break; case DatePeriodFrequency.Minutes: var minutes = calendar.AddMinutes(start, period.Quantifier * i); yield return DeferOccurrenceFallingOnWeekend(calendar, minutes, skipWeekends); break; case DatePeriodFrequency.Hours: var hours = calendar.AddHours(start, period.Quantifier * i); yield return DeferOccurrenceFallingOnWeekend(calendar, hours, skipWeekends); break; case DatePeriodFrequency.Days: var days = calendar.AddDays(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, days, skipWeekends); break; case DatePeriodFrequency.Weeks: var weeks = calendar.AddWeeks(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, weeks, skipWeekends); break; case DatePeriodFrequency.Months: var months = calendar.AddMonths(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, months, skipWeekends); break; case DatePeriodFrequency.Years: var years = calendar.AddYears(start, period.Quantifier*i); yield return DeferOccurrenceFallingOnWeekend(calendar, years, skipWeekends); break; default: throw new ArgumentException("Frequency"); } } }