@@ -49,10 +49,17 @@ static class RecurrenceEvaluator
49
49
const string Numbered = "Numbered" ;
50
50
const string NoEnd = "NoEnd" ;
51
51
52
- const int WeeklyUnitIntervalDuration = 7 ; // in days
53
- const int MonthlyUnitIntervalDuration = 28 ; // in days
54
- const int YearlyUnitIntervalDuration = 365 ; // in days
55
-
52
+ const int WeekDayNumber = 7 ;
53
+ const int MinMonthDayNumber = 28 ;
54
+ const int MinYearDayNumber = 365 ;
55
+
56
+ /// <summary>
57
+ /// Checks if a provided timestamp is within any recurring time window specified by the Recurrence section in the time window filter settings.
58
+ /// If the time window filter has an invalid recurrence setting, an exception will be thrown.
59
+ /// <param name="time">A time stamp.</param>
60
+ /// <param name="settings">The settings of time window filter.</param>
61
+ /// <returns>True if the time stamp is within any recurring time window, false otherwise.</returns>
62
+ /// </summary>
56
63
public static bool MatchRecurrence ( DateTimeOffset time , TimeWindowFilterSettings settings )
57
64
{
58
65
if ( settings == null )
@@ -88,6 +95,13 @@ public static bool MatchRecurrence(DateTimeOffset time, TimeWindowFilterSettings
88
95
return false ;
89
96
}
90
97
98
+ /// <summary>
99
+ /// Try to find the closest previous recurrence occurrence before the provided time stamp according to the recurrence pattern.
100
+ /// <param name="time">A time stamp.</param>
101
+ /// <param name="settings">The settings of time window filter.</param>
102
+ /// <param name="previousOccurrence">The closest orevious occurrence.</param>
103
+ /// /// <returns>True if the closest previous occurrence is within the recurrence range, false otherwise.</returns>
104
+ /// </summary>
91
105
private static bool TryGetPreviousOccurrence ( DateTimeOffset time , TimeWindowFilterSettings settings , out DateTimeOffset previousOccurrence )
92
106
{
93
107
previousOccurrence = DateTimeOffset . MaxValue ;
@@ -619,13 +633,13 @@ private static bool TryValidateWeeklyRecurrencePattern(TimeWindowFilterSettings
619
633
620
634
RecurrencePattern pattern = settings . Recurrence . Pattern ;
621
635
622
- TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * WeeklyUnitIntervalDuration ) ;
636
+ TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * WeekDayNumber ) ;
623
637
624
638
TimeSpan timeWindowDuration = settings . End . Value - settings . Start . Value ;
625
639
626
640
//
627
641
// Time window duration must be shorter than how frequently it occurs
628
- if ( settings . End . Value - settings . Start . Value > intervalDuration )
642
+ if ( timeWindowDuration > intervalDuration )
629
643
{
630
644
paramName = $ "{ nameof ( settings . End ) } ";
631
645
@@ -684,7 +698,7 @@ private static bool TryValidateAbsoluteMonthlyRecurrencePattern(TimeWindowFilter
684
698
685
699
RecurrencePattern pattern = settings . Recurrence . Pattern ;
686
700
687
- TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * MonthlyUnitIntervalDuration ) ;
701
+ TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * MinMonthDayNumber ) ;
688
702
689
703
//
690
704
// Time window duration must be shorter than how frequently it occurs
@@ -730,7 +744,7 @@ private static bool TryValidateRelativeMonthlyRecurrencePattern(TimeWindowFilter
730
744
731
745
RecurrencePattern pattern = settings . Recurrence . Pattern ;
732
746
733
- TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * MonthlyUnitIntervalDuration ) ;
747
+ TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * MinMonthDayNumber ) ;
734
748
735
749
//
736
750
// Time window duration must be shorter than how frequently it occurs
@@ -782,7 +796,7 @@ private static bool TryValidateAbsoluteYearlyRecurrencePattern(TimeWindowFilterS
782
796
783
797
RecurrencePattern pattern = settings . Recurrence . Pattern ;
784
798
785
- TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * YearlyUnitIntervalDuration ) ;
799
+ TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * MinYearDayNumber ) ;
786
800
787
801
//
788
802
// Time window duration must be shorter than how frequently it occurs
@@ -833,7 +847,7 @@ private static bool TryValidateRelativeYearlyRecurrencePattern(TimeWindowFilterS
833
847
834
848
RecurrencePattern pattern = settings . Recurrence . Pattern ;
835
849
836
- TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * YearlyUnitIntervalDuration ) ;
850
+ TimeSpan intervalDuration = TimeSpan . FromDays ( pattern . Interval * MinYearDayNumber ) ;
837
851
838
852
//
839
853
// Time window duration must be shorter than how frequently it occurs
@@ -1211,9 +1225,17 @@ private static TimeSpan GetRecurrenceTimeZone(TimeWindowFilterSettings settings)
1211
1225
return timeZoneOffset ;
1212
1226
}
1213
1227
1228
+ /// <summary>
1229
+ /// Check whether the duration is shorter than the minimum gap between recurrence of days of week.
1230
+ /// </summary>
1231
+ /// <param name="duration">The time span of the duration.</param>
1232
+ /// <param name="interval">The recurrence interval.</param>
1233
+ /// <param name="daysOfWeek">The days of the week when the recurrence will occur.</param>
1234
+ /// <param name="firstDayOfWeek">The first day of the week.</param>
1235
+ /// <returns>True if the duration is compliant with days of week, false otherwise.</returns>
1214
1236
private static bool IsDurationCompliantWithDaysOfWeek ( TimeSpan duration , int interval , IEnumerable < string > daysOfWeek , string firstDayOfWeek )
1215
1237
{
1216
- if ( daysOfWeek == null || daysOfWeek . Count ( ) < 1 )
1238
+ if ( daysOfWeek == null || ! daysOfWeek . Any ( ) )
1217
1239
{
1218
1240
throw new ArgumentException ( nameof ( daysOfWeek ) ) ;
1219
1241
}
0 commit comments