BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Algorithms and Data Structures
.NET 3.5+

An Extensible Appointment Scheduling Library

Scheduling software can be used to plan future appointments, including those appointments that repeat on a regular basis. This article describes a library with four types of scheduling rule and the potential for additional rules to be incorporated.

MonthlySchedule Class

The final scheduling class is MonthlySchedule, which generates an appointment each month on the same date. The DayOfMonth property holds the day number within the month for appointments. When checking if a date should have an appointment, the date's day number is compared with the DayOfMonth property. If they match, OccursOnDate returns true. If you set the DayOfMonth value to a number that is higher than the number of days in the month being checked, the last day of the month will generate an appointment.

public class MonthlySchedule : RepeatingSchedule
{
    public int DayOfMonth { get; set; }

    public override bool OccursOnDate(DateTime date)
    {
        return DateIsInPeriod(date) & IsOnCorrectDate(date);
    }

    private bool IsOnCorrectDate(DateTime date)
    {
        if (date.Day == DayOfMonth)
            return true;
        else if (date.Day == DateTime.DaysInMonth(date.Year, date.Month)
                          && DayOfMonth > date.Day)
            return true;
        else
            return false;
    }
}

Final Test

For our final test we'll add two new schedules. The first is a weekly schedule that generates appointments on Mondays, Wednesdays and Fridays. It has a scheduling period that limits appointment generation to the two weeks from 28 May to 8 June 2012. The second schedule creates monthly appointments using 31 as the day of the month. This means that an appointment will be generated on the last day of every month.

var weekly = new WeeklySchedule
{
    Name = "Check Backup Reliability",
    TimeOfDay = new TimeSpan(8, 0, 0),
    SchedulingRange = new Period(new DateTime(2012, 5, 28), new DateTime(2012, 6, 8))
};
weekly.SetDays(
    new DayOfWeek[] { DayOfWeek.Monday, DayOfWeek.Wednesday, DayOfWeek.Friday });

var monthly = new MonthlySchedule
{
    Name = "Check Wages",
    TimeOfDay = new TimeSpan(18, 0, 0),
    DayOfMonth = 31,
    SchedulingRange = new Period(new DateTime(2012, 1, 2), new DateTime(2100, 1, 1))
};

Modify the schedule list as follows:

var schedules = new List<Schedule> { single1, single2, simple ,weekly, monthly };

You can now run the program to generate a calendar for all five schedules. The results are shown below.

2012-05-07 10:00 | Sprint Planning Meeting
2012-05-08 19:30 | Meet Bob for Pint
2012-05-12 09:30 | Confirm Meeting
2012-05-14 10:00 | Sprint Planning Meeting
2012-05-21 10:00 | Sprint Planning Meeting
2012-05-28 08:00 | Check Backup Reliability
2012-05-28 10:00 | Sprint Planning Meeting
2012-05-30 08:00 | Check Backup Reliability
2012-05-31 18:00 | Check Wages
2012-06-01 08:00 | Check Backup Reliability
2012-06-04 08:00 | Check Backup Reliability
2012-06-04 10:00 | Sprint Planning Meeting
2012-06-06 08:00 | Check Backup Reliability
2012-06-08 08:00 | Check Backup Reliability
2012-06-11 10:00 | Sprint Planning Meeting
2012-06-18 10:00 | Sprint Planning Meeting
2012-06-25 10:00 | Sprint Planning Meeting
2012-06-30 18:00 | Check Wages
29 April 2012