Skip to content

Commit

Permalink
snooze: if we can't find a window to fit in, look for the next future…
Browse files Browse the repository at this point in the history
… time
  • Loading branch information
adamdecaf committed Oct 18, 2024
1 parent 74ffac5 commit 2e52b54
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
2 changes: 0 additions & 2 deletions internal/provider/pd/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ func TestClient_RejectedLateCheckIn(t *testing.T) {
Times: []string{
// Never allow the current time to check-in
now.Add(-1*time.Hour - 30*time.Minute).Format("15:04"),
// Add a future time that's too far in the future
now.Add(2*time.Hour + 30*time.Minute).Format("15:04"),
},
Tolerance: "5m",
},
Expand Down
20 changes: 9 additions & 11 deletions internal/provider/snooze/snooze.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,21 +113,14 @@ func Calculate(now time.Time, schedule config.ScheduleConfig) (time.Time, time.D
}

// Find the hour:minute (within the tolerance) scheduled check-in which contains the current time.
current := now.Format("15:04")

for idx, hourminute := range times {
low := hourminute.Add(-1 * tolerance).Format("15:04")
high := hourminute.Add(tolerance).Format("15:04")

scheduledCheckIn := time.Date(now.Year(), now.Month(), now.Day(), hourminute.Hour(), hourminute.Minute(), 0, 0, now.Location())

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

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

return scheduledCheckIn, next.Sub(now) + tolerance, nil
}

// We've reached the next possible check-in (the low is greater than now)
if now.Before(low) {
return scheduledCheckIn, high.Sub(now), nil
}
}

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

Expand Down

0 comments on commit 2e52b54

Please sign in to comment.