private bool CheckDate(DateTime d) { if (ByMonth != null && !ByMonth.Contains(d.Month)) { return(false); } //only for YEARLY if (ByWeekNo != null) { var weekOfYear = d.GetWeekOfYear(this.WKST.DayOfWeek); if (!ByWeekNo.Contains(weekOfYear) && !ByWeekNo.Contains(weekOfYear - (d.GetWeekOfYearCount(this.WKST.DayOfWeek) + 1))) { return(false); } } if (ByYearDay != null && !ByYearDay.Contains(d.DayOfYear) && !ByYearDay.Contains(d.DayOfYear - (d.GetDaysInYear() + 1))) { return(false); } if (ByMonthDay != null && !ByMonthDay.Contains(d.Day) && !ByMonthDay.Contains(d.Day - d.GetDaysInMonth() + 1)) { return(false); } if (ByDay != null && !ByDay.ToList().Exists(item => item.DayOfWeek == d.DayOfWeek)) { return(false); } return(true); }
public override bool Equals(object obj) { if (obj is Recur) { Recur r = (Recur)obj; if (!ArrayEquals(r.ByDay.ToArray(), ByDay.ToArray()) || !ArrayEquals(r.ByHour.ToArray(), ByHour.ToArray()) || !ArrayEquals(r.ByMinute.ToArray(), ByMinute.ToArray()) || !ArrayEquals(r.ByMonth.ToArray(), ByMonth.ToArray()) || !ArrayEquals(r.ByMonthDay.ToArray(), ByMonthDay.ToArray()) || !ArrayEquals(r.BySecond.ToArray(), BySecond.ToArray()) || !ArrayEquals(r.BySetPos.ToArray(), BySetPos.ToArray()) || !ArrayEquals(r.ByWeekNo.ToArray(), ByWeekNo.ToArray()) || !ArrayEquals(r.ByYearDay.ToArray(), ByYearDay.ToArray())) { return(false); } if (r.Count != Count) { return(false); } if (r.Frequency != Frequency) { return(false); } if (r.Interval != Interval && // MinValue and 1 are treated as identical for Interval ((r.Interval != int.MinValue && r.Interval != 1) || (Interval != int.MinValue && Interval != 1))) { return(false); } if (r.Until != null) { if (!r.Until.Equals(Until)) { return(false); } } else if (Until != null) { return(false); } if (r.Wkst != Wkst) { return(false); } return(true); } return(base.Equals(obj)); }
//ToDo: This needs a memberwise hash code implementation public override int GetHashCode() { var hashCode = ByDay.GetHashCode() ^ ByHour.GetHashCode() ^ ByMinute.GetHashCode() ^ ByMonth.GetHashCode() ^ ByMonthDay.GetHashCode() ^ BySecond.GetHashCode() ^ BySetPosition.GetHashCode() ^ ByWeekNo.GetHashCode() ^ ByYearDay.GetHashCode() ^ Count.GetHashCode() ^ Frequency.GetHashCode(); if (Interval.Equals(1)) { hashCode ^= 0x1; } else { hashCode ^= Interval.GetHashCode(); } hashCode ^= Until.GetHashCode(); hashCode ^= FirstDayOfWeek.GetHashCode(); return(hashCode); }
public override int GetHashCode() { int hashCode = ByDay.GetHashCode() ^ ByHour.GetHashCode() ^ ByMinute.GetHashCode() ^ ByMonth.GetHashCode() ^ ByMonthDay.GetHashCode() ^ BySecond.GetHashCode() ^ BySetPos.GetHashCode() ^ ByWeekNo.GetHashCode() ^ ByYearDay.GetHashCode() ^ Count.GetHashCode() ^ Frequency.GetHashCode(); if (Interval.Equals(1) || Interval.Equals(int.MinValue)) { hashCode ^= 0x1; } else { hashCode ^= Interval.GetHashCode(); } hashCode ^= Until.GetHashCode(); hashCode ^= Wkst.GetHashCode(); return(hashCode); }
public List <DateTime> GetDates(DateTime utcStartDate, DateTime fromDate, DateTime toDate, int maxCount, bool removeExDates = true) { var dates = new List <DateTime>(); var endDate = (this.Until == DateTime.MinValue ? toDate : (toDate > this.Until ? this.Until : toDate)); //push start date dates.Add(utcStartDate); DateTime d; switch (Freq) { case Frequency.Secondly: #region Secondly d = utcStartDate.AddSeconds(this.Interval); while (d <= endDate && CheckCount(dates, fromDate, maxCount)) { if (CheckDate(d) && (ByHour == null || (ByHour != null && ByHour.Contains(d.Hour))) && (ByMinute == null || (ByMinute != null && ByMinute.Contains(d.Minute))) && (BySecond == null || (BySecond != null && BySecond.Contains(d.Second)))) { if (d >= utcStartDate && d <= endDate && !dates.Contains(d)) { dates.Add(d); } } d = d.AddMinutes(this.Interval); } break; #endregion case Frequency.Minutely: #region Minutely d = utcStartDate.AddMinutes(this.Interval); while (d <= endDate && CheckCount(dates, fromDate, maxCount)) { if (CheckDate(d) && (ByHour == null || (ByHour != null && ByHour.Contains(d.Hour))) && (ByMinute == null || (ByMinute != null && ByMinute.Contains(d.Minute)))) { //seconds var seconds = new List <int>(); if (BySecond != null) { seconds.AddRange(BySecond); } else { seconds.Add(d.Second); } foreach (var s in seconds) { var newDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, d.Minute, s); if (newDate >= utcStartDate && newDate <= endDate && !dates.Contains(newDate)) { dates.Add(newDate); } } } d = d.AddMinutes(this.Interval); } break; #endregion case Frequency.Hourly: #region Hourly d = utcStartDate.AddHours(this.Interval); while (d <= endDate && CheckCount(dates, fromDate, maxCount)) { if (CheckDate(d) && (ByHour == null || (ByHour != null && ByHour.Contains(d.Hour)))) { //minutes var minutes = new List <int>(); if (ByMinute != null) { minutes.AddRange(ByMinute); } else { minutes.Add(d.Minute); } //seconds var seconds = new List <int>(); if (BySecond != null) { seconds.AddRange(BySecond); } else { seconds.Add(d.Second); } foreach (var m in minutes) { foreach (var s in seconds) { var newDate = new DateTime(d.Year, d.Month, d.Day, d.Hour, m, s); if (newDate >= utcStartDate && newDate <= endDate && !dates.Contains(newDate)) { dates.Add(newDate); } } } } d = d.AddHours(this.Interval); } break; #endregion case Frequency.Daily: #region Daily d = utcStartDate.AddDays(this.Interval); while (d <= endDate && CheckCount(dates, fromDate, maxCount)) { if (CheckDate(d)) { GetDatesWithTime(ref dates, utcStartDate, endDate, d, new List <DateTime>() { d }); } d = d.AddDays(this.Interval); } break; #endregion case Frequency.Weekly: #region Weekly d = utcStartDate; while (d <= endDate && CheckCount(dates, fromDate, maxCount)) { var dateRange = new List <DateTime>(); for (var i = 0; i < 7; i++) { dateRange.Add(d.AddDays(i)); } if (ByMonth != null) { dateRange.RemoveAll(date => !ByMonth.Contains(date.Month)); } if (ByYearDay != null) { dateRange.RemoveAll(date => (!ByYearDay.Contains(date.DayOfYear) && !ByYearDay.Contains(date.DayOfYear - (date.GetDaysInYear() + 1)))); } if (ByMonthDay != null) { dateRange.RemoveAll(date => (!ByMonthDay.Contains(date.Day) && !ByMonthDay.Contains(date.Day - (date.GetDaysInMonth() + 1)))); } if (ByDay != null) { dateRange.RemoveAll(date => !ByDay.ToList().Exists(wd => wd.DayOfWeek == date.DayOfWeek)); } if (ByDay == null && ByMonthDay == null && ByYearDay == null) { dateRange.RemoveAll(date => date.Day != d.Day); } GetDatesWithTime(ref dates, utcStartDate, endDate, d, dateRange); d = d.AddDays(7 * this.Interval); } break; #endregion case Frequency.Monthly: #region Monthly d = utcStartDate; while (d <= endDate && CheckCount(dates, fromDate, maxCount)) { var dateRange = new List <DateTime>(); if (ByMonth != null && !ByMonth.Contains(d.Month)) { d = d.AddMonths(this.Interval); continue; } var day = new DateTime(d.Year, d.Month, 1); while (day.Month == d.Month) { dateRange.Add(day); day = day.AddDays(1); } if (ByYearDay != null) { dateRange.RemoveAll(date => (!ByYearDay.Contains(date.DayOfYear) && !ByYearDay.Contains(date.DayOfYear - (date.GetDaysInYear() + 1)))); } if (ByMonthDay != null) { dateRange.RemoveAll(date => (!ByMonthDay.Contains(date.Day) && !ByMonthDay.Contains(date.Day - (date.GetDaysInMonth() + 1)))); } //only for MONTHLY or YEARLY if (ByDay != null) { var listDates = new List <DateTime>(); foreach (var date in ByDay) { listDates.AddRange(date.GetDates(new DateTime(d.Year, d.Month, 1), true)); } dateRange.RemoveAll(date => !listDates.Contains(date)); } if (ByDay == null && ByMonthDay == null && ByYearDay == null) { dateRange.RemoveAll(date => date.Day != d.Day); } GetDatesWithTime(ref dates, utcStartDate, endDate, d, dateRange); d = d.AddMonths(this.Interval); } break; #endregion case Frequency.Yearly: #region Yearly d = utcStartDate; if (d.Month == 2 && d.Day == 29) { if (Interval == 1 && ByMonth == null && ByWeekNo == null && ByYearDay == null && ByMonthDay == null && ByDay == null) { Interval = 4; } } while (d.Year <= endDate.Year && CheckCount(dates, fromDate, maxCount)) { var dateRange = new List <DateTime>(); bool isFirst = true; if (ByMonth != null) { foreach (var m in ByMonth) { var date = new DateTime(d.Year, m, 1); while (date.Month == m) { dateRange.Add(date); date = date.AddDays(1); } } isFirst = false; } //only for YEARLY if (ByWeekNo != null) { if (isFirst) { var date = new DateTime(d.Year, 1, 1); while (date.Year == d.Year) { var weekOfYear = date.GetWeekOfYear(this.WKST.DayOfWeek); if (ByWeekNo.Contains(weekOfYear) || ByWeekNo.Contains(weekOfYear - (date.GetWeekOfYearCount(this.WKST.DayOfWeek) + 1))) { dateRange.Add(date); } date = date.AddDays(1); } } else { dateRange.RemoveAll(date => { var weekOfYear = date.GetWeekOfYear(this.WKST.DayOfWeek); return((!ByWeekNo.Contains(weekOfYear) && !ByWeekNo.Contains(weekOfYear - (date.GetWeekOfYearCount(this.WKST.DayOfWeek) + 1)))); }); } isFirst = false; } if (ByYearDay != null) { if (isFirst) { foreach (var yearDay in ByYearDay) { dateRange.Add(new DateTime(d.Year, 1, 1).AddDays((yearDay > 0 ? yearDay : (d.GetDaysInYear() + yearDay)) - 1)); } } else { dateRange.RemoveAll(date => (!ByYearDay.Contains(date.DayOfYear) && !ByYearDay.Contains(date.DayOfYear - (date.GetDaysInYear() + 1)))); } isFirst = false; } if (ByMonthDay != null) { if (isFirst) { for (var m = 1; m <= 12; m++) { foreach (var day in ByMonthDay) { var dd = new DateTime(d.Year, m, 1); dateRange.Add(dd.AddDays((day > 0 ? day : (dd.GetDaysInMonth() + day)) - 1)); } } } else { dateRange.RemoveAll(date => (!ByMonthDay.Contains(date.Day) && !ByMonthDay.Contains(date.Day - (date.GetDaysInMonth() + 1)))); } isFirst = false; } //only for MONTHLY or YEARLY if (ByDay != null) { var listDates = new List <DateTime>(); foreach (var day in ByDay) { listDates.AddRange(day.GetDates(new DateTime(d.Year, 1, 1), false)); } listDates.Sort(); if (isFirst) { dateRange.AddRange(listDates); } else { dateRange.RemoveAll(date => !listDates.Contains(date)); } isFirst = false; } if (ByDay == null && ByMonthDay == null && ByYearDay == null && ByWeekNo == null) { dateRange.RemoveAll(date => date.Day != d.Day); } //add yearly same date if (isFirst) { dateRange.Add(d); } GetDatesWithTime(ref dates, utcStartDate, endDate, d, dateRange); d = d.AddYears(this.Interval); } break; #endregion } if (Count >= 0) { var count = this.Count; dates = dates.FindAll(date => (--count) >= 0); } if (removeExDates && ExDates != null) { foreach (var exDate in ExDates) { dates.RemoveAll(dt => (exDate.isDateTime && dt == exDate.Date) || (!exDate.isDateTime && dt.Date == exDate.Date)); } } dates.RemoveAll(dt => dt <fromDate || dt> endDate); return(dates); }