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 10 NewTimer(d time.Duration) Timer 11 NewTicker(d time.Duration) Ticker 12} 13 14type realClock struct{} 15 16func NewClock() Clock { 17 return &realClock{} 18} 19 20func (clock *realClock) Now() time.Time { 21 return time.Now() 22} 23 24func (clock *realClock) Since(t time.Time) time.Duration { 25 return time.Now().Sub(t) 26} 27 28func (clock *realClock) Sleep(d time.Duration) { 29 <-clock.NewTimer(d).C() 30} 31 32func (clock *realClock) NewTimer(d time.Duration) Timer { 33 return &realTimer{ 34 t: time.NewTimer(d), 35 } 36} 37 38func (clock *realClock) NewTicker(d time.Duration) Ticker { 39 return &realTicker{ 40 t: time.NewTicker(d), 41 } 42} 43