Skip to content

Commit 340f410

Browse files
test: improve tests and docs
1 parent 4687f2a commit 340f410

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

coderd/schedule/cron/cron.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,13 @@ func (s Schedule) Next(t time.Time) time.Time {
155155
return s.sched.Next(t)
156156
}
157157

158+
// IsWithinRange interprets a cron spec as a continuous time range,
159+
// and returns whether the provided time value falls within that range.
160+
//
161+
// For example, the expression "* 9-18 * * 1-5" should represent a continuous time range
162+
// from 09:00 to 18:59, Monday through Friday.
163+
// However, due to minor implementation imprecision, it is interpreted as
164+
// a range from 08:59:00 to 18:58:59, Monday through Friday.
158165
func (s Schedule) IsWithinRange(t time.Time) bool {
159166
// Get the next scheduled time
160167
next := s.Next(t)

coderd/schedule/cron/cron_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ func TestIsWithinRange(t *testing.T) {
171171
at time.Time
172172
expectedWithinRange bool
173173
}{
174+
// "* 9-18 * * 1-5" should be interpreted as a continuous time range from 08:59:00 to 18:58:59, Monday through Friday
174175
{
175176
name: "Right before the start of the time range",
176177
spec: "* 9-18 * * 1-5",
@@ -225,6 +226,18 @@ func TestIsWithinRange(t *testing.T) {
225226
at: mustParseTime(t, time.RFC1123, "Mon, 02 Jun 2025 02:00:00 UTC"),
226227
expectedWithinRange: false,
227228
},
229+
{
230+
name: "Outside the day range #1",
231+
spec: "* 9-18 * * 1-5",
232+
at: mustParseTime(t, time.RFC1123, "Sat, 07 Jun 2025 14:00:00 UTC"),
233+
expectedWithinRange: false,
234+
},
235+
{
236+
name: "Outside the day range #2",
237+
spec: "* 9-18 * * 1-5",
238+
at: mustParseTime(t, time.RFC1123, "Sun, 08 Jun 2025 14:00:00 UTC"),
239+
expectedWithinRange: false,
240+
},
228241
}
229242

230243
for _, testCase := range testCases {

0 commit comments

Comments
 (0)