mirror of https://github.com/spf13/cast.git
Add ToDuration
This commit is contained in:
parent
2318c5caf2
commit
9cff77da28
5
cast.go
5
cast.go
|
@ -17,6 +17,11 @@ func ToTime(i interface{}) time.Time {
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToDuration(i interface{}) time.Duration {
|
||||||
|
v, _ := ToDurationE(i)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
func ToFloat64(i interface{}) float64 {
|
func ToFloat64(i interface{}) float64 {
|
||||||
v, _ := ToFloat64E(i)
|
v, _ := ToFloat64E(i)
|
||||||
return v
|
return v
|
||||||
|
|
16
caste.go
16
caste.go
|
@ -34,6 +34,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) {
|
func ToBoolE(i interface{}) (bool, error) {
|
||||||
i = indirect(i)
|
i = indirect(i)
|
||||||
jww.DEBUG.Println("ToBoolE called on type:", reflect.TypeOf(i))
|
jww.DEBUG.Println("ToBoolE called on type:", reflect.TypeOf(i))
|
||||||
|
|
Loading…
Reference in New Issue