mirror of
https://github.com/TwiN/gatus.git
synced 2025-03-03 17:41:37 +01:00
fix(maintenance): timezone handling offset issue (#981)
* fix: uses time.Date to construct time at midnight in Timezone instead of Truncate * test: check for Perth validity using adjusted timezone handling * chore: remove newlines around day adjustment block * chore: remove separate define of dayWhereMaintenancePeriodWouldStart
This commit is contained in:
parent
dd839be918
commit
975ac3592e
@ -110,12 +110,13 @@ func (c *Config) IsUnderMaintenance() bool {
|
||||
if c.TimezoneLocation != nil {
|
||||
now = now.In(c.TimezoneLocation)
|
||||
}
|
||||
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)
|
||||
adjustedDate := now.Day()
|
||||
if now.Hour() < int(c.durationToStartFromMidnight.Hours()) {
|
||||
// if time in maintenance window is later than now, treat it as yesterday
|
||||
adjustedDate--
|
||||
}
|
||||
// Set to midnight prior to adding duration
|
||||
dayWhereMaintenancePeriodWouldStart := time.Date(now.Year(), now.Month(), adjustedDate, 0, 0, 0, 0, now.Location())
|
||||
hasMaintenanceEveryDay := len(c.Every) == 0
|
||||
hasMaintenancePeriodScheduledToStartOnThatWeekday := c.hasDay(dayWhereMaintenancePeriodWouldStart.Weekday().String())
|
||||
if !hasMaintenanceEveryDay && !hasMaintenancePeriodScheduledToStartOnThatWeekday {
|
||||
|
@ -264,6 +264,15 @@ func TestConfig_IsUnderMaintenance(t *testing.T) {
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "under-maintenance-perth-timezone-starting-now-for-2h",
|
||||
cfg: &Config{
|
||||
Start: fmt.Sprintf("%02d:00", inTimezone(now, "Australia/Perth", t).Hour()),
|
||||
Duration: 2 * time.Hour,
|
||||
Timezone: "Australia/Perth",
|
||||
},
|
||||
expected: true,
|
||||
},
|
||||
{
|
||||
name: "under-maintenance-utc-timezone-starting-now-for-2h",
|
||||
cfg: &Config{
|
||||
@ -340,3 +349,12 @@ func normalizeHour(hour int) int {
|
||||
}
|
||||
return hour
|
||||
}
|
||||
|
||||
func inTimezone(passedTime time.Time, timezone string, t *testing.T) time.Time {
|
||||
timezoneLocation, err := time.LoadLocation(timezone)
|
||||
|
||||
if err != nil {
|
||||
t.Fatalf("timezone %s did not load", timezone)
|
||||
}
|
||||
return passedTime.In(timezoneLocation)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user