2016-07-11 17:46:23 +00:00
|
|
|
package duration
|
|
|
|
|
|
|
|
import (
|
2017-02-27 19:42:00 +00:00
|
|
|
"errors"
|
2016-07-11 17:46:23 +00:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-02-27 19:42:00 +00:00
|
|
|
func ParseDurationSecond(in interface{}) (time.Duration, error) {
|
2016-07-11 18:19:35 +00:00
|
|
|
var dur time.Duration
|
2017-02-27 19:42:00 +00:00
|
|
|
switch in.(type) {
|
|
|
|
case string:
|
|
|
|
inp := in.(string)
|
|
|
|
var err error
|
|
|
|
// Look for a suffix otherwise its a plain second value
|
|
|
|
if strings.HasSuffix(inp, "s") || strings.HasSuffix(inp, "m") || strings.HasSuffix(inp, "h") {
|
|
|
|
dur, err = time.ParseDuration(inp)
|
|
|
|
if err != nil {
|
|
|
|
return dur, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Plain integer
|
|
|
|
secs, err := strconv.ParseInt(inp, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return dur, err
|
|
|
|
}
|
|
|
|
dur = time.Duration(secs) * time.Second
|
2016-07-11 17:46:23 +00:00
|
|
|
}
|
2017-02-27 19:42:00 +00:00
|
|
|
case int:
|
|
|
|
dur = time.Duration(in.(int)) * time.Second
|
|
|
|
case int32:
|
|
|
|
dur = time.Duration(in.(int32)) * time.Second
|
|
|
|
case int64:
|
|
|
|
dur = time.Duration(in.(int64)) * time.Second
|
|
|
|
case uint:
|
|
|
|
dur = time.Duration(in.(uint)) * time.Second
|
|
|
|
case uint32:
|
|
|
|
dur = time.Duration(in.(uint32)) * time.Second
|
|
|
|
case uint64:
|
|
|
|
dur = time.Duration(in.(uint64)) * time.Second
|
|
|
|
default:
|
|
|
|
return 0, errors.New("could not parse duration from input")
|
2016-07-11 17:46:23 +00:00
|
|
|
}
|
|
|
|
|
2016-07-11 18:19:35 +00:00
|
|
|
return dur, nil
|
2016-07-11 17:46:23 +00:00
|
|
|
}
|