File: clock.go

package info (click to toggle)
golang-github-pivotal-golang-clock 0.0~git20151018.0.3fd3c19-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, experimental, sid
  • size: 104 kB
  • ctags: 56
  • sloc: makefile: 2
file content (42 lines) | stat: -rw-r--r-- 719 bytes parent folder | download | duplicates (2)
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
package clock

import "time"

type Clock interface {
	Now() time.Time
	Sleep(d time.Duration)
	Since(t time.Time) time.Duration

	NewTimer(d time.Duration) Timer
	NewTicker(d time.Duration) Ticker
}

type realClock struct{}

func NewClock() Clock {
	return &realClock{}
}

func (clock *realClock) Now() time.Time {
	return time.Now()
}

func (clock *realClock) Since(t time.Time) time.Duration {
	return time.Now().Sub(t)
}

func (clock *realClock) Sleep(d time.Duration) {
	<-clock.NewTimer(d).C()
}

func (clock *realClock) NewTimer(d time.Duration) Timer {
	return &realTimer{
		t: time.NewTimer(d),
	}
}

func (clock *realClock) NewTicker(d time.Duration) Ticker {
	return &realTicker{
		t: time.NewTicker(d),
	}
}