package ntfy import ( "slices" "time" ) // Clock is a time of day. type Clock struct { Hour, Minute int } // Quiet are quiet hours: a window each day in which messages below // MinPriority are held for a digest, or dropped. type Quiet struct { // Start and End are the window's times of day in Location. An End // at or before Start ends the window on the next day. Start, End Clock // Days are the days a window starts on. Days []time.Weekday // Location is the zone of Start and End; nil means local time. Location *time.Location // MinPriority is the lowest priority that prints during the window. MinPriority int // Drop leaves out the messages held back, which otherwise print on // one digest when the window ends. Drop bool } // window returns the end of the quiet window t falls in, and whether it // falls in one. The window's bounds are the instants the zone's clocks // show its times of day (wallTime), so a window that crosses midnight or // a change of clocks keeps them: 22:00 to 07:00 is nine hours on most // nights, eight on the night clocks go forward. A bound the clocks skip is // the first instant after the gap. func (q *Quiet) window(t time.Time) (end time.Time, in bool) { if q == nil { return time.Time{}, false } loc := q.Location if loc == nil { loc = time.Local } t = t.In(loc) // A window that holds t started on t's day or on the day before. The // day is taken at noon: in a zone whose clocks go forward at midnight, // midnight does not exist, and time.Date moves it by the gap, which // can put it on the day before. for back := 0; back <= 1; back++ { day := time.Date(t.Year(), t.Month(), t.Day()-back, 12, 0, 0, 0, loc) if !slices.Contains(q.Days, day.Weekday()) { continue } start, end := q.bounds(day, loc) if !t.Before(start) && t.Before(end) { return end, true } } return time.Time{}, false } // bounds returns the start and end of the window that starts on day. func (q *Quiet) bounds(day time.Time, loc *time.Location) (start, end time.Time) { y, m, d := day.Date() start = wallTime(y, m, d, q.Start, loc) endDay := d if q.End.Hour*60+q.End.Minute <= q.Start.Hour*60+q.Start.Minute { endDay++ } return start, wallTime(y, m, endDay, q.End, loc) } // wallTime returns the instant the clocks in loc show c on the given day, // which may be one past the end of a month. A time the clocks skip, when // they go forward, is the first instant after the gap; time.Date moves it // by the gap's length, to before the gap or after it depending on the // zone (west of UTC, a skipped midnight lands on the evening before). A // time the clocks show twice is the one time.Date gives. func wallTime(y int, m time.Month, d int, c Clock, loc *time.Location) time.Time { y, m, d = time.Date(y, m, d, 12, 0, 0, 0, loc).Date() t := time.Date(y, m, d, c.Hour, c.Minute, 0, 0, loc) asked := time.Date(y, m, d, c.Hour, c.Minute, 0, 0, time.UTC) shown := time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, time.UTC) start, end := t.ZoneBounds() switch { case shown.Equal(asked): return t case shown.Before(asked): // t is before the gap: the gap ends where t's zone period ends. return end default: // t is after the gap: the gap ends where t's zone period starts. return start } }