71 lines
2.4 KiB
C#
71 lines
2.4 KiB
C#
![]() |
namespace ClassLibrary;
|
|||
|
|
|||
|
public class Calculations
|
|||
|
{
|
|||
|
static void AvailablePeriods(TimeSpan[] startTimes, int[] durations, TimeSpan beginWorkingTime, TimeSpan endWorkingTime, int consultationTime)
|
|||
|
{
|
|||
|
DateTime[] startTimesAsDateTime = new DateTime[startTimes.Length];
|
|||
|
for (int i = 0; i < startTimes.Length; i++)
|
|||
|
{
|
|||
|
startTimesAsDateTime[i] = DateTime.Today.Add(startTimes[i]);
|
|||
|
}
|
|||
|
|
|||
|
DateTime beginWorkingDateTime = DateTime.Today.Add(beginWorkingTime);
|
|||
|
DateTime endWorkingDateTime = DateTime.Today.Add(endWorkingTime);
|
|||
|
|
|||
|
string[] result = DatesBumBum(beginWorkingDateTime, endWorkingDateTime, startTimesAsDateTime, durations, consultationTime);
|
|||
|
|
|||
|
int count = 0;
|
|||
|
foreach (string date in result)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(date))
|
|||
|
{
|
|||
|
Console.WriteLine(date);
|
|||
|
count++;
|
|||
|
}
|
|||
|
}
|
|||
|
Console.WriteLine(count);
|
|||
|
}
|
|||
|
|
|||
|
static string[] DatesBumBum(DateTime beginWorkingTime, DateTime endWorkingTime, DateTime[] startTimes, int[] durations, int consultationTime)
|
|||
|
{
|
|||
|
int maxIntervals = (int)((endWorkingTime - beginWorkingTime).TotalMinutes / consultationTime);
|
|||
|
string[] availablePeriods = new string[maxIntervals];
|
|||
|
int index = 0;
|
|||
|
DateTime workTime = beginWorkingTime;
|
|||
|
int i = 0;
|
|||
|
|
|||
|
while (workTime < endWorkingTime)
|
|||
|
{
|
|||
|
DateTime nextBusyStart = (i < startTimes.Length) ? startTimes[i] : endWorkingTime;
|
|||
|
TimeSpan availableTime = nextBusyStart - workTime;
|
|||
|
int availableMinutes = (int)availableTime.TotalMinutes;
|
|||
|
|
|||
|
while (availableMinutes >= consultationTime)
|
|||
|
{
|
|||
|
DateTime nextAvailableEnd = workTime.AddMinutes(consultationTime);
|
|||
|
|
|||
|
if (nextAvailableEnd > endWorkingTime)
|
|||
|
{
|
|||
|
nextAvailableEnd = endWorkingTime;
|
|||
|
}
|
|||
|
|
|||
|
availablePeriods[index++] = $"{workTime:HH:mm}-{nextAvailableEnd:HH:mm}";
|
|||
|
|
|||
|
workTime = nextAvailableEnd;
|
|||
|
availableMinutes -= consultationTime;
|
|||
|
}
|
|||
|
|
|||
|
if (i < startTimes.Length)
|
|||
|
{
|
|||
|
workTime = startTimes[i].AddMinutes(durations[i]);
|
|||
|
i++;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
return availablePeriods;
|
|||
|
}
|
|||
|
}
|