Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
zhiyuanliang-ms committed Dec 9, 2024
1 parent 5519811 commit 93c9800
Showing 1 changed file with 15 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -347,28 +347,27 @@ private static bool IsDurationCompliantWithDaysOfWeek(TimeSpan duration, int int
return true;
}

DateTime firstDayOfThisWeek = DateTime.Today.AddDays(
DaysPerWeek - CalculateWeeklyDayOffset(DateTime.Today.DayOfWeek, firstDayOfWeek));

List<DayOfWeek> sortedDaysOfWeek = SortDaysOfWeek(daysOfWeek, firstDayOfWeek);

DateTime prev = DateTime.MinValue;
DayOfWeek firstDay = sortedDaysOfWeek.First(); // the closest occurrence day to the first day of week

DateTime firstOccurrenceOfThisWeek = DateTime.Today.AddDays(
DaysPerWeek - CalculateWeeklyDayOffset(DateTime.Today.DayOfWeek, firstDay));

DateTime prev = firstOccurrenceOfThisWeek;

TimeSpan minGap = TimeSpan.FromDays(DaysPerWeek);

foreach (DayOfWeek dayOfWeek in sortedDaysOfWeek)
foreach (DayOfWeek dayOfWeek in sortedDaysOfWeek.Skip(1))
{
DateTime date = firstDayOfThisWeek.AddDays(
CalculateWeeklyDayOffset(dayOfWeek, firstDayOfWeek));
DateTime date = firstOccurrenceOfThisWeek.AddDays(
CalculateWeeklyDayOffset(dayOfWeek, firstDay));

if (prev != DateTime.MinValue)
{
TimeSpan gap = date - prev;
TimeSpan gap = date - prev;

if (gap < minGap)
{
minGap = gap;
}
if (gap < minGap)
{
minGap = gap;
}

prev = date;
Expand All @@ -378,12 +377,9 @@ private static bool IsDurationCompliantWithDaysOfWeek(TimeSpan duration, int int
// It may across weeks. Check the next week if the interval is one week.
if (interval == 1)
{
DateTime firstDayOfNextWeek = firstDayOfThisWeek.AddDays(DaysPerWeek);

DateTime firstOccurrenceInNextWeek = firstDayOfNextWeek.AddDays(
CalculateWeeklyDayOffset(sortedDaysOfWeek.First(), firstDayOfWeek)); // firstDayOfWeek may not in the sortedDaysOfWeek
DateTime firstOccurrenceOfNextWeek = firstOccurrenceOfThisWeek.AddDays(DaysPerWeek);

TimeSpan gap = firstOccurrenceInNextWeek - prev;
TimeSpan gap = firstOccurrenceOfNextWeek - prev;

if (gap < minGap)
{
Expand Down

0 comments on commit 93c9800

Please sign in to comment.