parser.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package cron
  2. import (
  3. "log"
  4. "math"
  5. "strconv"
  6. "strings"
  7. )
  8. // Returns a new crontab schedule representing the given spec.
  9. // Panics with a descriptive error if the spec is not valid.
  10. func Parse(spec string) *Schedule {
  11. if spec[0] == '@' {
  12. return parseDescriptor(spec)
  13. }
  14. // Split on whitespace. We require 4 or 5 fields.
  15. // (minute) (hour) (day of month) (month) (day of week, optional)
  16. fields := strings.Fields(spec)
  17. if len(fields) != 5 && len(fields) != 6 {
  18. log.Panicf("Expected 5 or 6 fields, found %d: %s", len(fields), spec)
  19. }
  20. // If a fifth field is not provided (DayOfWeek), then it is equivalent to star.
  21. if len(fields) == 5 {
  22. fields = append(fields, "*")
  23. }
  24. schedule := &Schedule{
  25. Second: getField(fields[0], seconds),
  26. Minute: getField(fields[1], minutes),
  27. Hour: getField(fields[2], hours),
  28. Dom: getField(fields[3], dom),
  29. Month: getField(fields[4], months),
  30. Dow: getField(fields[5], dow),
  31. }
  32. return schedule
  33. }
  34. // Return an Int with the bits set representing all of the times that the field represents.
  35. // A "field" is a comma-separated list of "ranges".
  36. func getField(field string, r bounds) uint64 {
  37. // list = range {"," range}
  38. var bits uint64
  39. ranges := strings.FieldsFunc(field, func(r rune) bool { return r == ',' })
  40. for _, expr := range ranges {
  41. bits |= getRange(expr, r)
  42. }
  43. return bits
  44. }
  45. func getRange(expr string, r bounds) uint64 {
  46. // number | number "-" number [ "/" number ]
  47. var (
  48. start, end, step uint
  49. rangeAndStep = strings.Split(expr, "/")
  50. lowAndHigh = strings.Split(rangeAndStep[0], "-")
  51. singleDigit = len(lowAndHigh) == 1
  52. )
  53. var extra_star uint64
  54. if lowAndHigh[0] == "*" || lowAndHigh[0] == "?" {
  55. start = r.min
  56. end = r.max
  57. extra_star = STAR_BIT
  58. } else {
  59. start = parseIntOrName(lowAndHigh[0], r.names)
  60. switch len(lowAndHigh) {
  61. case 1:
  62. end = start
  63. case 2:
  64. end = parseIntOrName(lowAndHigh[1], r.names)
  65. default:
  66. log.Panicf("Too many hyphens: %s", expr)
  67. }
  68. }
  69. switch len(rangeAndStep) {
  70. case 1:
  71. step = 1
  72. case 2:
  73. step = mustParseInt(rangeAndStep[1])
  74. // Special handling: "N/step" means "N-max/step".
  75. if singleDigit {
  76. end = r.max
  77. }
  78. default:
  79. log.Panicf("Too many slashes: %s", expr)
  80. }
  81. if start < r.min {
  82. log.Panicf("Beginning of range (%d) below minimum (%d): %s", start, r.min, expr)
  83. }
  84. if end > r.max {
  85. log.Panicf("End of range (%d) above maximum (%d): %s", end, r.max, expr)
  86. }
  87. if start > end {
  88. log.Panicf("Beginning of range (%d) beyond end of range (%d): %s", start, end, expr)
  89. }
  90. return getBits(start, end, step) | extra_star
  91. }
  92. func parseIntOrName(expr string, names map[string]uint) uint {
  93. if names != nil {
  94. if namedInt, ok := names[strings.ToLower(expr)]; ok {
  95. return namedInt
  96. }
  97. }
  98. return mustParseInt(expr)
  99. }
  100. func mustParseInt(expr string) uint {
  101. num, err := strconv.Atoi(expr)
  102. if err != nil {
  103. log.Panicf("Failed to parse int from %s: %s", expr, err)
  104. }
  105. if num < 0 {
  106. log.Panicf("Negative number (%d) not allowed: %s", num, expr)
  107. }
  108. return uint(num)
  109. }
  110. func getBits(min, max, step uint) uint64 {
  111. var bits uint64
  112. // If step is 1, use shifts.
  113. if step == 1 {
  114. return ^(math.MaxUint64 << (max + 1)) & (math.MaxUint64 << min)
  115. }
  116. // Else, use a simple loop.
  117. for i := min; i <= max; i += step {
  118. bits |= 1 << i
  119. }
  120. return bits
  121. }
  122. func all(r bounds) uint64 {
  123. return getBits(r.min, r.max, 1) | STAR_BIT
  124. }
  125. func first(r bounds) uint64 {
  126. return getBits(r.min, r.min, 1)
  127. }
  128. func parseDescriptor(spec string) *Schedule {
  129. switch spec {
  130. case "@yearly", "@annually":
  131. return &Schedule{
  132. Second: 1 << seconds.min,
  133. Minute: 1 << minutes.min,
  134. Hour: 1 << hours.min,
  135. Dom: 1 << dom.min,
  136. Month: 1 << months.min,
  137. Dow: all(dow),
  138. }
  139. case "@monthly":
  140. return &Schedule{
  141. Second: 1 << seconds.min,
  142. Minute: 1 << minutes.min,
  143. Hour: 1 << hours.min,
  144. Dom: 1 << dom.min,
  145. Month: all(months),
  146. Dow: all(dow),
  147. }
  148. case "@weekly":
  149. return &Schedule{
  150. Second: 1 << seconds.min,
  151. Minute: 1 << minutes.min,
  152. Hour: 1 << hours.min,
  153. Dom: all(dom),
  154. Month: all(months),
  155. Dow: 1 << dow.min,
  156. }
  157. case "@daily", "@midnight":
  158. return &Schedule{
  159. Second: 1 << seconds.min,
  160. Minute: 1 << minutes.min,
  161. Hour: 1 << hours.min,
  162. Dom: all(dom),
  163. Month: all(months),
  164. Dow: all(dow),
  165. }
  166. case "@hourly":
  167. return &Schedule{
  168. Second: 1 << seconds.min,
  169. Minute: 1 << minutes.min,
  170. Hour: all(hours),
  171. Dom: all(dom),
  172. Month: all(months),
  173. Dow: all(dow),
  174. }
  175. }
  176. log.Panicf("Unrecognized descriptor: %s", spec)
  177. return nil
  178. }