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