INNER CODE UNIT · Go

randomTimeInRange

asobti/kube-monkey · internal/pkg/calendar/calendar.go:137

func randomTimeInRange(now time.Time, r *rand.Rand, startHour int, endHour int, loc *time.Location) (time.Time, bool) {
	start, remaining, _ := rangeToday(now, startHour, endHour, loc)
	if remaining == 0 {
		return time.Time{}, false
	}

	return start.Add(time.Duration(r.Int63n(int64(remaining)))), true
}

// rangeToday returns the part of today's range that is still ahead of now, plus
// the length of the whole range. The remaining length is zero when the range
// has passed or when startHour and endHour do not describe a real range.
func rangeToday(now time.Time, startHour int, endHour int, loc *time.Location) (start time.Time, remaining time.Duration, full time.Duration) {
	now = now.In(loc)
	year, month, date := now.Date()
	start = time.Date(year, month, date, startHour, 0, 0, 0, loc)
	end := time.Date(year, month, date, endHour, 0, 0, 0, loc)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…