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
- Variables
- func Active()
- func After(d Duration) <-chan Time
- func Fastforward()
- func Scale() float64
- func Set(now Time)
- func SetScale(scale float64)
- func Sleep(d Duration)
- func Start()
- func Step(dt Duration)
- func Stop()
- func Tick(d Duration) <-chan Time
- type Clock
- type Duration
- type Location
- type Month
- type ParseError
- type Ticker
- type Time
- func Date(year int, month Month, day, hour, min, sec, nsec int, loc *Location) Time
- func NextAt() Time
- func Now() Time
- func Parse(layout, value string) (Time, error)
- func ParseInLocation(layout, value string, loc *Location) (Time, error)
- func Unix(sec int64, nsec int64) Time
- func UnixMicro(usec int64) Time
- func UnixMilli(msec int64) Time
- type Timer
- type Weekday
Constants ¶
const ( Nanosecond = time.Nanosecond Microsecond = time.Microsecond Millisecond = time.Millisecond Second = time.Second Minute = time.Minute Hour = time.Hour )
Duration constants.
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.
const ( Sunday = time.Sunday Monday = time.Monday Tuesday = time.Tuesday Wednesday = time.Wednesday Thursday = time.Thursday Friday = time.Friday Saturday = time.Saturday )
Weekday constants.
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 ¶
var Local = time.Local
See time.Local.
var UTC = time.UTC
See time.UTC.
Functions ¶
func After ¶
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 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 Step ¶
func Step(dt Duration)
Step advances the current time on the global Clock instance by dt.
func 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.
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 NewClockAt ¶
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 ¶
See time.Duration.
func 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".
type Ticker ¶
type Ticker = relativetime.Ticker[Time, Duration]
Ticker is an alias for relativetime.Ticker using the types Time and Duration.
func 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.
type Time ¶
See time.Time.
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 ParseInLocation ¶
See time.ParseInLocation.
type Timer ¶
type Timer = relativetime.Timer[Time, Duration]
Timer is an alias for relativetime.Timer using the types Time and Duration.