mocktime

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: 4 Imported by: 0

Documentation

Overview

Package mocktime provides a drop in replacement for time that starts at a fixed epoch and may be controlled as a relative clock.

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.

View Source
const (
	January   = time.January
	February  = time.February
	March     = time.March
	April     = time.April
	May       = time.May
	June      = time.June
	July      = time.July
	August    = time.August
	September = time.September
	October   = time.October
	November  = time.November
	December  = time.December
)

Month constants.

View Source
const (
	Sunday    = time.Sunday
	Monday    = time.Monday
	Tuesday   = time.Tuesday
	Wednesday = time.Wednesday
	Thursday  = time.Thursday
	Friday    = time.Friday
	Saturday  = time.Saturday
)

Weekday constants.

View Source
const (
	Layout      = "01/02 03:04:05PM '06 -0700" // The reference time, in numerical order.
	ANSIC       = "Mon Jan _2 15:04:05 2006"
	UnixDate    = "Mon Jan _2 15:04:05 MST 2006"
	RubyDate    = "Mon Jan 02 15:04:05 -0700 2006"
	RFC822      = "02 Jan 06 15:04 MST"
	RFC822Z     = "02 Jan 06 15:04 -0700" // RFC822 with numeric zone
	RFC850      = "Monday, 02-Jan-06 15:04:05 MST"
	RFC1123     = "Mon, 02 Jan 2006 15:04:05 MST"
	RFC1123Z    = "Mon, 02 Jan 2006 15:04:05 -0700" // RFC1123 with numeric zone
	RFC3339     = "2006-01-02T15:04:05Z07:00"
	RFC3339Nano = "2006-01-02T15:04:05.999999999Z07:00"
	Kitchen     = "3:04PM"
	// Handy time stamps.
	Stamp      = "Jan _2 15:04:05"
	StampMilli = "Jan _2 15:04:05.000"
	StampMicro = "Jan _2 15:04:05.000000"
	StampNano  = "Jan _2 15:04:05.000000000"
	DateTime   = "2006-01-02 15:04:05"
	DateOnly   = "2006-01-02"
	TimeOnly   = "15:04:05"
)

Layouts (See time.Layout).

Variables

View Source
var Local = time.Local

See time.Local.

View Source
var UTC = time.UTC

See time.UTC.

Functions

func Active

func Active()

Active returns true if the global Clock instance is currently running.

func After

func 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 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 Fastforward added in v0.2.0

func Fastforward()

Fastforward steps the global Clock instance forward to trigger timers until there are no timers left to trigger on it.

func Scale

func Scale() float64

Scale returns the scaling factor of the global Clock instance.

func Set

func Set(now Time)

Set changes the current time on the global Clock instance to now.

func SetScale

func SetScale(scale float64)

SetScale sets the scaling factor for the global Clock instance.

func Sleep

func 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 Start

func Start()

Start starts or resumes the global Clock instance.

func Step

func Step(dt Duration)

Step advances the current time on the global Clock instance by dt.

func Stop

func Stop()

Stop pauses the global Clock instance.

func Tick

func 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.

Types

type Clock

type Clock struct {
	*relativetime.Clock[Time, Duration, *realtime.Timer]
	// contains filtered or unexported fields
}

Clock provides a drop in replacement for realtime.Clock, but with additional methods to allow direct control over its behavior.

func NewClock

func NewClock() Clock

NewClock returns a new Clock set to the current time.

func NewClockAt

func NewClockAt(at Time) Clock

NewClockAt returns a new Clock set to the the time, at.

func (Clock) Fastforward added in v0.2.0

func (c Clock) Fastforward()

Fastforward steps forward to trigger timers until there are no timers left to trigger.

type Duration

type Duration = time.Duration

See time.Duration.

func ParseDuration

func 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 Since

func Since(t Time) Duration

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

func Until

func Until(t Time) Duration

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

type Location

type Location = time.Location

See time.Location.

func FixedZone

func FixedZone(name string, offset int) *Location

See time.FixedZone.

func LoadLocation

func LoadLocation(name string) (*Location, error)

See time.LoadLocation.

func LoadLocationFromTZData

func LoadLocationFromTZData(name string, data []byte) (*Location, error)

See time.LoadLocationFromTZData.

type Month

type Month = time.Month

See time.Month.

type ParseError

type ParseError = time.ParseError

See time.ParseError.

type Ticker

type Ticker = relativetime.Ticker[Time, Duration]

Ticker is an alias for relativetime.Ticker using the types Time and Duration.

func NewTicker

func 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.

type Time

type Time = time.Time

See time.Time.

func Date

func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time

See time.Date.

func NextAt added in v0.2.0

func NextAt() Time

NextAt returns the time of the next scheduled Timer or Ticker on the global Clock instance.

func Now

func Now() Time

Now returns the current time on the global Clock instance.

func Parse

func Parse(layout, value string) (Time, error)

See time.Parse.

func ParseInLocation

func ParseInLocation(layout, value string, loc *Location) (Time, error)

See time.ParseInLocation.

func Unix

func Unix(sec int64, nsec int64) Time

See time.Unix.

func UnixMicro

func UnixMicro(usec int64) Time

See time.UnixMicro.

func UnixMilli

func UnixMilli(msec int64) Time

See time.UnixMilli.

type Timer

type Timer = relativetime.Timer[Time, Duration]

Timer is an alias for relativetime.Timer using the types Time and Duration.

func AfterFunc

func 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 NewTimer

func NewTimer(d Duration) *Timer

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

type Weekday

type Weekday = time.Weekday

See time.Weekday.

Jump to

Keyboard shortcuts

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