From 2e52b54e257719d05beba00c6315ce8b63d4579d Mon Sep 17 00:00:00 2001 From: Adam Shannon Date: Fri, 18 Oct 2024 13:01:14 -0500 Subject: [PATCH] snooze: if we can't find a window to fit in, look for the next future time --- internal/provider/pd/client_test.go | 2 -- internal/provider/snooze/snooze.go | 20 +++++++++----------- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/internal/provider/pd/client_test.go b/internal/provider/pd/client_test.go index 3c5e545..08c96e7 100644 --- a/internal/provider/pd/client_test.go +++ b/internal/provider/pd/client_test.go @@ -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", }, diff --git a/internal/provider/snooze/snooze.go b/internal/provider/snooze/snooze.go index 495716a..4f8985a 100644 --- a/internal/provider/snooze/snooze.go +++ b/internal/provider/snooze/snooze.go @@ -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 { @@ -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())