Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 package clock | |
| 6 | |
| 7 import ( | |
| 8 "time" | |
| 9 ) | |
| 10 | |
| 11 // A Clock is an interface to system time. | |
| 12 // | |
| 13 // The standard clock is SystemClock, which falls through to the system time lib rary. | |
| 14 // Another clock, FakeClock, is available to simulate time facilities for testin g. | |
| 15 type Clock interface { | |
| 16 Now() time.Time // Returns the current time (see t ime.Now). | |
| 17 Sleep(time.Duration) // Sleeps the current goroutine (s ee time.Sleep) | |
| 18 NewTimer() Timer // Creates a new Timer instance. | |
| 19 After(time.Duration) <-chan time.Time // Waits a duration, then sends th e current time. | |
| 20 } | |
| 21 | |
| 22 // Implementation of Clock that uses Go's standard library. | |
|
iannucci
2015/06/02 06:15:33
implementations should go in subpackages I think.
| |
| 23 type systemClock struct{} | |
| 24 | |
| 25 // System clock instance. | |
| 26 var systemClockInstance systemClock | |
| 27 | |
| 28 // SystemClock returns an instance of a Clock whose method calls directly use Go 's "time" | |
| 29 // library. | |
| 30 func SystemClock() Clock { | |
| 31 return systemClockInstance | |
| 32 } | |
| 33 | |
| 34 // Implements Clock. | |
| 35 func (systemClock) Now() time.Time { | |
| 36 return time.Now() | |
| 37 } | |
| 38 | |
| 39 // Implements Clock. | |
| 40 func (systemClock) Sleep(d time.Duration) { | |
| 41 time.Sleep(d) | |
| 42 } | |
| 43 | |
| 44 // Implements Clock. | |
| 45 func (systemClock) NewTimer() Timer { | |
| 46 return new(systemTimer) | |
| 47 } | |
| 48 | |
| 49 // Implements Clock. | |
| 50 func (systemClock) After(d time.Duration) <-chan time.Time { | |
| 51 return time.After(d) | |
| 52 } | |
| OLD | NEW |