-
Notifications
You must be signed in to change notification settings - Fork 20
/
time.go
61 lines (48 loc) · 1.34 KB
/
time.go
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
60
61
package utils
import (
"time"
)
// TimeSource is something that can provide a time
type TimeSource interface {
Now() time.Time
}
// defaultTimeSource returns the current system time
type defaultTimeSource struct{}
func (s defaultTimeSource) Now() time.Time {
return time.Now()
}
// a time source which returns a fixed time
type fixedTimeSource struct {
now time.Time
}
func (s *fixedTimeSource) Now() time.Time {
return s.now
}
// NewFixedTimeSource creates a new fixed time source
func NewFixedTimeSource(now time.Time) TimeSource {
return &fixedTimeSource{now: now}
}
// a time source which returns a sequence of times 1 second after each other
type sequentialTimeSource struct {
current time.Time
}
func (s *sequentialTimeSource) Now() time.Time {
now := s.current
s.current = s.current.Add(time.Second * 1)
return now
}
// NewSequentialTimeSource creates a new sequential time source
func NewSequentialTimeSource(start time.Time) TimeSource {
return &sequentialTimeSource{current: start}
}
// DefaultTimeSource is the default time source
var DefaultTimeSource TimeSource = defaultTimeSource{}
var currentTimeSource = DefaultTimeSource
// Now returns the current time
func Now() time.Time {
return currentTimeSource.Now()
}
// SetTimeSource sets the time source used by Now()
func SetTimeSource(source TimeSource) {
currentTimeSource = source
}