diff --git a/config/maintenance/maintenance.go b/config/maintenance/maintenance.go index d34f6a99..c5c12660 100644 --- a/config/maintenance/maintenance.go +++ b/config/maintenance/maintenance.go @@ -95,10 +95,15 @@ func (c Config) IsUnderMaintenance() bool { return false } now := time.Now().UTC() - dayWhereMaintenancePeriodWouldStart := now.Add(-c.Duration).Truncate(24 * time.Hour) + var dayWhereMaintenancePeriodWouldStart time.Time + if now.Hour() >= int(c.durationToStartFromMidnight.Hours()) { + dayWhereMaintenancePeriodWouldStart = now.Truncate(24 * time.Hour) + } else { + dayWhereMaintenancePeriodWouldStart = now.Add(-c.Duration).Truncate(24 * time.Hour) + } hasMaintenanceEveryDay := len(c.Every) == 0 - hasMaintenancePeriodScheduledForThatWeekday := c.hasDay(dayWhereMaintenancePeriodWouldStart.Weekday().String()) - if !hasMaintenanceEveryDay && !hasMaintenancePeriodScheduledForThatWeekday { + hasMaintenancePeriodScheduledToStartOnThatWeekday := c.hasDay(dayWhereMaintenancePeriodWouldStart.Weekday().String()) + if !hasMaintenanceEveryDay && !hasMaintenancePeriodScheduledToStartOnThatWeekday { // The day when the maintenance period would start is not scheduled // to have any maintenance, so we can just return false. return false diff --git a/config/maintenance/maintenance_test.go b/config/maintenance/maintenance_test.go index d90aabc9..b8cabfd1 100644 --- a/config/maintenance/maintenance_test.go +++ b/config/maintenance/maintenance_test.go @@ -153,7 +153,7 @@ func TestConfig_IsUnderMaintenance(t *testing.T) { expected: true, }, { - name: "under-maintenance", + name: "under-maintenance-starting-now-for-2h", cfg: &Config{ Start: fmt.Sprintf("%02d:00", now.Hour()), Duration: 2 * time.Hour, @@ -161,9 +161,33 @@ func TestConfig_IsUnderMaintenance(t *testing.T) { expected: true, }, { - name: "not-under-maintenance", + name: "under-maintenance-starting-now-for-8h", cfg: &Config{ - Start: fmt.Sprintf("%02d:00", now.Add(-5*time.Hour).Hour()), + Start: fmt.Sprintf("%02d:00", now.Hour()), + Duration: 8 * time.Hour, + }, + expected: true, + }, + { + name: "under-maintenance-starting-4h-ago-for-8h", + cfg: &Config{ + Start: fmt.Sprintf("%02d:00", now.Hour()-4), + Duration: 8 * time.Hour, + }, + expected: true, + }, + { + name: "under-maintenance-starting-4h-ago-for-3h", + cfg: &Config{ + Start: fmt.Sprintf("%02d:00", now.Hour()-4), + Duration: 3 * time.Hour, + }, + expected: false, + }, + { + name: "under-maintenance-starting-5h-ago-for-1h", + cfg: &Config{ + Start: fmt.Sprintf("%02d:00", now.Hour()-5), Duration: time.Hour, }, expected: false,