github.com/go-graphite/carbonapi@v0.17.0/pkg/parser/interval.go (about) 1 package parser 2 3 import ( 4 "strconv" 5 "strings" 6 ) 7 8 // IntervalString converts a sign and string into a number of seconds 9 func IntervalString(s string, defaultSign int) (int32, error) { 10 if len(s) == 0 { 11 return 0, ErrUnknownTimeUnits 12 } 13 14 if s == "-" || s == "+" { 15 return 0, ErrUnknownTimeUnits 16 } 17 18 sign := defaultSign 19 20 switch s[0] { 21 case '-': 22 sign = -1 23 s = s[1:] 24 case '+': 25 sign = 1 26 s = s[1:] 27 } 28 29 var totalInterval int32 30 for len(s) > 0 { 31 var j int 32 for j < len(s) && '0' <= s[j] && s[j] <= '9' { 33 j++ 34 } 35 var offsetStr string 36 offsetStr, s = s[:j], s[j:] 37 38 j = 0 39 for j < len(s) && (s[j] < '0' || '9' < s[j]) { 40 j++ 41 } 42 var unitStr string 43 unitStr, s = s[:j], s[j:] 44 45 var units int 46 switch strings.ToLower(unitStr) { 47 case "s", "sec", "secs", "second", "seconds": 48 units = 1 49 case "m", "min", "mins", "minute", "minutes": 50 units = 60 51 case "h", "hour", "hours": 52 units = 60 * 60 53 case "d", "day", "days": 54 units = 24 * 60 * 60 55 case "w", "week", "weeks": 56 units = 7 * 24 * 60 * 60 57 case "mon", "month", "months": 58 units = 30 * 24 * 60 * 60 59 case "y", "year", "years": 60 units = 365 * 24 * 60 * 60 61 default: 62 return 0, ErrUnknownTimeUnits 63 } 64 65 offset, err := strconv.Atoi(offsetStr) 66 if err != nil { 67 return 0, err 68 } 69 totalInterval += int32(sign * offset * units) 70 } 71 72 return totalInterval, nil 73 } 74 75 func TruthyBool(s string) bool { 76 switch s { 77 case "", "0", "false", "False", "no", "No": 78 return false 79 case "1", "true", "True", "yes", "Yes": 80 return true 81 } 82 return false 83 }