spec.go 3.7 KB

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