1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
package gronx
import (
"errors"
"fmt"
"strconv"
"strings"
"time"
)
func inStep(val int, s string, bounds []int) (bool, error) {
parts := strings.Split(s, "/")
step, err := strconv.Atoi(parts[1])
if err != nil {
return false, err
}
if step <= 0 {
return false, errors.New("step can't be 0")
}
if strings.Index(s, "*/") == 0 {
return (val-bounds[0])%step == 0, nil
}
if strings.Index(s, "0/") == 0 {
return val%step == 0, nil
}
sub, end := strings.Split(parts[0], "-"), val
start, err := strconv.Atoi(sub[0])
if err != nil {
return false, err
}
if len(sub) > 1 {
end, err = strconv.Atoi(sub[1])
if err != nil {
return false, err
}
}
if (len(sub) > 1 && end < start) || start < bounds[0] || end > bounds[1] {
return false, fmt.Errorf("step '%s' out of bounds(%d, %d)", parts[0], bounds[0], bounds[1])
}
return inStepRange(val, start, end, step), nil
}
func inRange(val int, s string, bounds []int) (bool, error) {
parts := strings.Split(s, "-")
start, err := strconv.Atoi(parts[0])
if err != nil {
return false, err
}
end, err := strconv.Atoi(parts[1])
if err != nil {
return false, err
}
if end < start || start < bounds[0] || end > bounds[1] {
return false, fmt.Errorf("range '%s' out of bounds(%d, %d)", s, bounds[0], bounds[1])
}
return start <= val && val <= end, nil
}
func inStepRange(val, start, end, step int) bool {
for i := start; i <= end && i <= val; i += step {
if i == val {
return true
}
}
return false
}
func isValidMonthDay(val string, last int, ref time.Time) (valid bool, err error) {
day, loc := ref.Day(), ref.Location()
if val == "L" {
return day == last, nil
}
pos := strings.Index(val, "W")
if pos < 1 {
return false, errors.New("invalid offset value: " + val)
}
nval, err := strconv.Atoi(val[0:pos])
if err != nil {
return false, err
}
for _, i := range []int{0, -1, 1, -2, 2} {
incr := i + nval
if incr > 0 && incr <= last {
iref := time.Date(ref.Year(), ref.Month(), incr, ref.Hour(), ref.Minute(), ref.Second(), 0, loc)
week := int(iref.Weekday())
if week > 0 && week < 6 && iref.Month() == ref.Month() {
valid = day == iref.Day()
break
}
}
}
return valid, nil
}
func isValidWeekDay(val string, last int, ref time.Time) (bool, error) {
loc := ref.Location()
if pos := strings.Index(val, "L"); pos > 0 {
nval, err := strconv.Atoi(val[0:pos])
if err != nil {
return false, err
}
for i := 0; i < 7; i++ {
day := last - i
dref := time.Date(ref.Year(), ref.Month(), day, ref.Hour(), ref.Minute(), ref.Second(), 0, loc)
if int(dref.Weekday()) == nval%7 {
return ref.Day() == day, nil
}
}
}
pos := strings.Index(val, "#")
parts := strings.Split(strings.ReplaceAll(val, "7#", "0#"), "#")
if pos < 1 || len(parts) < 2 {
return false, errors.New("invalid offset value: " + val)
}
day, err := strconv.Atoi(parts[0])
if err != nil {
return false, err
}
nth, err := strconv.Atoi(parts[1])
if err != nil {
return false, err
}
if day < 0 || day > 7 || nth < 1 || nth > 5 || int(ref.Weekday()) != day {
return false, nil
}
return (ref.Day()-1)/7 == nth-1, nil
}
|