package api import ( "encoding/json" "slices" "strconv" "strings" "time" ) // quietHourRequest is one recurring time window during which an alert group // suppresses notifications. type quietHourRequest struct { // Weekdays are days of the week (0 = Sunday .. 6 = Saturday); empty // means every day. Weekdays flexInts `json:"weekdays" binding:"max=7"` // Start and End are wall-clock times "HH:MM" in TimeZone. End may be // "24:00"; Start > End wraps overnight into the next day, and // "00:00" to "00:00" (or "24:00") covers the entire day. Start string `json:"start" binding:"required,max=5"` End string `json:"end" binding:"required,max=5"` TimeZone string `json:"time_zone" binding:"required,max=64"` } // quietHourView is one normalized quiet window returned by the API and stored // on an alert group. type quietHourView struct { Weekdays []int `json:"weekdays"` Start string `json:"start"` End string `json:"end"` TimeZone string `json:"time_zone"` } // validateQuietHours validates and normalizes quiet windows, returning an // error code on failure. func validateQuietHours(in []quietHourRequest) ([]quietHourView, string) { out := make([]quietHourView, 0, len(in)) for _, w := range in { tz := strings.TrimSpace(w.TimeZone) if tz == "" || tz == "Local" { return nil, "invalid_time_zone" } if _, err := time.LoadLocation(tz); err != nil { return nil, "invalid_time_zone" } start := strings.TrimSpace(w.Start) end := strings.TrimSpace(w.End) 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 startMin == endMin && startMin != 0 { return nil, "invalid_quiet_hours" } days := make([]int, 0, len(w.Weekdays)) for _, d := range w.Weekdays { if d < 0 || d > 6 { return nil, "invalid_quiet_hours" } days = append(days, d) } slices.Sort(days) days = slices.Compact(days) out = append(out, quietHourView{ Weekdays: days, Start: start, End: end, TimeZone: tz, }) } return out, "" } // 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 } hour, err := strconv.Atoi(s[:2]) if err != nil || hour < 0 || hour > 23 { return 0, false } minute, err := strconv.Atoi(s[3:]) if err != nil || minute < 0 || minute > 59 { return 0, false } 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{} } var out []quietHourView if err := json.Unmarshal(raw, &out); err != nil || out == nil { return []quietHourView{} } for i := range out { if out[i].Weekdays == nil { out[i].Weekdays = []int{} } } return out } // contains reports whether t falls inside w in w's time zone. func (w quietHourView) contains(t time.Time) bool { startMin, startOK := parseClockMinutes(w.Start) endMin, endOK := parseEndMinutes(w.End) if !startOK || !endOK { return false } 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 startMin == 0 && (endMin == 0 || endMin == 24*60) { return hasDay(today) } 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 >= 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) { return true } } return false } func isQuietRawAt(raw []byte, t time.Time) bool { return isQuietAt(decodeQuietHours(raw), t) }