tstime

package
v1.69.0-pre Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2024 License: BSD-3-Clause Imports: 5 Imported by: 26

Documentation

Overview

Package tstime defines Tailscale-specific time utilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Parse3339

func Parse3339(s string) (time.Time, error)

Parse3339 is a wrapper around time.Parse(time.RFC3339, s).

func Parse3339B added in v1.4.0

func Parse3339B(b []byte) (time.Time, error)

Parse3339B is Parse3339 but for byte slices.

func ParseDuration added in v1.32.0

func ParseDuration(s string) (time.Duration, error)

ParseDuration is more expressive than time.ParseDuration, also accepting 'd' (days) and 'w' (weeks) literals.

func RandomDurationBetween added in v1.4.0

func RandomDurationBetween(min, max time.Duration) time.Duration

RandomDurationBetween returns a random duration in range [min,max). If panics if max < min.

func Sleep added in v1.38.0

func Sleep(ctx context.Context, d time.Duration) bool

Sleep is like time.Sleep but returns early upon context cancelation. It reports whether the full sleep duration was achieved.

Types

type Clock added in v1.46.0

type Clock interface {
	// Now returns the current time, as in time.Now.
	Now() time.Time
	// NewTimer returns a timer whose notion of the current time is controlled
	// by this Clock. It follows the semantics of time.NewTimer as closely as
	// possible but is adapted to return an interface, so the channel needs to
	// be returned as well.
	NewTimer(d time.Duration) (TimerController, <-chan time.Time)
	// NewTicker returns a ticker whose notion of the current time is controlled
	// by this Clock. It follows the semantics of time.NewTicker as closely as
	// possible but is adapted to return an interface, so the channel needs to
	// be returned as well.
	NewTicker(d time.Duration) (TickerController, <-chan time.Time)
	// AfterFunc returns a ticker whose notion of the current time is controlled
	// by this Clock. When the ticker expires, it will call the provided func.
	// It follows the semantics of time.AfterFunc.
	AfterFunc(d time.Duration, f func()) TimerController
	// Since returns the time elapsed since t.
	// It follows the semantics of time.Since.
	Since(t time.Time) time.Duration
}

Clock offers a subset of the functionality from the std/time package. Normally, applications will use the StdClock implementation that calls the appropriate std/time exported funcs. The advantage of using Clock is that tests can substitute a different implementation, allowing the test to control time precisely, something required for certain types of tests to be possible at all, speeds up execution by not needing to sleep, and can dramatically reduce the risk of flakes due to tests executing too slowly or quickly.

type DefaultClock added in v1.52.0

type DefaultClock struct{ Clock }

DefaultClock is a wrapper around a Clock. It uses StdClock by default if Clock is nil.

func (DefaultClock) AfterFunc added in v1.52.0

func (c DefaultClock) AfterFunc(d time.Duration, f func()) TimerController

func (DefaultClock) NewTicker added in v1.52.0

func (c DefaultClock) NewTicker(d time.Duration) (TickerController, <-chan time.Time)

func (DefaultClock) NewTimer added in v1.52.0

func (c DefaultClock) NewTimer(d time.Duration) (TimerController, <-chan time.Time)

func (DefaultClock) Now added in v1.52.0

func (c DefaultClock) Now() time.Time

func (DefaultClock) Since added in v1.52.0

func (c DefaultClock) Since(t time.Time) time.Duration

type StdClock added in v1.46.0

type StdClock struct{}

StdClock is a simple implementation of Clock using the relevant funcs in the std/time package.

func (StdClock) AfterFunc added in v1.46.0

func (StdClock) AfterFunc(d time.Duration, f func()) TimerController

AfterFunc calls time.AfterFunc.

func (StdClock) NewTicker added in v1.46.0

func (StdClock) NewTicker(d time.Duration) (TickerController, <-chan time.Time)

NewTicker calls time.NewTicker. As an interface does not allow for struct members and other packages cannot add receivers to another package, the channel is also returned because it would be otherwise inaccessible.

func (StdClock) NewTimer added in v1.46.0

func (StdClock) NewTimer(d time.Duration) (TimerController, <-chan time.Time)

NewTimer calls time.NewTimer. As an interface does not allow for struct members and other packages cannot add receivers to another package, the channel is also returned because it would be otherwise inaccessible.

func (StdClock) Now added in v1.46.0

func (StdClock) Now() time.Time

Now calls time.Now.

func (StdClock) Since added in v1.46.0

func (StdClock) Since(t time.Time) time.Duration

Since calls time.Since.

type TickerController added in v1.46.0

type TickerController interface {
	// Reset follows the same semantics as with time.Ticker.Reset.
	Reset(d time.Duration)
	// Stop follows the same semantics as with time.Ticker.Stop.
	Stop()
}

TickerController offers the receivers of a time.Ticker to ensure compatibility with standard timers, but allows for the option of substituting a standard timer with something else for testing purposes.

type TimerController added in v1.46.0

type TimerController interface {
	// Reset follows the same semantics as with time.Timer.Reset.
	Reset(d time.Duration) bool
	// Stop follows the same semantics as with time.Timer.Stop.
	Stop() bool
}

TimerController offers the receivers of a time.Timer to ensure compatibility with standard timers, but allows for the option of substituting a standard timer with something else for testing purposes.

Directories

Path Synopsis
Package mono provides fast monotonic time.
Package mono provides fast monotonic time.
Package rate provides a rate limiter.
Package rate provides a rate limiter.

Jump to

Keyboard shortcuts

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