forked from mirror/cast
Add ToDuration
This commit is contained in:
parent
dace11d497
commit
3cc9db95ac
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
|
@ -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) {
|
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