64 lines
1.8 KiB
C#
64 lines
1.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DatesLibrary;
|
|
|
|
public class Calculations
|
|
{
|
|
public static string[] AvailablePeriods(
|
|
TimeSpan[] startTimes,
|
|
int[] durations,
|
|
TimeSpan beginWorkingTime,
|
|
TimeSpan endWorkingTime,
|
|
int consultationTime)
|
|
{
|
|
List<string> availablePeriods = new();
|
|
TimeSpan workTime = beginWorkingTime;
|
|
int i = 0;
|
|
|
|
while (workTime < endWorkingTime)
|
|
{
|
|
TimeSpan nextBusyStart = (i < startTimes.Length) ? startTimes[i] : endWorkingTime;
|
|
int availableMinutes = (int)(nextBusyStart - workTime).TotalMinutes;
|
|
|
|
if (availableMinutes < consultationTime)
|
|
{
|
|
if (i < startTimes.Length)
|
|
{
|
|
workTime = startTimes[i].Add(TimeSpan.FromMinutes(durations[i]));
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
continue;
|
|
}
|
|
|
|
while (availableMinutes >= consultationTime)
|
|
{
|
|
TimeSpan nextAvailableEnd = workTime.Add(TimeSpan.FromMinutes(consultationTime));
|
|
|
|
if (nextAvailableEnd > endWorkingTime)
|
|
break;
|
|
|
|
availablePeriods.Add($"{workTime:hh\\:mm}-{nextAvailableEnd:hh\\:mm}");
|
|
|
|
workTime = nextAvailableEnd;
|
|
availableMinutes -= consultationTime;
|
|
}
|
|
|
|
if (i < startTimes.Length)
|
|
{
|
|
workTime = startTimes[i].Add(TimeSpan.FromMinutes(durations[i]));
|
|
i++;
|
|
}
|
|
else
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
|
|
return availablePeriods.ToArray();
|
|
}
|
|
} |