spec.go 3.9 KB

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