blob: b275b82e22d7b9f8eee7b39bd8d706217fa261e3 (
plain)
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
|
package service
import (
"math/rand"
"time"
)
type Duration struct {
time.Duration
}
func DurationRef(v time.Duration) *Duration {
return &Duration{v}
}
func (s Duration) MarshalText() ([]byte, error) {
return []byte(s.Duration.String()), nil
}
func (s *Duration) Max(args ...*Duration) Duration {
m := s
for _, o := range args {
if m.Duration < o.Duration {
v := o
m = v
}
}
return *m
}
func (s *Duration) Min(args ...*Duration) Duration {
m := s
for _, o := range args {
if m.Duration > o.Duration {
v := o
m = v
}
}
return *m
}
func (s *Duration) UnmarshalText(text []byte) error {
v, err := time.ParseDuration(string(text))
if err != nil {
return err
}
*s = Duration{v}
return nil
}
func RandomDuration(from Duration, until Duration) Duration {
period := until.Duration - from.Duration
return Duration{from.Duration + time.Duration(rand.Intn(int(period)))}
}
|