Exemple #1
0
        public static CrontabEntry Create(ScheduleEntry scheduleEntry)
        {
            CrontabEntry ceb = new CrontabEntry();

            // Initialize the flag arrays
            ceb.SecondFlags = new bool[60];
            ceb.MinuteFlags = new bool[60];
            ceb.HourFlags = new bool[24];
            ceb.MonthFlags = new bool[12];
            ceb.DayOfWeekFlags = new bool[7];
            ceb.DayOfMonthFlags = new bool[31];
            ceb.WeekSequence = Sequence.Undefined;
            ceb.MonthSequence = Sequence.Undefined;
            ceb.YearFlags = new bool[2500];

            ParseToken(scheduleEntry.Seconds, ceb.SecondFlags, false);
            ParseToken(scheduleEntry.Minutes, ceb.MinuteFlags, false);
            ParseToken(scheduleEntry.Hours, ceb.HourFlags, false);
            ParseToken(scheduleEntry.Months, ceb.MonthFlags, true);

            Sequence sequence;

            ParseToken(scheduleEntry.DaysOfWeek, ceb.DayOfWeekFlags, false, out sequence);
            ceb.WeekSequence = sequence;

            ParseToken(scheduleEntry.DaysOfMonth, ceb.DayOfMonthFlags, true, out sequence);
            ceb.MonthSequence = sequence;

            ParseToken(scheduleEntry.Years, ceb.YearFlags, false);

            ceb.Tag = scheduleEntry.Tag;

            return ceb;
        }
        //-----------------------------------------------------------------------------------------
        /// <summary>
        /// Builds a Date from a CrontabEntry and from a starting Date 
        /// </summary>
        /// <param name="ceb"></param>
        /// <param name="afterDate"></param>
        /// <returns></returns>
        public ScheduleTime Next(CrontabEntry ceb, DateTime afterDate)
        {
            Calendar cal = CultureInfo.InvariantCulture.Calendar;
            DateTime after = new DateTime(afterDate.Ticks);

            int second = GetNextIndex(ceb.SecondFlags, after.Second);
            if (second == -1)
            {
                second = GetNextIndex(ceb.SecondFlags, 0);
                after = cal.AddMinutes(after, 1);
            }

            int minute = GetNextIndex(ceb.MinuteFlags, after.Minute);
            if (minute == -1)
            {
                second = GetNextIndex(ceb.SecondFlags, 0);
                minute = GetNextIndex(ceb.MinuteFlags, 0);
                after = cal.AddHours(after, 1);
            }

            int hour = GetNextIndex(ceb.HourFlags, after.Hour);
            if (hour == -1)
            {
                second = GetNextIndex(ceb.SecondFlags, 0);
                minute = GetNextIndex(ceb.MinuteFlags, 0);
                hour = GetNextIndex(ceb.HourFlags, 0);
                after = cal.AddDays(after, 1);
            }

            int dayOfMonthIndex = GetNextIndex(ceb.DayOfMonthFlags, after.Day - 1);
            if (dayOfMonthIndex == -1)
            {
                second = GetNextIndex(ceb.SecondFlags, 0);
                minute = GetNextIndex(ceb.MinuteFlags, 0);
                hour = GetNextIndex(ceb.HourFlags, 0);

                switch (ceb.MonthSequence)
                {
                    // NOTE: day is numbered from 0 to (N-1)

                    case Sequence.First:
                        dayOfMonthIndex = 0;
                        break;
                    case Sequence.Second:
                        dayOfMonthIndex = 1;
                        break;
                    case Sequence.Third:
                        dayOfMonthIndex = 2;
                        break;
                    case Sequence.Fourth:
                        dayOfMonthIndex = 3;
                        break;
                    case Sequence.Last:
                        dayOfMonthIndex = DateTime.DaysInMonth(after.Year, after.Month) - 1;
                        break;
                    default:
                        dayOfMonthIndex = GetNextIndex(ceb.DayOfMonthFlags, 0);
                        after = cal.AddMonths(after, 1);
                        break;
                }
            }

            bool dayMatchRealDate = false;
            while (!dayMatchRealDate)
            {
                if (CheckDayValidInMonth(dayOfMonthIndex + 1, after.Month, after.Year))
                {
                    dayMatchRealDate = true;
                }
                else
                {
                    after = cal.AddMonths(after, 1);
                }
            }

            int monthIndex = GetNextIndex(ceb.MonthFlags, after.Month - 1);
            if (monthIndex == -1)
            {
                second = GetNextIndex(ceb.SecondFlags, 0);
                minute = GetNextIndex(ceb.MinuteFlags, 0);
                hour = GetNextIndex(ceb.HourFlags, 0);
                dayOfMonthIndex = GetNextIndex(ceb.DayOfMonthFlags, 0);
                monthIndex = GetNextIndex(ceb.MonthFlags, 0);
                after = cal.AddYears(after, 1);
            }

            int year = GetNextIndex(ceb.YearFlags, after.Year);
            if (year == -1)
            {
                second = GetNextIndex(ceb.SecondFlags, 0);
                minute = GetNextIndex(ceb.MinuteFlags, 0);
                hour = GetNextIndex(ceb.HourFlags, 0);
                dayOfMonthIndex = GetNextIndex(ceb.DayOfMonthFlags, 0);
                monthIndex = GetNextIndex(ceb.MonthFlags, 0);
                year = GetNextIndex(ceb.YearFlags, 0);
            }

            DateTime byMonthDays = GetTime(second, minute, hour, dayOfMonthIndex + 1, monthIndex + 1, year);
            DateTime calendar = new DateTime(byMonthDays.Ticks);

            bool[] bDaysOfWeek = ceb.DayOfWeekFlags;
            int dow = (int)calendar.DayOfWeek;

            if (bDaysOfWeek[dow])
            {
                if (ceb.WeekSequence == Sequence.Undefined)
                {
                    return new ScheduleTime(ceb, calendar);
                }
                else
                {
                    if (ceb.WeekSequence != Sequence.Last)
                        throw new NotImplementedException("Implemenation missing for WeekSequence");

                    int remainingDaysOfMonth = DateTime.DaysInMonth(calendar.Year, calendar.Month) - calendar.Day;
                    if (remainingDaysOfMonth <= 6)
                    {
                        return new ScheduleTime(ceb, calendar);
                    }
                    else
                    {
                        calendar = calendar.AddDays(remainingDaysOfMonth - 6);
                        return Next(ceb, calendar);
                    }
                }
            }
            else
            {
                calendar = calendar.AddDays(1);
                return Next(ceb, calendar);
            }
        }
 ///
 /// This method builds a Date from a CrontabEntry. launching the same 
 /// method with now as parameter
 ///
 //-----------------------------------------------------------------------------------------
 /// <summary>
 /// This method builds a Date from a CrontabEntry. launching the same 
 /// method with now as parameter.
 /// </summary>
 /// <param name="ceb"></param>
 /// <returns></returns>
 public ScheduleTime Next(CrontabEntry ceb)
 {
     DateTime now = DateTime.Now;
     return Next(ceb, now);
 }
Exemple #4
0
 internal ScheduleTime(CrontabEntry entry, DateTime date)
 {
     this.entry = entry;
     this.next = date;
 }