|
1 | | -package clocks |
| 1 | +package offset |
2 | 2 |
|
3 | 3 | import ( |
4 | 4 | "context" |
5 | 5 | "time" |
| 6 | + |
| 7 | + clocks "github.com/vimeo/go-clocks" |
6 | 8 | ) |
7 | 9 |
|
8 | | -// OffsetClock wraps another clock, adjusting time intervals by a constant |
| 10 | +// Clock wraps another clock, adjusting time intervals by adding a constant |
9 | 11 | // offset. (useful for simulating clock-skew) |
10 | | -type OffsetClock struct { |
11 | | - inner Clock |
| 12 | +// Note that relative durations are unaffected. |
| 13 | +type Clock struct { |
| 14 | + inner clocks.Clock |
12 | 15 | offset time.Duration |
13 | 16 | } |
14 | 17 |
|
15 | 18 | // Now implements Clock, returning the current time (according to the captive |
16 | 19 | // clock adjusted by offset) |
17 | | -func (o *OffsetClock) Now() time.Time { |
| 20 | +func (o *Clock) Now() time.Time { |
18 | 21 | return o.inner.Now().Add(o.offset) |
19 | 22 | } |
20 | 23 |
|
21 | 24 | // Until implements Clock, returning the difference between the current time |
22 | 25 | // and its argument (according to the captive clock adjusted by offset) |
23 | | -func (o *OffsetClock) Until(t time.Time) time.Duration { |
| 26 | +func (o *Clock) Until(t time.Time) time.Duration { |
24 | 27 | return o.inner.Until(t) + o.offset |
25 | 28 | } |
26 | 29 |
|
27 | 30 | // SleepUntil blocks until either ctx expires or until arrives. |
28 | 31 | // Return value is false if context-cancellation/expiry prompted an |
29 | 32 | // early return |
30 | | -func (o *OffsetClock) SleepUntil(ctx context.Context, until time.Time) bool { |
| 33 | +func (o *Clock) SleepUntil(ctx context.Context, until time.Time) bool { |
31 | 34 | return o.inner.SleepUntil(ctx, until.Add(o.offset)) |
32 | 35 | } |
33 | 36 |
|
34 | | -// SleepFor is the relative-time equivalent of SleepUntil. In the |
35 | | -// default implementation, this is the lower-level method, but other |
36 | | -// implementations may disagree. |
37 | | -func (o *OffsetClock) SleepFor(ctx context.Context, dur time.Duration) bool { |
| 37 | +// SleepFor is the relative-time equivalent of SleepUntil. |
| 38 | +// Return value is false if context-cancellation/expiry prompted an |
| 39 | +// early return |
| 40 | +func (o *Clock) SleepFor(ctx context.Context, dur time.Duration) bool { |
38 | 41 | // SleepFor is relative, so it doesn't need any adjustment |
39 | 42 | return o.inner.SleepFor(ctx, dur) |
40 | 43 | } |
41 | 44 |
|
42 | | -// NewOffsetClock creates an OffsetClock and returns it |
43 | | -func NewOffsetClock(inner Clock, offset time.Duration) *OffsetClock { |
44 | | - return &OffsetClock{ |
| 45 | +// NewOffsetClock creates an OffsetClock. |
| 46 | +// offset is added to all absolute times |
| 47 | +func NewOffsetClock(inner clocks.Clock, offset time.Duration) *Clock { |
| 48 | + return &Clock{ |
45 | 49 | inner: inner, |
46 | 50 | offset: offset, |
47 | 51 | } |
|
0 commit comments