spec.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. package cron
  2. import "time"
  3. // SpecSchedule specifies a duty cycle (to the second granularity), based on a
  4. // traditional crontab specification. It is computed initially and stored as bit sets.
  5. type SpecSchedule struct {
  6. Second, Minute, Hour, Dom, Month, Dow uint64
  7. // Override location for this schedule.
  8. Location *time.Location
  9. }
  10. // bounds provides a range of acceptable values (plus a map of name to value).
  11. type bounds struct {
  12. min, max uint
  13. names map[string]uint
  14. }
  15. // The bounds for each field.
  16. var (
  17. seconds = bounds{0, 59, nil}
  18. minutes = bounds{0, 59, nil}
  19. hours = bounds{0, 23, nil}
  20. dom = bounds{1, 31, nil}
  21. months = bounds{1, 12, map[string]uint{
  22. "jan": 1,
  23. "feb": 2,
  24. "mar": 3,
  25. "apr": 4,
  26. "may": 5,
  27. "jun": 6,
  28. "jul": 7,
  29. "aug": 8,
  30. "sep": 9,
  31. "oct": 10,
  32. "nov": 11,
  33. "dec": 12,
  34. }}
  35. dow = bounds{0, 6, map[string]uint{
  36. "sun": 0,
  37. "mon": 1,
  38. "tue": 2,
  39. "wed": 3,
  40. "thu": 4,
  41. "fri": 5,
  42. "sat": 6,
  43. }}
  44. )
  45. const (
  46. // Set the top bit if a star was included in the expression.
  47. starBit = 1 << 63
  48. )
  49. // Next returns the next time this schedule is activated, greater than the given
  50. // time. If no time can be found to satisfy the schedule, return the zero time.
  51. func (s *SpecSchedule) Next(t time.Time) time.Time {
  52. // General approach:
  53. // For Month, Day, Hour, Minute, Second:
  54. // Check if the time value matches. If yes, continue to the next field.
  55. // If the field doesn't match the schedule, then increment the field until it matches.
  56. // While incrementing the field, a wrap-around brings it back to the beginning
  57. // of the field list (since it is necessary to re-verify previous field
  58. // values)
  59. // Convert the given time into the schedule's timezone, if one is specified.
  60. // Save the original timezone so we can convert back after we find a time.
  61. // Note that schedules without a time zone specified (time.Local) are treated
  62. // as local to the time provided.
  63. origLocation := t.Location()
  64. loc := s.Location
  65. if loc == time.Local {
  66. loc = t.Location()
  67. }
  68. if s.Location != time.Local {
  69. t = t.In(s.Location)
  70. }
  71. // Start at the earliest possible time (the upcoming second).
  72. t = t.Add(1*time.Second - time.Duration(t.Nanosecond())*time.Nanosecond)
  73. // This flag indicates whether a field has been incremented.
  74. added := false
  75. // If no time is found within five years, return zero.
  76. yearLimit := t.Year() + 5
  77. WRAP:
  78. if t.Year() > yearLimit {
  79. return time.Time{}
  80. }
  81. // Find the first applicable month.
  82. // If it's this month, then do nothing.
  83. for 1<<uint(t.Month())&s.Month == 0 {
  84. // If we have to add a month, reset the other parts to 0.
  85. if !added {
  86. added = true
  87. // Otherwise, set the date at the beginning (since the current time is irrelevant).
  88. t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, loc)
  89. }
  90. t = t.AddDate(0, 1, 0)
  91. // Wrapped around.
  92. if t.Month() == time.January {
  93. goto WRAP
  94. }
  95. }
  96. // Now get a day in that month.
  97. for !dayMatches(s, t) {
  98. if !added {
  99. added = true
  100. t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, loc)
  101. }
  102. t = t.AddDate(0, 0, 1)
  103. if t.Day() == 1 {
  104. goto WRAP
  105. }
  106. }
  107. for 1<<uint(t.Hour())&s.Hour == 0 {
  108. if !added {
  109. added = true
  110. t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, loc)
  111. }
  112. t = t.Add(1 * time.Hour)
  113. if t.Hour() == 0 {
  114. goto WRAP
  115. }
  116. }
  117. for 1<<uint(t.Minute())&s.Minute == 0 {
  118. if !added {
  119. added = true
  120. t = t.Truncate(time.Minute)
  121. }
  122. t = t.Add(1 * time.Minute)
  123. if t.Minute() == 0 {
  124. goto WRAP
  125. }
  126. }
  127. for 1<<uint(t.Second())&s.Second == 0 {
  128. if !added {
  129. added = true
  130. t = t.Truncate(time.Second)
  131. }
  132. t = t.Add(1 * time.Second)
  133. if t.Second() == 0 {
  134. goto WRAP
  135. }
  136. }
  137. return t.In(origLocation)
  138. }
  139. // dayMatches returns true if the schedule's day-of-week and day-of-month
  140. // restrictions are satisfied by the given time.
  141. func dayMatches(s *SpecSchedule, t time.Time) bool {
  142. var (
  143. domMatch bool = 1<<uint(t.Day())&s.Dom > 0
  144. dowMatch bool = 1<<uint(t.Weekday())&s.Dow > 0
  145. )
  146. if s.Dom&starBit > 0 || s.Dow&starBit > 0 {
  147. return domMatch && dowMatch
  148. }
  149. return domMatch || dowMatch
  150. }