Documentation ¶
Overview ¶
Package steppedtime provides a simple clock and time implementation starting at zero and counting upwards. It advances only when explicitly stepped.
Index ¶
- Constants
- type Clock
- func (c *Clock) After(d Duration) <-chan Time
- func (c *Clock) AfterFunc(d Duration, f func()) *Timer
- func (*Clock) Hours(n float64) Duration
- func (*Clock) Microseconds(n int64) Duration
- func (*Clock) Milliseconds(n int64) Duration
- func (*Clock) Minutes(n float64) Duration
- func (*Clock) Nanoseconds(n int64) Duration
- func (c *Clock) NewTicker(d Duration) *Ticker
- func (c *Clock) NewTimer(d Duration) *Timer
- func (c *Clock) Now() (now Time)
- func (*Clock) ParseDuration(s string) (Duration, error)
- func (*Clock) Seconds(n float64) Duration
- func (c *Clock) Set(now Time)
- func (c *Clock) Since(t Time) Duration
- func (c *Clock) Sleep(d Duration)
- func (c *Clock) Step(dt Duration)
- func (c *Clock) Tick(d Duration) <-chan Time
- func (c *Clock) Until(t Time) Duration
- type Duration
- type Ticker
- type Time
- type Timer
Constants ¶
const ( Nanosecond = time.Nanosecond Microsecond = time.Microsecond Millisecond = time.Millisecond Second = time.Second Minute = time.Minute Hour = time.Hour )
Duration constants.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Clock ¶
type Clock struct {
// contains filtered or unexported fields
}
Clock represents a simulation clock that only advances when explicitly stepped. Its methods are thread-safe. The zero-value of a Clock is perfectly valid.
func (*Clock) After ¶
After waits for the duration to elapse and then sends the current time on the returned channel. It is equivalent to clock.NewTimer(d).C(). The underlying Timer is not recovered by the garbage collector until the timer fires. If efficiency is a concern, use clock.NewTimer instead and call Timer.Stop if the timer is no longer needed.
func (*Clock) AfterFunc ¶
AfterFunc waits for the duration to elapse and then calls f in its own goroutine. It returns a Timer that can be used to cancel the call using its Stop method.
func (*Clock) Microseconds ¶
Microseconds returns a Duration value representing n microseconds.
func (*Clock) Milliseconds ¶
Milliseconds returns a Duration value representing n milliseconds.
func (*Clock) Nanoseconds ¶
Nanoseconds returns a Duration value representing n nanoseconds.
func (*Clock) NewTicker ¶
NewTicker returns a new Ticker containing a channel that will send the current time on the channel after each tick. The period of the ticks is specified by the duration argument. The ticker will adjust the time interval or drop ticks to make up for slow receivers. The duration d must be greater than zero; if not, NewTicker will panic. Stop the ticker to release associated resources.
func (*Clock) NewTimer ¶
NewTimer creates a new Timer that will send the current time on its channel after at least duration d.
func (*Clock) ParseDuration ¶
ParseDuration parses a duration string. A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as "300ms", "-1.5h" or "2h45m". Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
func (*Clock) Set ¶
Set sets the current time to now. If any timers are active, a value of now earlier than the previous setting may lead to undefined behavior.
func (*Clock) Since ¶
Since returns the time elapsed since t. It is shorthand for clock.Now().Sub(t).
func (*Clock) Sleep ¶
Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.
func (*Clock) Step ¶
Step advances the current time by dt. If any timers are active, a negative value for dt may lead to undefined behavior.
func (*Clock) Tick ¶
Tick is a convenience wrapper for NewTicker providing access to the ticking channel only. While Tick is useful for clients that have no need to shut down the Ticker, be aware that without a way to shut it down the underlying Ticker cannot be recovered by the garbage collector; it "leaks". Unlike NewTicker, Tick will return nil if d <= 0.
type Ticker ¶
type Ticker struct {
// contains filtered or unexported fields
}
A Ticker provides a channel that delivers “ticks” of a clock at intervals.
type Time ¶
type Time int64
Time represents the number of nanoseconds since the start of the clock.
func (Time) Compare ¶
Compare compares the time instant t with u. If t is before u, it returns -1; if t is after u, it returns +1; if they're the same, it returns 0.
type Timer ¶
type Timer struct {
// contains filtered or unexported fields
}
The Timer type represents a single event. When the Timer expires, the current time will be sent on the channel returned by C(), unless the Timer was created by AfterFunc. A Timer must be created with NewTimer or AfterFunc.