forked from aws/amazon-ecs-agent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ttime.go
46 lines (36 loc) · 1001 Bytes
/
ttime.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
// Package ttime implements a testable alternative to the Go "time" package.
package ttime
import "time"
// Time represents an implementation for this package's methods
type Time interface {
Now() time.Time
Sleep(d time.Duration)
}
// DefaultTime is a Time that behaves normally
type DefaultTime struct{}
var _time Time = &DefaultTime{}
// Now returns the current time
func (*DefaultTime) Now() time.Time {
return time.Now()
}
// Sleep sleeps for the given duration
func (*DefaultTime) Sleep(d time.Duration) {
time.Sleep(d)
}
// SetTime configures what 'Time' implementation to use for each of the
// package-level methods.
func SetTime(t Time) {
_time = t
}
// Now returns the implementation's current time
func Now() time.Time {
return _time.Now()
}
// Sleep calls the implementation's Sleep method
func Sleep(d time.Duration) {
_time.Sleep(d)
}
// Since returns the time different from Now and the given time t
func Since(t time.Time) time.Duration {
return _time.Now().Sub(t)
}