IsWithinRange interprets a cron spec as a continuous time range, and returns whether the provided time value falls within that range. For example, the expression "* 9-18 * * 1-5" represents a continuous time range from 09:00:00 to 18:59:59, Monday through Friday.
(t time.Time)
| 184 | // For example, the expression "* 9-18 * * 1-5" represents a continuous time range |
| 185 | // from 09:00:00 to 18:59:59, Monday through Friday. |
| 186 | func (s Schedule) IsWithinRange(t time.Time) bool { |
| 187 | // Truncate to the beginning of the current minute. |
| 188 | currentMinute := t.Truncate(time.Minute) |
| 189 | |
| 190 | // Go back 1 second from the current minute to find what the next scheduled time would be. |
| 191 | justBefore := currentMinute.Add(-time.Second) |
| 192 | next := s.Next(justBefore) |
| 193 | |
| 194 | // If the next scheduled time is exactly at the current minute, |
| 195 | // then we are within the range. |
| 196 | return next.Equal(currentMinute) |
| 197 | } |
| 198 | |
| 199 | var ( |
| 200 | t0 = time.Date(1970, 1, 1, 1, 1, 1, 0, time.UTC) |