style(alerting): align quiet hours code and tests with Go style guide
ci / image (pull_request) Successful in 4m28s
ci / build (pull_request) Successful in 8m16s
ci / lint (pull_request) Successful in 12m11s
ci / test (pull_request) Successful in 15m40s

This commit is contained in:
Sebastian Unterschütz committed 2026-11-22 19:43:17 +01:00
1 parent ee3769f578
commit 1c31003baf
4 files changed
+162 -94

No files matched your search

+33 -25
View File
@@ -45,14 +45,14 @@ func validateQuietHours(in []quietHourRequest) ([]quietHourView, string) {
}
start := strings.TrimSpace(w.Start)
end := strings.TrimSpace(w.End)
sMin, okS := parseClockMinutes(start, false)
eMin, okE := parseClockMinutes(end, true)
if !okS || !okE {
startMin, startOK := parseClockMinutes(start)
endMin, endOK := parseEndMinutes(end)
if !startOK || !endOK {
return nil, "invalid_quiet_hours"
}
// Identical non-zero times would be an empty window; 00:00-00:00
// (like 00:00-24:00) means the whole day.
if sMin == eMin && sMin != 0 {
if startMin == endMin && startMin != 0 {
return nil, "invalid_quiet_hours"
}
days := make([]int, 0, len(w.Weekdays))
@@ -74,26 +74,33 @@ func validateQuietHours(in []quietHourRequest) ([]quietHourView, string) {
return out, ""
}
// parseClockMinutes parses "HH:MM" into minutes since midnight. When allow24
// is true, "24:00" is also accepted as 1440.
func parseClockMinutes(s string, allow24 bool) (int, bool) {
// parseClockMinutes parses "HH:MM" (00:00..23:59) into minutes since midnight.
func parseClockMinutes(s string) (int, bool) {
if len(s) != 5 || s[2] != ':' {
return 0, false
}
h, err1 := strconv.Atoi(s[:2])
m, err2 := strconv.Atoi(s[3:])
if err1 != nil || err2 != nil || m < 0 || m > 59 {
hour, err := strconv.Atoi(s[:2])
if err != nil || hour < 0 || hour > 23 {
return 0, false
}
if h == 24 && m == 0 && allow24 {
return 24 * 60, true
}
if h < 0 || h > 23 {
minute, err := strconv.Atoi(s[3:])
if err != nil || minute < 0 || minute > 59 {
return 0, false
}
return h*60 + m, true
return hour*60 + minute, true
}
// parseEndMinutes parses a window end time, accepting "24:00" in addition to
// 00:00..23:59.
func parseEndMinutes(s string) (int, bool) {
if s == "24:00" {
return 24 * 60, true
}
return parseClockMinutes(s)
}
// decodeQuietHours decodes stored JSONB quiet windows, always returning a
// non-nil slice so JSON responses serialize as [] rather than null.
func decodeQuietHours(raw []byte) []quietHourView {
if len(raw) == 0 {
return []quietHourView{}
@@ -112,29 +119,30 @@ func decodeQuietHours(raw []byte) []quietHourView {
// contains reports whether t falls inside w in w's time zone.
func (w quietHourView) contains(t time.Time) bool {
sMin, okS := parseClockMinutes(w.Start, false)
eMin, okE := parseClockMinutes(w.End, true)
if !okS || !okE {
startMin, startOK := parseClockMinutes(w.Start)
endMin, endOK := parseEndMinutes(w.End)
if !startOK || !endOK {
return false
}
lt := t.In(loadLocation(w.TimeZone))
nowMin := lt.Hour()*60 + lt.Minute()
today := int(lt.Weekday())
local := t.In(loadLocation(w.TimeZone))
nowMin := local.Hour()*60 + local.Minute()
today := int(local.Weekday())
hasDay := func(d int) bool {
return len(w.Weekdays) == 0 || slices.Contains(w.Weekdays, d)
}
if sMin == 0 && (eMin == 0 || eMin == 24*60) {
if startMin == 0 && (endMin == 0 || endMin == 24*60) {
return hasDay(today)
}
if sMin < eMin {
return hasDay(today) && nowMin >= sMin && nowMin < eMin
if startMin < endMin {
return hasDay(today) && nowMin >= startMin && nowMin < endMin
}
// Overnight window (Start > End), e.g. 22:00-07:00 or 18:00-00:00:
// covers [Start, 24:00) on matching days and [00:00, End) on the next day.
yesterday := (today + 6) % 7
return (hasDay(today) && nowMin >= sMin) || (hasDay(yesterday) && nowMin < eMin)
return (hasDay(today) && nowMin >= startMin) || (hasDay(yesterday) && nowMin < endMin)
}
// isQuietAt reports whether any window in windows suppresses notifications at t.
func isQuietAt(windows []quietHourView, t time.Time) bool {
for _, w := range windows {
if w.contains(t) {