Skip to content

Commit

Permalink
snooze: account for banking days inside intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Oct 28, 2024
1 parent c997f5b commit 7015279
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 3 deletions.
25 changes: 22 additions & 3 deletions internal/provider/snooze/snooze.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,22 @@ func Calculate(now time.Time, schedule config.ScheduleConfig) (time.Time, time.D
next = next.AddDate(0, 0, nextCheckIn.Day()-1)
}

return scheduledCheckIn, next.Sub(now) + tolerance, nil
// Check if the time after snoozing will be a banking day
snooze := next.Sub(now) + tolerance
if schedule.BankingDays != nil {
snooze = snoozeUntilNextBankingDay(scheduledCheckIn, snooze)
}
return scheduledCheckIn, snooze, nil
}

// We've reached the next possible check-in (the low is greater than now)
// We couldn't find an interval to check-in, so return the time until the first available check-in.
// This is useful for the initial snooze on startup
if now.Before(low) {
return scheduledCheckIn, high.Sub(now), nil
snooze := high.Sub(now)
if schedule.BankingDays != nil {
snooze = snoozeUntilNextBankingDay(scheduledCheckIn, snooze)
}
return scheduledCheckIn, snooze, nil
}
}

Expand Down Expand Up @@ -169,3 +179,12 @@ func Calculate(now time.Time, schedule config.ScheduleConfig) (time.Time, time.D

return time.Time{}, time.Second, nil
}

func snoozeUntilNextBankingDay(scheduledCheckIn time.Time, snooze time.Duration) time.Duration {
bt := base.NewTime(scheduledCheckIn.Add(snooze))
if !bt.IsBankingDay() {
bt = bt.AddBankingDay(1)
snooze = bt.Time.Sub(scheduledCheckIn)
}
return snooze
}
36 changes: 36 additions & 0 deletions internal/provider/snooze/snooze_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,42 @@ func TestSnooze_BankingDays(t *testing.T) {
require.Equal(t, "95h38m55s", snooze.String())
require.Equal(t, "2024-10-15T09:05:00-04:00", now.In(nyc).Add(snooze).Format(time.RFC3339))
})

t.Run("last checkin before weekend", func(t *testing.T) {
now := time.Date(2024, time.October, 25, 17, 30, 5, 0, nyc)

var schedule config.ScheduleConfig
schedule.BankingDays = &config.PartialDay{
Timezone: "America/New_York",
Times: []string{"10:00", "14:15", "16:15", "17:30"},
Tolerance: "5m",
}

clockTime, snooze, err := Calculate(now, schedule)
require.NoError(t, err)

require.Equal(t, "2024-10-25T17:30:00-04:00", clockTime.Format(time.RFC3339))
require.Equal(t, "64h34m55s", snooze.String())
require.Equal(t, "2024-10-28T10:05:00-04:00", now.In(nyc).Add(snooze).Format(time.RFC3339))
})

t.Run("weekend startup, early to check-in", func(t *testing.T) {
now := time.Date(2024, time.October, 26, 13, 22, 5, 0, time.UTC)

var schedule config.ScheduleConfig
schedule.BankingDays = &config.PartialDay{
Timezone: "America/New_York",
Times: []string{"14:00", "15:00", "17:00"},
Tolerance: "5m",
}

clockTime, snooze, err := Calculate(now, schedule)
require.NoError(t, err)

require.Equal(t, "2024-10-26T14:00:00-04:00", clockTime.Format(time.RFC3339))
require.Equal(t, "52h42m55s", snooze.String())
require.Equal(t, "2024-10-28T14:05:00-04:00", now.In(nyc).Add(snooze).Format(time.RFC3339))
})
}

func TestSnooze_Close(t *testing.T) {
Expand Down

0 comments on commit 7015279

Please sign in to comment.