starting 5/6, every other week

Several years ago I was on a small development team that wrote a system to maintain the “on-call” schedules for 100-200 of the company's IT support teams distributed throughout the world.  Taoufik Tahiri, our brilliant chief designer, came up with most of the logic for the inner workings of the scheduling system.  One of the things I liked the most was how it handled regular, reoccurring shifts or schedules.

Over the years several people have asked me how this part of the system worked since, at the data layer, it did not generate multiple instances to represent all the shifts/dates/data.  Essentially it worked by using the patterns inherent to the calendar – a week has seven days, a year has 12 months, etc.

Using a simple example, let’s say you have a shift or event that starts on a particular date (startDate) and occurs every other (frequency = 2) Sunday at 8am.  We call this a “weekly” type.  With a simple date and mod calculation you can quickly tell if the weekly shift is occurring on any given date (anyGivenDate):

private bool IsOccurring( DateTime startDate, DateTime anyGivenDate, int frequency )
{
       if ( startDate.DayOfWeek != anyGivenDate.DayOfWeek )
              return false;
       TimeSpan daysElapsed = anyGivenDate - startDate;
       int numWeeks = ( daysElapsed.Days / 7 );
       int remainder = numWeeks % frequency;
       return ( remainder == 0 ) ? true : false;
}

A few years ago I wrote some software for scheduling our ushering team which used this same concept but also introduced other “frequency types” including:

  • Two On, Two Off
  • Week of Month (example: third Sunday of the month)
  • Weekday of Month (see explanation below)
  • Misc Dates (a random, misc list of dates)

If a Sunday usher desires to work on the 1st and 4th weeks, setting their frequency to "Week of Month" and checking the 1st and 4th checkboxes - their schedule resembles the following:

         Jan                    Feb                    Mar
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
             1  2  3    1  2  3  4  5  6  7       1  2  3  4  5  6
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    7  8  9 10 11 12 13
11 12 13 14 15 16 17   15 16 17 18 19 20 21   14 15 16 17 18 19 20
18 19 20 21 22 23 24   22 23 24 25 26 27 28   21 22 23 24 25 26 27
25 26 27 28 29 30 31   29                     28 29 30 31

If, on the other hand, a Sunday usher desires to work on the 1st and 4th Sunday (weekday), setting their frequency to "Weekday of Month" and checking the 1st and 4th checkboxes - their schedule will resemble the following:

         Jan                    Feb                    Mar
 S  M Tu  W Th  F  S    S  M Tu  W Th  F  S    S  M Tu  W Th  F  S
             1  2  3    1  2  3  4  5  6  7       1  2  3  4  5  6
 4  5  6  7  8  9 10    8  9 10 11 12 13 14    7  8  9 10 11 12 13
11 12 13 14 15 16 17   15 16 17 18 19 20 21   14 15 16 17 18 19 20
18 19 20 21 22 23 24   22 23 24 25 26 27 28   21 22 23 24 25 26 27
25 26 27 28 29 30 31   29                     28 29 30 31

Note that while months (such as Feb in the example) where the first week contains a Sunday are identical, the other months are quite different.

Although this is only a simplified explanation, the system handles a variety of other scenarios including exception instances which represent shifts, events or schedules which differ (for any reason) from the normal pattern.