Eyad
Eyad

Reputation: 14229

DateTime interval restriction in C#

The problem: I am in process of implementing a scheduler for my advisor in school. The scheduler supposes to setup a 15 minutes interval time slot from 8:00 AM to 5:00 PM, Monday to Friday. In addition, the advisor will have to specify the start and end dates of the scheduler. The scheduler will also feature an option to specify if the 15 minutes time slot is not open. Meaning my advisor will be able to mark specific time slot as NOT AVAILABLE.

What I have so far: I have created a simple class:

public class TimeSlot  
    {
        public DateTime dateTime
        {
            get;
            set;
        }

        public bool isAvailable
        {
            get;
            set;
        }

        TimeSlot(DateTime dt, bool Avalible)
        {
            dateTime = dt;
            isAvailable = Avalible;
        } 
    }

The class basically represents an object for one time slot in the scheduler. I also have a list of time slots that keeps a list of the valid time slots:

List<TimeSlot> TSList = new List<TimeSlot>();

Note that a valid time slot means the following:

  1. Date is within: Monday to Friday.
  2. Time is within: 8:00 AM to 5:00 PM
  3. Time slots are within: 15 minutes interval.

In addition, I have a method that fill in the TSList as the following:

 private void button_Next_Click(object sender, RoutedEventArgs e)
    {
        /* Getting the values of fromDate and toDate from the GUI controls*/
        DateTime fromDate = datePicker1.SelectedDate.Value;
        DateTime toDate = datePicker2.SelectedDate.Value;

        while (fromDate <= toDate)
        {
            /*This ensures that we only deal with days Monday to Friday*/
            if (fromDate.DayOfWeek.ToString() != "Saturday" && fromDate.DayOfWeek.ToString() != "Sunday")
            {
                /*PROBLEM HERE!!*/
            }

            /*Updating fromDate: Incrementing fromDate by 1 day*/
            fromDate = fromDate.AddDays(1);
        }

    }

Notes that I was only able to satisfy the first condition in my valid time slot conditions. Thus, I was only able to restrict the dates to be within Monday to Friday range.

The questions: I am trying to achieve the missing two valid conditions for a time slot:

  1. How to restrict the times to be only 8:00am to 5:00 pm?
  2. How to make time slots separated by 15 minutes interval?

Upvotes: 5

Views: 6184

Answers (4)

mike
mike

Reputation: 3166

DateTime myScheduledTimeSlot = new DateTime(2010, 10, 26, 8, 45, 0);

// Use existing check to check day of week constraint...

// Check if the datetime falls on a correct minute boundary
switch (myScheduledTimeSlot.Minute)
{
  case 0:
  case 15:
  case 30:
  case 45:
    // The time slot is valid
    break;
  default:
    // The time slot is not valid
    break;
}  

It is pretty simple to check whether it falls in a 15 minute slot as you don't have weird boundaries keeping every hour identical. I'd recommend checking out Quart.NET if you want to save some time doing eventing/scheduling.

Upvotes: -1

Cheeso
Cheeso

Reputation: 192457

Why are you considering building this out of nothing?

Why are you not starting with one of the many calendar management programs that are available off the shelf? For example, Microsoft Outlook contains calendar and schedule management, and you can do all of what you describe, easily. It also integrates with other scheduling tools via .ICS files, it syncs with mobile devices, syncs with Google Calendar, and so on.

But there are lots of other options. Google Calendar is another obvious one.

I don't know why you would ever consider starting from scratch. Unless it's an academic exercise (and no, I don't mean that you work in academia), then you should use larger building blocks to start.

It's like building a structure, starting with sand and water, instead of pre-fabricated concrete block.

Upvotes: 3

Nick Martyshchenko
Nick Martyshchenko

Reputation: 4249

Just quick implementation. Let me know if you need some comments.

        // Round interval
        const int roundInterval = 15;

        var remainder = fromDate.TimeOfDay.Minutes % roundInterval;

        var curTime = remainder == 0 ? fromDate : fromDate.AddMinutes(roundInterval - remainder);
        curTime = curTime.AddSeconds(-curTime.TimeOfDay.Seconds);

        var delta = TimeSpan.FromMinutes(roundInterval);

        while (curTime < toDate)
        {
            while (curTime.DayOfWeek == DayOfWeek.Saturday || curTime.DayOfWeek == DayOfWeek.Sunday)
            {
                curTime = curTime.Date.AddDays(1);
            }

            if (curTime.TimeOfDay.Hours < 8)
            {
                curTime = curTime.AddHours(8 - curTime.TimeOfDay.Hours);
                curTime = curTime.AddMinutes(-curTime.TimeOfDay.Minutes);
                continue;
            }

            if (curTime.TimeOfDay.Hours >= 17)
            {
                curTime = curTime.AddHours(24 - curTime.TimeOfDay.Hours);
                curTime = curTime.AddMinutes(-curTime.TimeOfDay.Minutes);
                continue;
            }

            TSList.Add(new TimeSlot(curTime, true));

            curTime = curTime.Add(delta);
        }
    }

Upvotes: 1

MartinStettner
MartinStettner

Reputation: 29164

First, please use DayOfWeek.Saturday and DayOfWeek.Sunday for the comparision, converting to a string is not necessary...

Then just use a simple loop like

DateTime startSlot = fromDate.Date.AddHours(8); // Starts at 8:00AM
while (startSlot.Hour < 17) {
  // Construct time slot class
  startSlot = startSlot.AddMinutes(15);
}

This gives you startSlot values starting at 8:00am at every date ranging to 5pm (i.e. the last one is 4:45pm).

Upvotes: 3

Related Questions