1

I have a scenario where user can create and add a rule. This rule is to be assigned to some time slot in a week for execution. Suppose a second rule is added, it can not be assigned between the time slot of first one.

NOTE: I have to implement this for a single week i.e., Monday to Sunday. No second week exists. This single week logic will run for every week for the entire year.

Thorough my attempt, if one rule is running from Tuesday to Friday I am able to assign the time slots of merely Monday OR Saturday to Sunday for another rule but not able to book/assign a time slot from Saturday to Monday (i.e., the starting point of the week).

Following is my attempt so far:

foreach (var item in rules)
{
    DateTime StartDateWantToBook = Convert.ToDateTime((DateTime.Today.Month + "/" + StartDay + "/" + DateTime.Today.Year) + " " + StartDayTime);
    DateTime EndDateWantToBook = Convert.ToDateTime((DateTime.Today.Month + "/" + EndDay + "/" + DateTime.Today.Year) + " " + EndDayTime);
    DateTime StartDateAlreadyBooked = Convert.ToDateTime((DateTime.Today.Month + "/" + item.StartDay + "/" + DateTime.Today.Year) + " " + item.StartDayTime);
    DateTime EndDateAlreadyBooked = Convert.ToDateTime((DateTime.Today.Month + "/" + item.EndDay + "/" + DateTime.Today.Year) + " " + item.EndDayTime);

    //For same days and sun-sat days
    if ((EndDateAlreadyBooked - StartDateAlreadyBooked).TotalSeconds >= 0)
    {
        if ((StartDateWantToBook <= StartDateAlreadyBooked && EndDateWantToBook <= StartDateAlreadyBooked) || (StartDateWantToBook >= EndDateAlreadyBooked && EndDateWantToBook >= EndDateAlreadyBooked)
            || (EndDateWantToBook <= StartDateAlreadyBooked && StartDateWantToBook >= EndDateAlreadyBooked))
        {
            IsExist = false;
        }
        else
            IsExist = true;
    }
    else
    { 
         if ((StartDateWantToBook <= StartDateAlreadyBooked && EndDateWantToBook <= StartDateAlreadyBooked) && (StartDateWantToBook >= EndDateAlreadyBooked && EndDateWantToBook >= EndDateAlreadyBooked))
         {
             IsExist = false;
         }
         else
             IsExist = true;
     }

     if (IsExist == true)
     {
         break;
     }
 }

Please somebody suggest what should be edited... Or fresh logic to do this...

EDIT : This is how my time slots are colored based on the rule..White spaces are unoccupied.

Rule Template View

LATEST EDIT :

In my case i am taking Sunday=1, Monday=2.....Saturday=7. Let's have a scenario, where i booked Sunday 10:00 to Sunday 14:00. So my

ExistingStartDate : 7/1/2015 10:00 am

ExistingEndDate : 7/1/2015 02:00 pm

Now, i am booking another time slot from Monday 10:00 am to Sunday 06:00 pm So,

RequestedStartDate : 7/2/2015 10:00 am

RequestedEndDate : 7/1/2015 06:00 pm.

In this case no if() conditions passes..uploaded an image for reference..

devgal
  • 149
  • 1
  • 14

2 Answers2

2

My understanding, from your description, is that any partial overlap in slots is enough to prevent the creation of the new record.

Assuming this is your existing time slot

         |-----------------|

Clashes will occur with the following requests

 (1)  |---------------|
 (2)  |------------------------ |
 (3)          |--------|
 (4)               |-----------| 

You could write conditions for all these scenarios, but I think it could be boiled down to the following:

A clash occurs if

  • the start or end data of the requested booking falls during the start and end date of the existing booking. This covers scenarios 1, 3 and 4
  • The start or end date of the existing booking falls during the start and end date of the requested booking. This covers scenario 2.

So your logic would be something like...

if((StartDateWantToBook < EndDateAlreadyBooked) && (StartDateAlreadyBooked < EndDateWantToBook))

Just one final point: your variable names are very similar - variable names like RequestedStart, RequestedEnd, ExistingStart and ExistingEnd would make your code more readable and maintainable.

Edit: If you can't prevent the user selecting the a start date which is after the end date you can do one of the following...

// Throw an exception....
if (StartDateWantToBook > EndDateWantToBook)
  throw new Exception("Start Date must occur before End Date");

or...

// Correct the user's selection by swapping the values...
if (StartDateWantToBook > EndDateWantToBook)
{  
    DateTime temp = StartDateWantToBook ;
    StartDateWantToBook = EndDateWantToBook;
    EndDateWantToBook = temp;
}

  throw new Exception("Start Date must occur before End Date");
amcdermott
  • 1,505
  • 14
  • 21
  • Thanks for the help, i give it a try..but it's not working in my case...could you please see my LATEST EDIT in the question.. – devgal Jul 08 '15 at 11:28
  • Your latest edit won't work because your requested date range goes from Feb/2015 to Jan/2015. That's a data validation issue rather than a business logic one. The start of your method should check that the requested end date occurs *after* the requested start date and throw an exception if it doesn't. – amcdermott Jul 08 '15 at 11:55
  • It's in MM/DD/YYYY format...will go from 2nd July to 1st July. I can not validate as user is free to select and book any time slot Sunday to Monday or Monday to Sunday...For implementing this I have assumed Sunday as 1 but in actual starting point can be any thing...may be my assumption approach is completely wrong....but i have to make this as daywise instead of datewise... do you have any suggestion now, at this point of time. Please help..!! – devgal Jul 08 '15 at 12:10
  • thanks for the reply.....but swapping the values will do all the things wrong...for example, if user book from monday to sunday...swapping will book sunday to monday, instead of booking for full week it will book for 1 day – devgal Jul 09 '15 at 06:50
  • But surely the user books from date1 to date2 rather than just selecting days? I mean, you're working with 'DateTime' variables here no? – amcdermott Jul 09 '15 at 08:47
1

Seems like you want to check for overlaps between two periods. That's easily done with a "cross check":

bool overlaps = false;
if( (StartDateWantToBook <= EndDateAlreadyBooked ) && (StartDateAlreadyBooked <= EndDateWantToBook) )
  overlaps = true;
Community
  • 1
  • 1
Carra
  • 16,707
  • 5
  • 58
  • 73
  • Thanks for the trick and concise solution...but in my case it didn't worked....Can you please see my LATEST EDIT.... – devgal Jul 08 '15 at 11:43