Add ToDuration

This commit is contained in:
Chance Zibolski 2015-02-18 18:56:24 -08:00 committed by spf13
parent dace11d497
commit 3cc9db95ac
2 changed files with 21 additions and 0 deletions

View File

@ -17,6 +17,11 @@ func ToTime(i interface{}) time.Time {
return v
}
func ToDuration(i interface{}) time.Duration {
v, _ := ToDurationE(i)
return v
}
func ToFloat64(i interface{}) float64 {
v, _ := ToFloat64E(i)
return v

View File

@ -35,6 +35,22 @@ func ToTimeE(i interface{}) (tim time.Time, err error) {
}
}
func ToDurationE(i interface{}) (d time.Duration, err error) {
i = indirect(i)
jww.DEBUG.Println("ToDurationE called on type:", reflect.TypeOf(i))
switch s := i.(type) {
case time.Duration:
return s, nil
case string:
d, err = time.ParseDuration(s)
return
default:
err = fmt.Errorf("Unable to Cast %#v to Duration\n", i)
return
}
}
func ToBoolE(i interface{}) (bool, error) {
i = indirect(i)
jww.DEBUG.Println("ToBoolE called on type:", reflect.TypeOf(i))