parser.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package cron
  2. import (
  3. "log"
  4. "math"
  5. "strconv"
  6. "strings"
  7. )
  8. // Parse returns a new crontab schedule representing the given spec.
  9. // It 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. // getField returns an Int with the bits set representing all of the times that
  35. // the field represents. 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. // getRange returns the bits indicated by the given expression:
  46. // number | number "-" number [ "/" number ]
  47. func getRange(expr string, r bounds) uint64 {
  48. var (
  49. start, end, step uint
  50. rangeAndStep = strings.Split(expr, "/")
  51. lowAndHigh = strings.Split(rangeAndStep[0], "-")
  52. singleDigit = len(lowAndHigh) == 1
  53. )
  54. var extra_star uint64
  55. if lowAndHigh[0] == "*" || lowAndHigh[0] == "?" {
  56. start = r.min
  57. end = r.max
  58. extra_star = starBit
  59. } else {
  60. start = parseIntOrName(lowAndHigh[0], r.names)
  61. switch len(lowAndHigh) {
  62. case 1:
  63. end = start
  64. case 2:
  65. end = parseIntOrName(lowAndHigh[1], r.names)
  66. default:
  67. log.Panicf("Too many hyphens: %s", expr)
  68. }
  69. }
  70. switch len(rangeAndStep) {
  71. case 1:
  72. step = 1
  73. case 2:
  74. step = mustParseInt(rangeAndStep[1])
  75. // Special handling: "N/step" means "N-max/step".
  76. if singleDigit {
  77. end = r.max
  78. }
  79. default:
  80. log.Panicf("Too many slashes: %s", expr)
  81. }
  82. if start < r.min {
  83. log.Panicf("Beginning of range (%d) below minimum (%d): %s", start, r.min, expr)
  84. }
  85. if end > r.max {
  86. log.Panicf("End of range (%d) above maximum (%d): %s", end, r.max, expr)
  87. }
  88. if start > end {
  89. log.Panicf("Beginning of range (%d) beyond end of range (%d): %s", start, end, expr)
  90. }
  91. return getBits(start, end, step) | extra_star
  92. }
  93. // parseIntOrName returns the (possibly-named) integer contained in expr.
  94. func parseIntOrName(expr string, names map[string]uint) uint {
  95. if names != nil {
  96. if namedInt, ok := names[strings.ToLower(expr)]; ok {
  97. return namedInt
  98. }
  99. }
  100. return mustParseInt(expr)
  101. }
  102. // mustParseInt parses the given expression as an int or panics.
  103. func mustParseInt(expr string) uint {
  104. num, err := strconv.Atoi(expr)
  105. if err != nil {
  106. log.Panicf("Failed to parse int from %s: %s", expr, err)
  107. }
  108. if num < 0 {
  109. log.Panicf("Negative number (%d) not allowed: %s", num, expr)
  110. }
  111. return uint(num)
  112. }
  113. // getBits sets all bits in the range [min, max], modulo the given step size.
  114. func getBits(min, max, step uint) uint64 {
  115. var bits uint64
  116. // If step is 1, use shifts.
  117. if step == 1 {
  118. return ^(math.MaxUint64 << (max + 1)) & (math.MaxUint64 << min)
  119. }
  120. // Else, use a simple loop.
  121. for i := min; i <= max; i += step {
  122. bits |= 1 << i
  123. }
  124. return bits
  125. }
  126. // all returns all bits within the given bounds. (plus the star bit)
  127. func all(r bounds) uint64 {
  128. return getBits(r.min, r.max, 1) | starBit
  129. }
  130. // first returns bits with only the first (minimum) value set.
  131. func first(r bounds) uint64 {
  132. return getBits(r.min, r.min, 1)
  133. }
  134. // parseDescriptor returns a pre-defined schedule for the expression, or panics
  135. // if none matches.
  136. func parseDescriptor(spec string) *Schedule {
  137. switch spec {
  138. case "@yearly", "@annually":
  139. return &Schedule{
  140. Second: 1 << seconds.min,
  141. Minute: 1 << minutes.min,
  142. Hour: 1 << hours.min,
  143. Dom: 1 << dom.min,
  144. Month: 1 << months.min,
  145. Dow: all(dow),
  146. }
  147. case "@monthly":
  148. return &Schedule{
  149. Second: 1 << seconds.min,
  150. Minute: 1 << minutes.min,
  151. Hour: 1 << hours.min,
  152. Dom: 1 << dom.min,
  153. Month: all(months),
  154. Dow: all(dow),
  155. }
  156. case "@weekly":
  157. return &Schedule{
  158. Second: 1 << seconds.min,
  159. Minute: 1 << minutes.min,
  160. Hour: 1 << hours.min,
  161. Dom: all(dom),
  162. Month: all(months),
  163. Dow: 1 << dow.min,
  164. }
  165. case "@daily", "@midnight":
  166. return &Schedule{
  167. Second: 1 << seconds.min,
  168. Minute: 1 << minutes.min,
  169. Hour: 1 << hours.min,
  170. Dom: all(dom),
  171. Month: all(months),
  172. Dow: all(dow),
  173. }
  174. case "@hourly":
  175. return &Schedule{
  176. Second: 1 << seconds.min,
  177. Minute: 1 << minutes.min,
  178. Hour: all(hours),
  179. Dom: all(dom),
  180. Month: all(months),
  181. Dow: all(dow),
  182. }
  183. }
  184. log.Panicf("Unrecognized descriptor: %s", spec)
  185. return nil
  186. }