This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathdefault.go
59 lines (42 loc) · 2.24 KB
/
default.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
package clock
import (
"time"
)
// DefaultClock is an implementation of the Clock interface that uses standard
// time methods.
type DefaultClock struct{}
// Now returns the current local time.
func (dc DefaultClock) Now() time.Time { return time.Now() }
// After waits for the duration to elapse and then sends the current time on the returned channel.
func (dc DefaultClock) After(d time.Duration) <-chan time.Time { return time.After(d) }
// Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.
func (dc DefaultClock) Sleep(d time.Duration) { time.Sleep(d) }
// Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks".
func (dc DefaultClock) Tick(d time.Duration) <-chan time.Time { return time.Tick(d) }
// AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.
func (dc DefaultClock) AfterFunc(d time.Duration, f func()) Timer {
return &defaultTimer{*time.AfterFunc(d, f)}
}
// NewTimer creates a new Timer that will send the current time on its channel after at least duration d.
func (dc DefaultClock) NewTimer(d time.Duration) Timer {
return &defaultTimer{*time.NewTimer(d)}
}
// NewTicker returns a new Ticker containing a channel that will send the time with a period specified by the duration argument.
func (dc DefaultClock) NewTicker(d time.Duration) Ticker {
return &defaultTicker{*time.NewTicker(d)}
}
// Since returns the time elapsed since t.
func (dc DefaultClock) Since(t time.Time) time.Duration { return time.Since(t) }
type defaultTimer struct{ time.Timer }
var _ Timer = new(defaultTimer)
func (d *defaultTimer) Chan() <-chan time.Time {
return d.C
}
type defaultTicker struct{ time.Ticker }
var _ Ticker = new(defaultTicker)
func (d *defaultTicker) Chan() <-chan time.Time {
return d.C
}
// C holds a default clock that uses time.Now as its time source. This is what you should
// normally use in your code.
var C = DefaultClock{}