steppedtime

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 17, 2023 License: MIT Imports: 3 Imported by: 0

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

View Source
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 NewClock

func NewClock() *Clock

NewClock returns a new Clock.

func (*Clock) After

func (c *Clock) After(d Duration) <-chan Time

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

func (c *Clock) AfterFunc(d Duration, f func()) *Timer

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) Hours

func (*Clock) Hours(n float64) Duration

Hours returns a Duration value representing n Hours.

func (*Clock) Microseconds

func (*Clock) Microseconds(n int64) Duration

Microseconds returns a Duration value representing n microseconds.

func (*Clock) Milliseconds

func (*Clock) Milliseconds(n int64) Duration

Milliseconds returns a Duration value representing n milliseconds.

func (*Clock) Minutes

func (*Clock) Minutes(n float64) Duration

Minutes returns a Duration value representing n Minutes.

func (*Clock) Nanoseconds

func (*Clock) Nanoseconds(n int64) Duration

Nanoseconds returns a Duration value representing n nanoseconds.

func (*Clock) NewTicker

func (c *Clock) NewTicker(d Duration) *Ticker

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

func (c *Clock) NewTimer(d Duration) *Timer

NewTimer creates a new Timer that will send the current time on its channel after at least duration d.

func (*Clock) Now

func (c *Clock) Now() (now Time)

Now returns the current time.

func (*Clock) ParseDuration

func (*Clock) ParseDuration(s string) (Duration, error)

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) Seconds

func (*Clock) Seconds(n float64) Duration

Seconds returns a Duration value representing n Seconds.

func (*Clock) Set

func (c *Clock) Set(now Time)

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

func (c *Clock) Since(t Time) Duration

Since returns the time elapsed since t. It is shorthand for clock.Now().Sub(t).

func (*Clock) Sleep

func (c *Clock) Sleep(d Duration)

Sleep pauses the current goroutine for at least the duration d. A negative or zero duration causes Sleep to return immediately.

func (*Clock) Step

func (c *Clock) Step(dt Duration)

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

func (c *Clock) Tick(d Duration) <-chan Time

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.

func (*Clock) Until

func (c *Clock) Until(t Time) Duration

Until returns the duration until t. It is shorthand for t.Sub(clock.Now()).

type Duration

type Duration = time.Duration

See time.Duration.

type Ticker

type Ticker struct {
	// contains filtered or unexported fields
}

A Ticker provides a channel that delivers “ticks” of a clock at intervals.

func (*Ticker) C

func (t *Ticker) C() <-chan Time

C returns the channel on which the ticks are delivered.

func (*Ticker) Reset

func (t *Ticker) Reset(d Duration)

Reset stops a ticker and resets its period to the specified duration. The next tick will arrive after the new period elapses. The duration d must be greater than zero; if not, Reset will panic.

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop turns off a ticker. After Stop, no more ticks will be sent. Stop does not close the channel, to prevent a concurrent goroutine reading from the channel from seeing an erroneous "tick".

type Time

type Time int64

Time represents the number of nanoseconds since the start of the clock.

func (Time) Add

func (t Time) Add(d Duration) Time

Add returns the time t+d.

func (Time) After

func (t Time) After(u Time) bool

After reports whether the time instant t is after u.

func (Time) Before

func (t Time) Before(u Time) bool

Before reports whether the time instant t is before u.

func (Time) Compare

func (t Time) Compare(u Time) int

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.

func (Time) Equal

func (t Time) Equal(u Time) bool

Equal reports whether t and u represent the same time instant.

func (Time) IsZero

func (t Time) IsZero() bool

IsZero reports whether t represents the zero time instant, the start of the clock.

func (Time) Sub

func (t Time) Sub(u Time) Duration

Sub returns the duration t-u.

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.

func (*Timer) C

func (t *Timer) C() <-chan Time

C returns the channel on which the ticks are delivered.

func (*Timer) Reset

func (t *Timer) Reset(d Duration) (active bool)

Reset changes the timer to expire after duration d. It returns true if the timer had been active, false if the timer had expired or been stopped.

func (*Timer) Stop

func (t *Timer) Stop() (active bool)

Stop prevents the Timer from firing. It returns true if the call stops the timer, false if the timer has already expired or been stopped. Stop does not close the channel, to prevent a read from the channel succeeding incorrectly.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL