8000 fix: precision issue with is-within-range function · coder/coder@0f4d521 · GitHub
[go: up one dir, main page]

Skip to content

Commit 0f4d521

Browse files
fix: precision issue with is-within-range function
Co-authored-by: Danny Kopping <danny@coder.com>
1 parent 47cb6fc commit 0f4d521

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

coderd/schedule/cron/cron.go

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,16 @@ func (s Schedule) Next(t time.Time) time.Time {
163163
// However, due to minor implementation imprecision, it is interpreted as
164164
// a range from 08:59:00 to 18:58:59, Monday through Friday.
165165
func (s Schedule) IsWithinRange(t time.Time) bool {
166-
// Get the next scheduled time
167-
next := s.Next(t)
168-
169-
// If the next time is more than a minute away, we're not within range
170-
return next.Sub(t) <= time.Minute
166+
// Truncate to the beginning of the current minute.
167+
currentMinute := t.Truncate(time.Minute)
168+
169+
// Go back 1 second from the current minute to find what the next scheduled time would be.
170+
justBefore := currentMinute.Add(-time.Second)
171+
next := s.Next(justBefore)
172+
173+
// If the next scheduled time is exactly at the current minute,
174+
// then we are within the range.
175+
return next.Equal(currentMinute)
171176
}
172177

173178
var (

0 commit comments

Comments
 (0)
0