-
Notifications
You must be signed in to change notification settings - Fork 126
Small fix for recurring time window testcase #522
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
5519811
93c9800
bd9e937
c477006
6ed724c
5a2f87f
26f0c78
c49f145
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -341,49 +341,38 @@ private static bool TryValidateNumberOfOccurrences(TimeWindowFilterSettings sett | |
| private static bool IsDurationCompliantWithDaysOfWeek(TimeSpan duration, int interval, IEnumerable<DayOfWeek> daysOfWeek, DayOfWeek firstDayOfWeek) | ||
| { | ||
| Debug.Assert(interval > 0); | ||
| Debug.Assert(daysOfWeek.Count() > 0); | ||
|
|
||
| if (daysOfWeek.Count() == 1) | ||
| { | ||
| 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 | ||
|
|
||
| DayOfWeek prev = firstDay; | ||
|
|
||
| TimeSpan minGap = TimeSpan.FromDays(DaysPerWeek); | ||
|
|
||
| foreach (DayOfWeek dayOfWeek in sortedDaysOfWeek) | ||
| foreach (DayOfWeek dayOfWeek in sortedDaysOfWeek.Skip(1)) | ||
| { | ||
| DateTime date = firstDayOfThisWeek.AddDays( | ||
| CalculateWeeklyDayOffset(dayOfWeek, firstDayOfWeek)); | ||
| TimeSpan gap = TimeSpan.FromDays(CalculateWeeklyDayOffset(dayOfWeek, prev)); | ||
|
|
||
| if (prev != DateTime.MinValue) | ||
| if (gap < minGap) | ||
| { | ||
| TimeSpan gap = date - prev; | ||
|
|
||
| if (gap < minGap) | ||
| { | ||
| minGap = gap; | ||
| } | ||
| minGap = gap; | ||
| } | ||
|
|
||
| prev = date; | ||
| prev = dayOfWeek; | ||
| } | ||
|
|
||
| // | ||
| // 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)); | ||
|
|
||
| TimeSpan gap = firstOccurrenceInNextWeek - prev; | ||
| TimeSpan gap = TimeSpan.FromDays(CalculateWeeklyDayOffset(firstDay, prev)); | ||
|
|
||
| if (gap < minGap) | ||
| { | ||
|
|
@@ -413,7 +402,7 @@ private static int CalculateWeeklyDayOffset(DayOfWeek day1, DayOfWeek day2) | |
| /// </summary> | ||
| private static List<DayOfWeek> SortDaysOfWeek(IEnumerable<DayOfWeek> daysOfWeek, DayOfWeek firstDayOfWeek) | ||
| { | ||
| List<DayOfWeek> result = daysOfWeek.ToList(); | ||
| List<DayOfWeek> result = daysOfWeek.Distinct().ToList(); // dedup | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we take a Set instead so we don't need to Distinct it?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally like the |
||
|
|
||
| result.Sort((x, y) => | ||
| CalculateWeeklyDayOffset(x, firstDayOfWeek) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't like this Skip syntax- it seems less readable, could you add a comment why we skip the first?
I'd prefer we don't do a skip and explicitly check
if prev == firstDayorif prev != nullinstead.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How about this? Using index to skip the first day, instead of
.Skip(1)