github.com/juju/clock@v1.0.3/clock.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the LGPLv3, see LICENCE file for details.
     3  
     4  package clock
     5  
     6  import "time"
     7  
     8  // Clock provides an interface for dealing with clocks.
     9  type Clock interface {
    10  	// Now returns the current clock time.
    11  	Now() time.Time
    12  
    13  	// After waits for the duration to elapse and then sends the
    14  	// current time on the returned channel.
    15  	After(time.Duration) <-chan time.Time
    16  
    17  	// AfterFunc waits for the duration to elapse and then calls f in its own goroutine.
    18  	// It returns a Timer that can be used to cancel the call using its Stop method.
    19  	AfterFunc(d time.Duration, f func()) Timer
    20  
    21  	// NewTimer creates a new Timer that will send the current time
    22  	// on its channel after at least duration d.
    23  	NewTimer(d time.Duration) Timer
    24  }
    25  
    26  // Alarm returns a channel that will have the time sent on it at some point
    27  // after the supplied time occurs.
    28  //
    29  // This is short for c.After(t.Sub(c.Now())).
    30  func Alarm(c Clock, t time.Time) <-chan time.Time {
    31  	return c.After(t.Sub(c.Now()))
    32  }
    33  
    34  // The Timer type represents a single event.
    35  // A Timer must be created with AfterFunc.
    36  // This interface follows time.Timer's methods but provides easier mocking.
    37  type Timer interface {
    38  	// When the Timer expires, the current time will be sent on the
    39  	// channel returned from Chan, unless the Timer was created by
    40  	// AfterFunc.
    41  	Chan() <-chan time.Time
    42  
    43  	// Reset changes the timer to expire after duration d.
    44  	// It returns true if the timer had been active, false if
    45  	// the timer had expired or been stopped.
    46  	Reset(time.Duration) bool
    47  
    48  	// Stop prevents the Timer from firing. It returns true if
    49  	// the call stops the timer, false if the timer has already expired or been stopped.
    50  	// Stop does not close the channel, to prevent a read
    51  	// from the channel succeeding incorrectly.
    52  	Stop() bool
    53  }