Skip to content

Commit eef1706

Browse files
committed
add support for time.Duration
1 parent 865c795 commit eef1706

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

opts.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"reflect"
66
"strconv"
77
"strings"
8+
"time"
89
"unicode"
910
)
1011

@@ -218,6 +219,15 @@ func (o Opts) Bind(v interface{}) error {
218219
field.Set(reflect.ValueOf(vals))
219220
continue
220221
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
222+
ft := field.Type()
223+
if ft.PkgPath() == "time" && ft.Name() == "Duration" {
224+
if s, err := o.String(k); err == nil {
225+
if d, err := time.ParseDuration(s); err == nil {
226+
field.SetInt(int64(d))
227+
continue
228+
}
229+
}
230+
}
221231
if x, err := o.Int(k); err == nil {
222232
field.SetInt(int64(x))
223233
continue

0 commit comments

Comments
 (0)