spec.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. }
  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. // Start at the earliest possible time (the upcoming second).
  60. t = t.Add(1*time.Second - time.Duration(t.Nanosecond())*time.Nanosecond)
  61. // This flag indicates whether a field has been incremented.
  62. added := false
  63. // If no time is found within five years, return zero.
  64. yearLimit := t.Year() + 5
  65. WRAP:
  66. if t.Year() > yearLimit {
  67. return time.Time{}
  68. }
  69. // Find the first applicable month.
  70. // If it's this month, then do nothing.
  71. for 1<<uint(t.Month())&s.Month == 0 {
  72. // If we have to add a month, reset the other parts to 0.
  73. if !added {
  74. added = true
  75. // Otherwise, set the date at the beginning (since the current time is irrelevant).
  76. t = time.Date(t.Year(), t.Month(), 1, 0, 0, 0, 0, t.Location())
  77. }
  78. t = t.AddDate(0, 1, 0)
  79. // Wrapped around.
  80. if t.Month() == time.January {
  81. goto WRAP
  82. }
  83. }
  84. // Now get a day in that month.
  85. for !dayMatches(s, t) {
  86. if !added {
  87. added = true
  88. t = time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, t.Location())
  89. }
  90. t = t.AddDate(0, 0, 1)
  91. if t.Day() == 1 {
  92. goto WRAP
  93. }
  94. }
  95. for 1<<uint(t.Hour())&s.Hour == 0 {
  96. if !added {
  97. added = true
  98. t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), 0, 0, 0, t.Location())
  99. }
  100. t = t.Add(1 * time.Hour)
  101. if t.Hour() == 0 {
  102. goto WRAP
  103. }
  104. }
  105. for 1<<uint(t.Minute())&s.Minute == 0 {
  106. if !added {
  107. added = true
  108. t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), 0, 0, t.Location())
  109. }
  110. t = t.Add(1 * time.Minute)
  111. if t.Minute() == 0 {
  112. goto WRAP
  113. }
  114. }
  115. for 1<<uint(t.Second())&s.Second == 0 {
  116. if !added {
  117. added = true
  118. t = time.Date(t.Year(), t.Month(), t.Day(), t.Hour(), t.Minute(), t.Second(), 0, t.Location())
  119. }
  120. t = t.Add(1 * time.Second)
  121. if t.Second() == 0 {
  122. goto WRAP
  123. }
  124. }
  125. return t
  126. }
  127. // dayMatches returns true if the schedule's day-of-week and day-of-month
  128. // restrictions are satisfied by the given time.
  129. func dayMatches(s *SpecSchedule, t time.Time) bool {
  130. var (
  131. domMatch bool = 1<<uint(t.Day())&s.Dom > 0
  132. dowMatch bool = 1<<uint(t.Weekday())&s.Dow > 0
  133. )
  134. if s.Dom&starBit > 0 || s.Dow&starBit > 0 {
  135. return domMatch && dowMatch
  136. }
  137. return domMatch || dowMatch
  138. }