コード例 #1
0
        public DateTime GetNextRecurrence(DateTime baseTime)
        {
            DateTime recurrence = baseTime;

            if (DaysOfMonth != null)
            {
                var findDay = (from d in DaysOfMonth where d > recurrence.Day select d).FirstOrDefault();
                if (findDay == 0)
                {
                    recurrence = recurrence.AddMonths(1);
                    findDay    = (from d in DaysOfMonth where d > recurrence.Day select d).FirstOrDefault();
                }

                if (findDay == 0)
                {
                    var err = string.Format("Cannot find day of month recurrence for recurrence: {0}", ToString());
                    _log.Error(err);
                    throw new ArgumentOutOfRangeException(err);
                }

                if (findDay > DateTime.DaysInMonth(baseTime.Year, baseTime.Month))
                {
                    findDay = DateTime.DaysInMonth(baseTime.Year, baseTime.Month);
                }
                recurrence = new DateTime(recurrence.Year, recurrence.Month, findDay, recurrence.Hour, recurrence.Minute,
                                          recurrence.Second);
            }
            else if (DaysOfWeek != null)
            {
                if (!DaysOfWeek.All(d => d >= (int)DayOfWeek.Sunday && d <= (int)DayOfWeek.Saturday))
                {
                    var err = string.Format("Cannot find day of week recurrence for recurrence {0}", ToString());
                    _log.Error(err);
                    throw new ArgumentOutOfRangeException(err);
                }

                while (!DaysOfWeek.Contains((int)recurrence.DayOfWeek))
                {
                    recurrence = recurrence.AddDays(1.0);
                }
            }
            else
            {
                if (MonthsLater > 0)
                {
                    recurrence = recurrence.AddMonths(MonthsLater);
                }
                if (DaysLater > 0)
                {
                    recurrence = recurrence.AddDays(DaysLater);
                }
                if (HoursLater > 0)
                {
                    recurrence = recurrence.AddHours(HoursLater);
                }
                if (MinutesLater > 0)
                {
                    recurrence = recurrence.AddMinutes(MinutesLater);
                }
            }

            if (AtFixedTime != DateTime.MinValue)
            {
                recurrence = new DateTime(recurrence.Year, recurrence.Month, recurrence.Day, AtFixedTime.Hour,
                                          AtFixedTime.Minute, AtFixedTime.Second);
                if (recurrence < DateTime.UtcNow)
                {
                    recurrence = recurrence.AddDays(1.0);
                }
            }

            _dblog.InfoFormat("Recurrence {0} from base time {1} next {2}", ToString(), baseTime, recurrence);
            return(recurrence);
        }