Reputation: 550
I'm doing an appointment finder which looks through people's calendars and searches for available time for meetings. It works very fine. However, I wanted to exclude the lookup for the break times, which are a DateRange between two DateTimes which both have 1/1/1 as the year (as placeholder) and include the time behind it. I'm iterating through each minute of a working day and a function should determine whether the current time is in the break hours DateRange.
I have this List with a custom DateRange class:
private static List<DateRange> _breakTimes = new List<DateRange>() {
new DateRange(new DateTime(1, 1, 1, 10, 0, 0), new DateTime(1, 1, 1, 10, 30, 0)),
new DateRange(new DateTime(1, 1, 1, 13, 30, 0), new DateTime(1, 1, 1, 14, 0, 0))
};
And here's the function that iterates through all working days (I have commented the line where the check function should go):
foreach (var date in GetWorkingDays(startDate, endDate))
{
for (int hour = 0; hour <= 24; hour++)
{
if (hour < startingHour || hour >= endingHour) continue;
for (int minute = 0; minute < 60; minute++)
{
var currentDate = new DateTime(date.Year, date.Month, date.Day, hour, minute, 0);
bool shouldContinue = false;
foreach (var breakTime in _breakTimes)
{
// Here it should look wheter the current time is in the break times:
if ((currentDate.Hour >= breakTime.StartDate.Hour && currentDate.Hour <= breakTime.EndDate.Hour) && (currentDate.Minute > breakTime.StartDate.Minute && currentDate.Minute <= breakTime.EndDate.Minute))
{
shouldContinue = true;
break;
}
}
if (shouldContinue) continue;
if (minute == 59)
{
_workingDaysWithHours.Add(new DateRange(new DateTime(date.Year, date.Month, date.Day, hour, minute, 0), new DateTime(date.Year, date.Month, date.Day, hour + 1, 0, 0)));
continue;
}
_workingDaysWithHours.Add(new DateRange(new DateTime(date.Year, date.Month, date.Day, hour, minute, 0), new DateTime(date.Year, date.Month, date.Day, hour, minute + 1, 0)));
}
}
}
So I only need to know how to check whether the current days' time is between a break time. Hope it's understandable.
Upvotes: 0
Views: 51
Reputation: 1555
You can modify the code within this example to get what you want Algorithm to detect overlapping periods
foreach (var date in new [] { DateTime.Now })
{
for (int hour = 0; hour <= 24; hour++)
{
if (hour < startingHour || hour >= endingHour) continue;
for (int minute = 0; minute < 60; minute++)
{
var currentDate = new DateTime(date.Year, date.Month, date.Day, hour, minute, 0);
bool shouldContinue = false;
var currentTime = new DateTime(1, 1, 1, hour, minute, 0);
foreach (var breakTime in _breakTimes)
{
var isInBreak = breakTime.StartDate <= currentTime && currentTime < breakTime.EndDate;
if (isInBreak)
{
shouldContinue = true;
break;
}
}
if (shouldContinue) continue;
if (minute == 59)
{
_workingDaysWithHours.Add(new DateRange(new DateTime(date.Year, date.Month, date.Day, hour, minute, 0), new DateTime(date.Year, date.Month, date.Day, hour + 1, 0, 0)));
continue;
}
_workingDaysWithHours.Add(new DateRange(new DateTime(date.Year, date.Month, date.Day, hour, minute, 0), new DateTime(date.Year, date.Month, date.Day, hour, minute + 1, 0)));
}
}
}
Upvotes: 1
Reputation: 550
I solved it by using a List with Tuples and add the minutes seperatly:
private static List<Tuple<TimeSpan, int>> _breakTimes = new List<Tuple<TimeSpan, int>>()
{
new Tuple<TimeSpan, int>(new TimeSpan(10, 0, 0), 30),
new Tuple<TimeSpan, int>(new TimeSpan(13, 30, 0), 30)
};
And this is the function looking whether it's a break time:
foreach (var breakTime in _breakTimes)
{
if (currentTime >= breakTime.Item1 && currentTime < breakTime.Item1.Add(new TimeSpan(0, breakTime.Item2, 0)))
{
shouldContinue = true;
break;
}
}
Upvotes: 0