tags:

views:

117

answers:

4

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?
+2  A: 

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).

MartinStettner
You have perfectly answered my question! that was exactly what I needed. Thanks.
ealshabaan
+1  A: 

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);
        }
    }
Nick Martyshchenko
+3  A: 

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.

Cheeso
Or even windows scheduler can do the scheduling part. You can then focus on the 'task' part.
Mike Cheel
I guess I was assuming, when he said "8-5pm and Monday-Friday" that the resource being scheduled was a person, probably his advisor. In that case the Windows Task Scheduler (http://msdn.microsoft.com/en-us/library/aa383614(VS.85).aspx) wouldn't be the right thing. Also the Windows Task Scheduler doesn't have built-in smarts to handle things like restricting M-F, or requiring a 15-min slot, etc.
Cheeso
Sorry, but I cannot see how this would answer the question of how to restrict DateTime values to satisfy the given conditions ... Besides, if every programmer had only written software that hadn't existed before, there would be a lot less good programmers out there ...
MartinStettner
Because it's probably some kind of project for uni/school. It doesn't make the question any less valid.
mrnye
well if it's a project for school - a learning project- then yes, I can see why you'd do it from nothing. If it's really a project to manage a schedule, then... there's no reason to make it from nothing, and there are many good reasons to start with an existing calendar tool, like Outlook or Google Calendar. And in that case the original question is moot, which is why I didn't bother answering it.
Cheeso
I do partially agree. Nevertheless the question wasn't about whether it makes sense to write such an application from scratch but about a very specific programming problem. Besides if you're not knowing the specific requirements you cannot tell if there's an existing solution (what about Non-Windows systems? What if the solution should have some command line interface or must connect to other systems?) I think your response should have been a comment because it's not really an answer to the question ...
MartinStettner
A: 
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.

mrnye
I almost clicked +1 just to counteract the one downvote, as you have at least given the rough idea of the solution to one of the questions posed. However, there are a couple errors in the code that might be confusing. I would not have downvoted myself without giving you a chance to edit first, though.
Andrew Barber
was it the typo of 40 instead of 45, or is there something more my monday morning brain is missing? I was just giving a basic example of how to check if a time (in my example the current time) was on a 15 minute barrier. I was just trying to add some insight to some logic that will be required to validate time slots for the OP.
mrnye
if whoever downvoted can clarify what they don't like, please let me know!
mrnye
I think the question wasn't about checking if the *current* time (UTC or not) was within the valid range but about creating *all* valid DateTime structures within a given date range.
MartinStettner
Ok, I can accept that, but I was really just giving a brief answer to provoke thought for the OP. There are many ways to skin the cat, and I was hoping the OP would be able to work it out himself rather than being spoonfed code
mrnye