MCPcopy Index your code
hub / github.com/CodisLabs/codis / Parse

Function Parse

pkg/utils/timesize/timesize.go:75–106  ·  view source on GitHub ↗
(s string)

Source from the content-addressed store, hash-verified

73var ErrBadTimeSize = errors.New("invalid timesize")
74
75func Parse(s string) (time.Duration, error) {
76 if !fullRegexp.MatchString(s) {
77 return 0, errors.Trace(ErrBadTimeSize)
78 }
79
80 subs := fullRegexp.FindStringSubmatch(s)
81 if len(subs) != 3 {
82 return 0, errors.Trace(ErrBadTimeSize)
83 }
84
85 text := subs[1]
86 unit := subs[2]
87
88 switch {
89 case unit != "":
90 return time.ParseDuration(text + unit)
91 case digitsOnly.MatchString(text):
92 n, err := strconv.ParseInt(text, 10, 64)
93 if err != nil {
94 return 0, errors.Trace(ErrBadTimeSize)
95 }
96 n *= int64(time.Second)
97 return time.Duration(n), nil
98 default:
99 n, err := strconv.ParseFloat(text, 64)
100 if err != nil {
101 return 0, errors.Trace(ErrBadTimeSize)
102 }
103 n *= float64(time.Second)
104 return time.Duration(n), nil
105 }
106}
107
108func MustParse(s string) time.Duration {
109 v, err := Parse(s)

Callers 2

UnmarshalTextMethod · 0.70
MustParseFunction · 0.70

Calls 1

DurationMethod · 0.80

Tested by

no test coverage detected