-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandutils.go
More file actions
37 lines (32 loc) · 796 Bytes
/
Copy pathrandutils.go
File metadata and controls
37 lines (32 loc) · 796 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
package goutils
import (
"fmt"
"math/rand"
"time"
)
const (
// TimeUnitTypeSecond as second
TimeUnitTypeSecond int = 0
// TimeUnitTypeMinute as minute
TimeUnitTypeMinute int = 1
// TimeUnitTypeHour as hour
TimeUnitTypeHour int = 2
)
// RandDuration generate pseudo time duration between lower and upper with lower and upper bound inclusively.
func RandDuration(lower, upper, timeUnitType int) time.Duration {
if upper < lower {
panic("upper bound should gt lower bound")
}
d := time.Second
switch timeUnitType {
case TimeUnitTypeSecond:
d = time.Second
case TimeUnitTypeMinute:
d = time.Minute
case TimeUnitTypeHour:
d = time.Hour
default:
panic(fmt.Sprintf("invalid time unit type: %d", timeUnitType))
}
return time.Duration(rand.Intn(upper-lower)+lower) * d
}