Exemple #1
0
 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));
 }
Exemple #2
0
        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);
        }
Exemple #3
0
        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);
        }
Exemple #4
0
        public bool CheckValidDate(Date_Time dt)
        {
            if (BySecond.Count != 0 && !BySecond.Contains(dt.Value.Second))
            {
                return(false);
            }
            if (ByMinute.Count != 0 && !ByMinute.Contains(dt.Value.Minute))
            {
                return(false);
            }
            if (ByHour.Count != 0 && !ByHour.Contains(dt.Value.Hour))
            {
                return(false);
            }
            if (ByDay.Count != 0)
            {
                bool found = false;
                foreach (DaySpecifier bd in ByDay)
                {
                    if (bd.CheckValidDate(this, dt))
                    {
                        found = true;
                        break;
                    }
                }
                if (!found)
                {
                    return(false);
                }
            }
            if (ByWeekNo.Count != 0)
            {
                bool found          = false;
                int  lastWeekOfYear = m_Calendar.GetWeekOfYear(new DateTime(dt.Value.Year, 12, 31), System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst);
                int  currWeekNo     = m_Calendar.GetWeekOfYear(dt.Value, System.Globalization.CalendarWeekRule.FirstFourDayWeek, Wkst);
                foreach (int WeekNo in ByWeekNo)
                {
                    if ((WeekNo > 0 && WeekNo == currWeekNo) ||
                        (WeekNo < 0 && lastWeekOfYear + WeekNo + 1 == currWeekNo))
                    {
                        found = true;
                    }
                }
                if (!found)
                {
                    return(false);
                }
            }
            if (ByMonth.Count != 0 && !ByMonth.Contains(dt.Value.Month))
            {
                return(false);
            }
            if (ByMonthDay.Count != 0)
            {
                // Handle negative days of month (count backwards from the end)
                // NOTE: fixes RRULE18 eval
                bool found       = false;
                int  DaysInMonth = m_Calendar.GetDaysInMonth(dt.Value.Year, dt.Value.Month);
                foreach (int Day in ByMonthDay)
                {
                    if ((Day > 0) && (Day == dt.Value.Day))
                    {
                        found = true;
                    }
                    else if ((Day < 0) && (DaysInMonth + Day + 1 == dt.Value.Day))
                    {
                        found = true;
                    }
                }

                if (!found)
                {
                    return(false);
                }
            }
            if (ByYearDay.Count != 0)
            {
                // Handle negative days of year (count backwards from the end)
                // NOTE: fixes RRULE25 eval
                bool     found      = false;
                int      DaysInYear = m_Calendar.GetDaysInYear(dt.Value.Year);
                DateTime baseDate   = new DateTime(dt.Value.Year, 1, 1);

                foreach (int Day in ByYearDay)
                {
                    if (Day > 0 && dt.Value.Date == baseDate.AddDays(Day - 1))
                    {
                        found = true;
                    }
                    else if (Day < 0 && dt.Value.Date == baseDate.AddYears(1).AddDays(Day))
                    {
                        found = true;
                    }
                }
                if (!found)
                {
                    return(false);
                }
            }
            return(true);
        }
Exemple #5
0
        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);
        }