@@ -163,6 +163,89 @@ func Test_Weekly(t *testing.T) {
163
163
}
164
164
}
165
165
166
+ func TestIsWithinRange (t * testing.T ) {
167
+ t .Parallel ()
168
+ testCases := []struct {
169
+ name string
170
+ spec string
171
+ at time.Time
172
+ expectedWithinRange bool
173
+ }{
174
+ {
175
+ name : "Right before the start of the time range" ,
176
+ spec : "* 9-18 * * 1-5" ,
177
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 8:58:59 UTC" ),
178
+ expectedWithinRange : false ,
179
+ },
180
+ {
181
+ name : "Start of the time range" ,
182
+ spec : "* 9-18 * * 1-5" ,
183
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 8:59:00 UTC" ),
184
+ expectedWithinRange : true ,
185
+ },
186
+ {
187
+ name : "9AM - One minute after the start of the time range" ,
188
+ spec : "* 9-18 * * 1-5" ,
189
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 9:00:00 UTC" ),
190
+ expectedWithinRange : true ,
191
+ },
192
+ {
193
+ name : "2PM - The middle of the time range" ,
194
+ spec : "* 9-18 * * 1-5" ,
195
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 14:00:00 UTC" ),
196
+ expectedWithinRange : true ,
197
+ },
198
+ {
199
+ name : "6PM - Around one hour before the end of the time range" ,
200
+ spec : "* 9-18 * * 1-5" ,
201
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 18:00:00 UTC" ),
202
+ expectedWithinRange : true ,
203
+ },
204
+ {
205
+ name : "End of the time range" ,
206
+ spec : "* 9-18 * * 1-5" ,
207
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 18:58:59 UTC" ),
208
+ expectedWithinRange : true ,
209
+ },
210
+ {
211
+ name : "Right after the end of the time range" ,
212
+ spec : "* 9-18 * * 1-5" ,
213
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 18:59:00 UTC" ),
214
+ expectedWithinRange : false ,
215
+ },
216
+ {
217
+ name : "7PM - Around one minute after the end of the time range" ,
218
+ spec : "* 9-18 * * 1-5" ,
219
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 19:00:00 UTC" ),
220
+ expectedWithinRange : false ,
221
+ },
222
+ {
223
+ name : "2AM - Significantly outside the time range" ,
224
+ spec : "* 9-18 * * 1-5" ,
225
+ at : mustParseTime (t , time .RFC1123 , "Mon, 02 Jun 2025 02:00:00 UTC" ),
226
+ expectedWithinRange : false ,
227
+ },
228
+ }
229
+
230
+ for _ , testCase := range testCases {
231
+ testCase := testCase
232
+ t .Run (testCase .name , func (t * testing.T ) {
233
+ t .Parallel ()
234
+ sched , err := cron .Weekly (testCase .spec )
235
+ require .NoError (t , err )
236
+ withinRange := sched .IsWithinRange (testCase .at )
237
+ require .Equal (t , testCase .expectedWithinRange , withinRange )
238
+ })
239
+ }
240
+ }
241
+
242
+ func mustParseTime (t * testing.T , layout , value string ) time.Time {
243
+ t .Helper ()
244
+ parsedTime , err := time .Parse (layout , value )
245
+ require .NoError (t , err )
246
+ return parsedTime
247
+ }
248
+
166
249
func mustLocation (t * testing.T , s string ) * time.Location {
167
250
t .Helper ()
168
251
loc , err := time .LoadLocation (s )
0 commit comments