OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package clock | 5 package clock |
6 | 6 |
7 import ( | 7 import ( |
8 "time" | 8 "time" |
| 9 |
| 10 "golang.org/x/net/context" |
9 ) | 11 ) |
10 | 12 |
11 // Implementation of Clock that uses Go's standard library. | 13 // Implementation of Clock that uses Go's standard library. |
12 type systemClock struct{} | 14 type systemClock struct{} |
13 | 15 |
14 // System clock instance. | 16 // System clock instance. |
15 var systemClockInstance systemClock | 17 var systemClockInstance systemClock |
16 | 18 |
17 var _ Clock = systemClock{} | 19 var _ Clock = systemClock{} |
18 | 20 |
19 // GetSystemClock returns an instance of a Clock whose method calls directly use | 21 // GetSystemClock returns an instance of a Clock whose method calls directly use |
20 // Go's "time" library. | 22 // Go's "time" library. |
21 func GetSystemClock() Clock { | 23 func GetSystemClock() Clock { |
22 return systemClockInstance | 24 return systemClockInstance |
23 } | 25 } |
24 | 26 |
25 func (systemClock) Now() time.Time { | 27 func (systemClock) Now() time.Time { |
26 return time.Now() | 28 return time.Now() |
27 } | 29 } |
28 | 30 |
29 func (systemClock) Sleep(d time.Duration) { | 31 func (sc systemClock) Sleep(c context.Context, d time.Duration) error { |
30 » time.Sleep(d) | 32 » ar := <-sc.After(c, d) |
| 33 » return ar.Err |
31 } | 34 } |
32 | 35 |
33 func (systemClock) NewTimer() Timer { | 36 func (systemClock) NewTimer(ctx context.Context) Timer { |
34 » return new(systemTimer) | 37 » return newSystemTimer(ctx) |
35 } | 38 } |
36 | 39 |
37 func (systemClock) After(d time.Duration) <-chan time.Time { | 40 func (sc systemClock) After(ctx context.Context, d time.Duration) <-chan TimerRe
sult { |
38 » return time.After(d) | 41 » t := sc.NewTimer(ctx) |
| 42 » t.Reset(d) |
| 43 » return t.GetC() |
39 } | 44 } |
OLD | NEW |