-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathduration.go
More file actions
40 lines (37 loc) · 894 Bytes
/
duration.go
File metadata and controls
40 lines (37 loc) · 894 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cast
import "time"
//Duration cast input to time.Duration
func Duration(input interface{}) (output time.Duration, err error) {
switch castValue := input.(type) {
case string:
output, err = time.ParseDuration(castValue)
if err != nil {
outputInt, errCast := Int(castValue)
if errCast != nil {
err = NewCastError("Could not convert to time.Duration")
return
}
output = time.Duration(int(outputInt))
}
return
case time.Duration:
output = castValue
return
default:
outputInt, errCast := Int(castValue)
if errCast == nil {
output = time.Duration(int(outputInt))
return
}
err = NewCastError("Could not convert to time.Duration")
}
return
}
//MustDuration cast input to time.Duration and panic if error
func MustDuration(input interface{}) time.Duration {
output, err := Duration(input)
if err != nil {
panic(err)
}
return output
}