コード例 #1
0
        public void TestRecurrence_Yearly_NthWeekDayOfMonth_First()
        {
            Console.WriteLine("TestRecurrence_Yearly_NthWeekDayOfMonth");

            // event occurs yearly every first Thursday of February

            ManagedAccount a = new ManagedAccount(Session);

            try
            {
                a.Create("Test User", "testpassword", "*****@*****.**", DateTime.UtcNow, AdminSecurityContext);

                TransitSchedule ts = new TransitSchedule();

                ts.AccountId         = a.Id;
                ts.StartDateTime     = DateTime.UtcNow;
                ts.RecurrencePattern = RecurrencePattern.Yearly_NthWeekDayOfMonth;
                ts.YearlyExDayIndex  = (short)DayIndex.first;
                ts.YearlyExDayName   = (short)DayName.Thursday;
                ts.YearlyExMonth     = (short)MonthName.February;
                ts.Endless           = true;
                ts.EndDateTime       = DateTime.UtcNow.AddHours(1);

                ManagedSchedule m_s         = new ManagedSchedule(Session);
                int             schedule_id = m_s.CreateOrUpdate(ts, AdminSecurityContext);

                Schedule s = Session.Load <Schedule>(schedule_id);

                Assert.IsNotNull(s.ScheduleInstances, "Schedule instances cannot be null.");

                foreach (ScheduleInstance ts_instance in s.ScheduleInstances)
                {
                    Console.WriteLine(string.Format("Event on {0}.", ts_instance.StartDateTime.ToLongDateString()));
                    Assert.AreEqual((int)ts_instance.StartDateTime.DayOfWeek, ts.YearlyExDayName, "Day of week is wrong.");
                    Assert.AreEqual(ts_instance.StartDateTime.Month, ts.YearlyExMonth, "Month is wrong.");
                    Assert.IsTrue(CBusinessDay.GetDayOfWeekOccurrenceThisMonth(ts_instance.StartDateTime) == 1, "Day of month is not the first instance.");
                }
            }
            finally
            {
                a.Delete(AdminSecurityContext);
            }
        }
コード例 #2
0
ファイル: ManagedSchedule.cs プロジェクト: qwdf1615/sncore
        public DateTime GetNextRecurrence(DateTime current)
        {
            switch (RecurrencePattern)
            {
            // every n days, simply adds number of days
            case RecurrencePattern.Daily_EveryNDays:
            {
                current = current.AddDays(mInstance.DailyEveryNDays);
                break;
            }

            // every week-day skips week-end days
            case RecurrencePattern.Daily_EveryWeekday:
            {
                do
                {
                    current = current.AddDays(1);
                }while (current.DayOfWeek == DayOfWeek.Saturday || current.DayOfWeek == DayOfWeek.Sunday);
                break;
            }

            // weekly occurs on certain days only
            case RecurrencePattern.Weekly:
            {
                // if doesn't ever occur (should be disallowed by UI)
                if (mInstance.WeeklyDaysOfWeek == 0)
                {
                    throw new Exception("No days of week selected for a weekly schedule.");
                }
                else
                {
                    do
                    {
                        current = current.AddDays(1);
                    } while (((short)Math.Pow(2, (short)current.DayOfWeek) & mInstance.WeeklyDaysOfWeek) == 0);
                }

                break;
            }

            // occurs on a certain day of every N months
            case RecurrencePattern.Monthly_DayNOfEveryNMonths:
            {
                int skipped = 0;
                do
                {
                    do
                    {
                        current = current.AddDays(1);
                    }while (current.Day != mInstance.MonthlyDay);

                    skipped++;
                }while (mInstance.MonthlyMonth != 0 && skipped != mInstance.MonthlyMonth);
                break;
            }

            // the ((DayIndex)mInstance.MonthlyExDayIndex)
            // (DayName)mInstance.MonthlyExDayName
            // of every mInstance.MonthlyExMonth month(s)
            case RecurrencePattern.Monthly_NthWeekDayOfEveryNMonth:
            {
                int skipped_matchingmonth = 0;
                do
                {
                    while (true)
                    {
                        current = current.AddDays(1);

                        // is this a matching day name
                        if (
                            mInstance.MonthlyExDayName == (int)current.DayOfWeek ||
                            mInstance.MonthlyExDayName == (short)DayName.day ||
                            (mInstance.MonthlyExDayName == (short)DayName.weekday &&
                             current.DayOfWeek != DayOfWeek.Sunday &&
                             current.DayOfWeek != DayOfWeek.Saturday) ||
                            (mInstance.MonthlyExDayName == (short)DayName.weekendday &&
                             (current.DayOfWeek == DayOfWeek.Sunday ||
                              current.DayOfWeek == DayOfWeek.Saturday))
                            )
                        {
                            // is it the last occurence this month?
                            if (mInstance.MonthlyExDayIndex == (short)DayIndex.last &&
                                CBusinessDay.IsLastDayOfWeekOccurrenceThisMonth(current))
                            {
                                break;
                            }

                            // which occurrence is it this month?
                            if (CBusinessDay.GetDayOfWeekOccurrenceThisMonth(current) == mInstance.MonthlyExDayIndex)
                            {
                                break;
                            }
                        }
                    }

                    skipped_matchingmonth++;
                }while (mInstance.MonthlyExMonth != 0 && skipped_matchingmonth != mInstance.MonthlyExMonth);
                break;
            }

            // yearly every ((MonthName)mInstance.YearlyMonth) YearlyDay
            case RecurrencePattern.Yearly_DayNOfMonth:
            {
                current = current.AddDays(1);         // skip the current instance

                // instance this year
                DateTime result = current.AddYears(-1);

                do
                {
                    // get the instance next year
                    result = new DateTime(result.Year + 1, mInstance.YearlyMonth, mInstance.YearlyDay)
                             .Add(current.TimeOfDay);
                } while (current > result);

                current = result;
                break;
            }

            // yearly the ((DayIndex)mInstance.YearlyExDayIndex) ((DayName)mInstance.YearlyExDayName) of ((MonthName)mInstance.YearlyExMonth)
            case RecurrencePattern.Yearly_NthWeekDayOfMonth:
            {
                current = current.AddDays(1);         // skip the current instance

                // start a year ago, gets incremented in the first loop
                DateTime result = current.AddYears(-1);

                do
                {
                    // get the instance next year
                    result = new DateTime(result.Year + 1, mInstance.YearlyExMonth, 1)
                             .Add(current.TimeOfDay);

                    while (true)
                    {
                        if ((short)result.DayOfWeek == mInstance.YearlyExDayName)
                        {
                            // is it the last occurence this month?
                            if (mInstance.YearlyExDayIndex == (short)DayIndex.last &&
                                CBusinessDay.IsLastDayOfWeekOccurrenceThisMonth(result))
                            {
                                break;
                            }

                            // which occurrence is it this month?
                            if (CBusinessDay.GetDayOfWeekOccurrenceThisMonth(result) == mInstance.YearlyExDayIndex)
                            {
                                break;
                            }
                        }

                        result = result.AddDays(1);
                    }
                } while (current > result);

                current = result;
                break;
            }
            }

            return(current);
        }