1package clock
2
3import "time"
4
5type Clock interface {
6	Now() time.Time
7	Sleep(d time.Duration)
8	Since(t time.Time) time.Duration
9	// After waits for the duration to elapse and then sends the current time
10	// on the returned channel.
11	// It is equivalent to clock.NewTimer(d).C.
12	// The underlying Timer is not recovered by the garbage collector
13	// until the timer fires. If efficiency is a concern, use clock.NewTimer
14	// instead and call Timer.Stop if the timer is no longer needed.
15	After(d time.Duration) <-chan time.Time
16
17	NewTimer(d time.Duration) Timer
18	NewTicker(d time.Duration) Ticker
19}
20
21type realClock struct{}
22
23func NewClock() Clock {
24	return &realClock{}
25}
26
27func (clock *realClock) Now() time.Time {
28	return time.Now()
29}
30
31func (clock *realClock) Since(t time.Time) time.Duration {
32	return time.Now().Sub(t)
33}
34
35func (clock *realClock) Sleep(d time.Duration) {
36	<-clock.NewTimer(d).C()
37}
38
39func (clock *realClock) After(d time.Duration) <-chan time.Time {
40	return clock.NewTimer(d).C()
41}
42
43func (clock *realClock) NewTimer(d time.Duration) Timer {
44	return &realTimer{
45		t: time.NewTimer(d),
46	}
47}
48
49func (clock *realClock) NewTicker(d time.Duration) Ticker {
50	return &realTicker{
51		t: time.NewTicker(d),
52	}
53}
54