Skip to content

Commit 2e52b54

Browse files
committed
snooze: if we can't find a window to fit in, look for the next future time
1 parent 74ffac5 commit 2e52b54

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

internal/provider/pd/client_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,6 @@ func TestClient_RejectedLateCheckIn(t *testing.T) {
114114
Times: []string{
115115
// Never allow the current time to check-in
116116
now.Add(-1*time.Hour - 30*time.Minute).Format("15:04"),
117-
// Add a future time that's too far in the future
118-
now.Add(2*time.Hour + 30*time.Minute).Format("15:04"),
119117
},
120118
Tolerance: "5m",
121119
},

internal/provider/snooze/snooze.go

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -113,21 +113,14 @@ func Calculate(now time.Time, schedule config.ScheduleConfig) (time.Time, time.D
113113
}
114114

115115
// Find the hour:minute (within the tolerance) scheduled check-in which contains the current time.
116-
current := now.Format("15:04")
117-
118116
for idx, hourminute := range times {
119-
low := hourminute.Add(-1 * tolerance).Format("15:04")
120-
high := hourminute.Add(tolerance).Format("15:04")
121-
122117
scheduledCheckIn := time.Date(now.Year(), now.Month(), now.Day(), hourminute.Hour(), hourminute.Minute(), 0, 0, now.Location())
123118

124-
// If we're before all scheduled check-ins today
125-
if idx == 0 && current < low {
126-
return scheduledCheckIn, scheduledCheckIn.Sub(now) + tolerance, nil
127-
}
119+
low := scheduledCheckIn.Add(-1 * tolerance)
120+
high := scheduledCheckIn.Add(tolerance)
128121

129122
// The current time must be within our scheduled time +/- the tolerance
130-
if low <= current && high >= current {
123+
if low.Before(now) && now.Before(high) {
131124
// Based on the scheduledCheckIn calculate how long to sleep for
132125
var nextCheckIn time.Time
133126
if len(times) > idx+1 {
@@ -145,9 +138,14 @@ func Calculate(now time.Time, schedule config.ScheduleConfig) (time.Time, time.D
145138

146139
return scheduledCheckIn, next.Sub(now) + tolerance, nil
147140
}
141+
142+
// We've reached the next possible check-in (the low is greater than now)
143+
if now.Before(low) {
144+
return scheduledCheckIn, high.Sub(now), nil
145+
}
148146
}
149147

150-
// Find the earliest time tomorrow
148+
// Find the earliest time tomorrow, since we were late to all of them.
151149
start := time.Date(now.Year(), now.Month(), now.Day(), times[0].Hour(), times[0].Minute(), 0, 0, now.Location())
152150
future := time.Date(now.Year(), now.Month(), now.Day(), start.Hour(), start.Minute(), 0, 0, now.Location())
153151

0 commit comments

Comments
 (0)