time

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2025 License: MIT Imports: 9 Imported by: 1

README

blugnu/time

A simple and lightweight module for Go providing a context clock and a mock clock for testing time-based scenarios in accelerated, deterministic time.

Features

  • Context Clock: A clock that can be passed in a context;

  • Mock Clock: A clock that can be used in tests to simulate time passing independently of the actual time, allowing for accelerated and deterministic testing of time-based code;

  • Mock Timers: Context deadlines, timeouts, timers and tickers that behave deterministically with a mock clock, allowing for testing of time-based code without relying on the system clock or the passage of real time;

  • Compatible: Compatible with the standard library time package where appropriate, allowing for easy migration from the standard library to blugnu/time; provides aliases for types and functions that are not clock-dependent with alternative functions for clock-dependent functionality;

  • Lightweight: No external dependencies, making it easy to use and integrate into existing projects;

Installation

go get github.com/blugnu/time

Usage

As far as possible, blugnu/time is designed to be a drop-in replacement for the standard library time package with additional functions where required.

Clock-Independent Usage

Aliases are provided for constants, types and clock-agnostic functions from the standard library time package:

      // standard library time
      import "time"
      ux := time.Unix()

      // becomes:
      import "github.com/blugnu/time"
      ux := time.Unix()
Clock-Dependent Functions

Clock-dependent functions are replaced by similes which accept a context, or may be called using an implementation provided by a clock:

      // standard library time
      import "time"
      time.Now()
      
      // becomes
      import "github.com/blugnu/time"
      time.Now(ctx)

      // or:
      clock := time.ClockFromContext(ctx)
      clock.Now()

Update references to clock-dependent functions to avoid mixing use of mocked and non-mocked time which would cause unpredictable behaviour in tests.

Context Deadlines and Timeouts

Context deadlines and timeouts are also clock-dependent. The blugnu/time package provides a ContextWithDeadline and ContextWithTimeout functions that return a context with a deadline or timeout that is based on the clock passed in the context. When using a mock clock, the deadline or timeout will be based on the mock clock, allowing for deterministic behaviour in tests.

      // standard library time
      import "time"
      ctx, cancel := context.WithDeadline(parentCtx, time.Now().Add(5*time.Second))
      defer cancel()

      // becomes
      import "github.com/blugnu/time"
      ctx, cancel := time.ContextWithDeadline(parentCtx, time.Now().Add(5*time.Second))
      defer cancel()
In Tests
  • inject a MockClock into the Context used for tests;

  • use the provided MockClock methods to advance the clock in a deterministic fashion to exercise time-dependent code, including context deadlines and timeouts independently of the elapsed time of the test.

Example
// Simulates testing some code which uses a context with a timeout that would
// normally take 10 seconds to complete if testing the context deadline expiry.
// 
// The test will instead run in milliseconds.
func TestAcceleratedTime() {
  // create a mock clock
  clock := time.NewMockClock()

  // create a context with a 10s timeout; the cancel function is not used 
  timer, _ := clock.ContextWithTimeout(context.Background(), 10*time.Second)

  // start a goroutine that will block until the context is cancelled;
  // a waitgroup is used to sync with the test
  var wg sync.WaitGroup
  wg.Add(1)
  go func() {
    defer wg.Done()
    <-timer.Done()
  }()

  // advance the mock clock by 10s; this will cause the context to be cancelled
  // and the goroutine to unblock
  clock.AdvanceBy(10 * time.Second)
  wg.Wait()

  // verify that the context was cancelled due to the timeout
  if timer.Err() != context.DeadlineExceeded {
    t.Errorf("expected context.DeadlineExceeded, got %s", timer.Err())
  }
}
Additional Functions

Functions are provided for adding or retrieving a clock to/from a context as well as initialising a mock clock either stand-alone or in a context:

    // returns the clock from the context or the system clock if not present
    ClockFromContext(ctx context.Context) Clock

    // adds a clock to the context; panics if the context already has a clock
    ContextWithClock(ctx context.Context, clock Clock) context.Context

    // adds a mock clock to the context; panics if the context already has a clock
    ContextWithMockClock(ctx context.Context, opts ...MockClockOption) (context.Context, MockClock)

    // configures a new mock clock
    NewMockClock(opts ...MockClockOption) Clock

    // returns the clock from the context or nil if not present
    TryClockFromContext(ctx context.Context) Clock

System Clock vs Mock Clocks

The system clock is the actual clock of the system, which is used to measure real time. There is only one system clock.

A mock clock is a simulated clock that can be used to control the passage of time in tests. A mock clock can be used to simulate time passing at a different rate than the system clock while preserving the behaviour of tickers, timers, timeouts and deadlines.

This can provide tests that run more reliably and more quickly than in real-time. For example, a test that requires many hours of elapsed clock time can be executed in milliseconds.

Individual tests may use different mock clocks, allowing for different tests to run at different rates or to simulate different clock behaviours.

Running vs Stopped Clock

A mock clock can be either running or stopped. Mock clocks are created stopped by default, unless the StartRunning() option is applied.

Stopped Clocks

A stopped clock will not advance time automatically. The clock must be explicitly advanced using the AdvanceBy or AdvanceTo methods. This provides precise control over the passage of time in tests.

Running Clocks

When a mock clock is running, it will advance time automatically in real-time whenever a clock operation is performed involving the current time, or the Update method called.

Despite the terminology, a running mock clock will not update in the background.

Attempting to explicitly advance a running clock will result in a panic. This is to prevent accidental use of a running clock in tests that expect a stopped clock.

Stopping and Starting

Although not usually necessary or recommended, a mock clock may be stopped and started using the Stop and Start methods. Every call to Stop must be matched with a call to Start to resume running.

Attempting to Start a clock that is already running will result in a panic.

Mock Clock Options

time.AtNow

The AtNow option is a convenience for time.AtTime(time.SystemClock().Now()).

time.AtTime

The AtTime option allows you to set the initial time of the mock clock. By default a mock clock is set to the zero time (Unix Epoch).

time.DropsTicks

The DropsTicks option sets the mock clock to drop any extra ticks when advancing time. This option only affects tickers, not timers or context deadlines.

By default, if a ticker is set to fire every 1s and the clock is advanced by 10s, then the ticker will fire 10 times, once for each second. With DropsTicks set the ticker will fire only once in this situation, at the end of the 10 seconds.

time.InLocation

The InLocation option allows you to set the location of the mock clock. The default is UTC.

time.StartRunning

The StartRunning option sets the mock clock to start running immediately when it is created. By default, the mock clock is stopped and must be started manually if required.

time.Yielding

The mock clock suspends the calling goroutine for 1ms when performing certain operations. The Yielding option allows this to be changed to some other duration or disabled entirely (specifying a duration of 0).

Documentation

Index

Examples

Constants

View Source
const (
	// durations
	Day         = time.Hour * 24
	Hour        = time.Hour
	Microsecond = time.Microsecond
	Millisecond = time.Millisecond
	Minute      = time.Minute
	Nanosecond  = time.Nanosecond
	Second      = time.Second
	Week        = time.Hour * 24 * 7

	// date/time formats
	Layout = time.Layout // The reference time, in numerical order

	ANSIC       = time.ANSIC
	DateOnly    = time.DateOnly
	DateTime    = time.DateTime
	RFC822      = time.RFC822
	RFC822Z     = time.RFC822Z // RFC822 with numeric zone
	RFC850      = time.RFC850
	RFC1123     = time.RFC1123
	RFC1123Z    = time.RFC1123Z // RFC1123 with numeric zone
	RFC3339     = time.RFC3339
	RFC3339Nano = time.RFC3339Nano
	Kitchen     = time.Kitchen
	RubyDate    = time.RubyDate
	Stamp       = time.Stamp
	StampMilli  = time.StampMilli
	StampMicro  = time.StampMicro
	StampNano   = time.StampNano
	TimeOnly    = time.TimeOnly
	UnixDate    = time.UnixDate
)

Variables

View Source
var (
	// the following functions are aliases for the corresponding functions in the standard time package
	Date            = time.Date
	Parse           = time.Parse
	ParseDuration   = time.ParseDuration
	ParseInLocation = time.ParseInLocation
	Unix            = time.Unix
	UnixMicro       = time.UnixMicro
	UnixMilli       = time.UnixMilli
)
View Source
var (
	ErrClockAlreadyExists = errors.New("clock already exists")
	ErrClockIsRunning     = errors.New("clock is running")
	ErrClockNotRunning    = errors.New("clock is stopped")
	ErrNotADelorean       = errors.New("not a DeLorean clock (cannot go back in time)")
)

Functions

func ContextWithClock

func ContextWithClock(ctx context.Context, c Clock) context.Context

ContextWithClock returns a new context containing a given clock.

  • If the context already contains a clock the function panics with ErrClockAlreadyExists.
  • If the given clock is nil a new context is returned with the system clock added.

func ContextWithDeadline

func ContextWithDeadline(ctx context.Context, t time.Time) (context.Context, context.CancelFunc)

ContextWithDeadline returns a new context with the given deadline. If the given time is in the past, the returned context is already done.

The deadline is set using the clock in the given context. If there is no clock in the context the system clock is used and the result is the same as calling context.WithDeadline.

If the context contains a mock clock, the deadline will expire when that mock clock is advanced to the deadline or later.

func ContextWithDeadlineCause

func ContextWithDeadlineCause(ctx context.Context, t time.Time, cause error) (context.Context, context.CancelFunc)

ContextWithDeadlineCause returns a new context with the given deadline and cause. If the given time is in the past, the returned context is already done.

The cause is used to set the context error.

The deadline is set using the clock in the given context. If there is no clock in the context the system clock is used and the result is the same as calling context.WithDeadlineCause.

If the context contains a mock clock, the deadline will expire when that mock clock is advanced to the deadline or later.

func ContextWithTimeout

func ContextWithTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)

ContextWithTimeout returns a new context with the given timeout. If the given duration is zero or negative, the returned context is already done.

The timeout is set using the clock in the given context. If there is no clock in the context the system clock is used and the result is the same as calling context.WithTimeout.

If the context contains a mock clock, the timeout will expire when that mock clock is advanced by at least the given duration from its current time.

func ContextWithTimeoutCause

func ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc)

ContextWithTimeoutCause returns a new context with the given timeout and cause. If the given duration is zero or negative, the returned context is already done.

The cause is used to set the context error.

The timeout is set using the clock in the given context. If there is no clock in the context the system clock is used and the result is the same as calling context.WithTimeoutCause.

If the context contains a mock clock, the timeout will expire when that mock clock is advanced by at least the given duration from its current time. The cause is used to set the context error.

func Sleep

func Sleep(ctx context.Context, d Duration)

Sleep suspends the calling goroutine for the duration specified.

If the clock in the context is a mock clock, the duration of the sleep may be modified by the configuration of the mock.

func Tick

func Tick(ctx context.Context, d Duration) <-chan Time

Types

type Clock

type Clock interface {
	// After returns a channel that will send the current time after at least
	// duration d.
	After(d time.Duration) <-chan time.Time

	// AfterFunc waits for the duration to elapse and then calls f in its own
	// goroutine. It returns a Timer that can be used to stop the countdown
	// or to reset the Timer to run at a different time.
	//
	// If the Timer is stopped, the function f will not be called.
	AfterFunc(d time.Duration, f func()) *Timer

	// NewTicker returns a new Ticker that will send the current time on its
	// channel after each tick. The duration d must be greater than zero; if
	// d <= 0, NewTicker will panic.
	//
	// The duration of the Ticker can be modified using the Reset method
	//
	// The Ticker will continue ticking until Stop is called on it.
	NewTicker(d time.Duration) *Ticker

	// NewTimer returns a new Timer that will send the current time on its
	// channel after the duration d. The duration d must be greater than zero;
	// if d <= 0, NewTimer will panic.
	//
	// The duration of the Timer can be modified using the Reset method.
	//
	// The Timer will tick at the designated time unless Stop is called on it
	// beforehand.  A stopped Timer will resume if it is reset.
	NewTimer(d time.Duration) *Timer

	// Now returns the current time.
	Now() time.Time

	// Since returns the duration since t, according to the current time.  It is
	// shorthand for time.Since(c.Now()).
	Since(t time.Time) time.Duration

	// Sleep pauses the calling goroutine for at least the duration d.
	Sleep(d time.Duration)

	// Tick returns a channel that will send the current time after each tick.
	// Unlike NewTicker, if the duration d is zero or negative, Tick will return
	// a nil channel and will not panic.
	Tick(d time.Duration) <-chan time.Time

	// Until returns the duration until t, according to the current time.  It is
	// shorthand for time.Until(c.Now()).
	Until(t time.Time) time.Duration

	// ContextWithDeadline returns a new context with the given deadline. If the
	// given time is in the past, the returned context is already done.
	//
	// This function should be used in preference over context.WithDeadline
	// to ensure that code relying on the deadline behaves correctly under test
	// conditions which may provide a mock clock in the parent context.
	ContextWithDeadline(ctx context.Context, d time.Time) (context.Context, context.CancelFunc)

	// ContextWithDeadlineCause returns a new context with the given deadline and
	// cause. If the given time is in the past, the returned context is already
	// done.
	//
	// The cause is used to set the context error.
	//
	// This function should be used in preference over context.WithDeadlineCause
	// to ensure that code relying on the deadline behaves correctly under test
	// conditions which may provide a mock clock in the parent context.
	ContextWithDeadlineCause(ctx context.Context, d time.Time, cause error) (context.Context, context.CancelFunc)

	// ContextWithTimeout returns a new context with the given timeout. If the
	// given duration is zero or negative, the returned context is already done.
	//
	// This function should be used in preference over context.WithTimeout
	// to ensure that code relying on the timeout behaves correctly under test
	// conditions which may provide a mock clock in the parent context.
	ContextWithTimeout(ctx context.Context, d time.Duration) (context.Context, context.CancelFunc)

	// ContextWithTimeoutCause returns a new context with the given timeout and
	// cause. If the given duration is zero or negative, the returned context is
	// already done.
	//
	// The cause is used to set the context error.
	//
	// This function should be used in preference over context.WithTimeoutCause
	// to ensure that code relying on the timeout behaves correctly under test
	// conditions which may provide a mock clock in the parent context.
	ContextWithTimeoutCause(ctx context.Context, d time.Duration, cause error) (context.Context, context.CancelFunc)
}

Clock represents an interface described by the functions in the time package of the standard library. It extends the time package with additional methods to create contexts with deadlines and timeouts based on the clock providing the interface.

This allows for the creation of mock clocks for testing purposes through an API that is similar to and consistent with that of the system clock in the standard library `time` package.

func ClockFromContext

func ClockFromContext(ctx context.Context) Clock

ClockFromContext returns the Clock in the given context. If no Clock is in the context the system clock is returned.

func SystemClock

func SystemClock() Clock

SystemClock returns a clock implementation that uses the `time` package functions of the standard library.

func TryClockFromContext

func TryClockFromContext(ctx context.Context) Clock

TryClockFromContext returns the Clock in the given context or nil if no Clock is present.

type ClockOption

type ClockOption func(*mockClock)

ClockOption represents an option that can be passed to NewMockClock.

func AtNow

func AtNow() ClockOption

AtNow is a convenience for AtTime(time.Now()).

This may be useful for testing purposes when you want to start the clock at the current time whilst retaining the ability to control the advancement of time.

The time is set in the location of the clock.

func AtTime

func AtTime(t time.Time) ClockOption

AtTime sets the initial time of the mock clock.

This may be useful for testing purposes when you want to start the clock at a particular time whilst retaining the ability to control the advancement of time. The time is set in the location of the clock.

Default

1970-01-01 00:00:00 +0000 UTC (in the location of the clock)

func DropsTicks

func DropsTicks() ClockOption

DropsTicks sets the mock clock to drop ticks when the clock is advanced. That is, if the clock is advanced by a duration that would ordinarily result in a ticker being triggered more than once, the clock will only trigger a single tick event for the final tick.

Example

When a ticker is set to tick every 300 ms and the clock is advanced by 1s:

  • in normal operation, the ticker will be triggered at 300ms, 600ms and 900ms.

  • with DropsTicks applied, the clock will only send a tick event for the final tick at 900ms.

This may be used to simulate a reader that is reading from a Ticker and failing to "keep up". This is not ideal since it is using the clock to simulate the reader behaviour but may be easier than contriving that reader behaviour in other ways for testing purposes.

Default

not set/disabled

func InLocation

func InLocation(loc *time.Location) ClockOption

InLocation sets the mock clock to the given location; the time returned by Now() will be in this location.

It is not normally necessary to set the location of the clock but may be useful when you want to start the clock in a particular location whilst retaining the ability to control the advancement of time.

Default

UTC

func StartRunning

func StartRunning() ClockOption

StartRunning sets the mock clock to start in a running state. In this state the clock is advanced by elapsed time whenever Now() is obtained from the clock or when Update() is explicitly called.

AdvanceBy() and AdvanceTo() are not supported when the clock is in a running state and will panic.

This more closely mimics the behaviour of a real clock but means that tests will run in real-time; this is not recommended for most tests as it will make them run more slowly than they might.

Default

not set / stopped

func Yielding

func Yielding(d time.Duration) ClockOption

Yielding sets a duration for which the calling goroutine will be suspended when performing operations such as advancing the clock or adding a timer or ticker.

This allows other goroutines to be scheduled at times when it may be useful for a test. The duration should rarely need to be changed and should not be set to a value that is too high as this will cause a test to run more slowly than it might.

To disable this behaviour (not recommended) set the duration to 0.

Default

1ms

type Duration

type Duration = time.Duration

the following types are aliases for the corresponding types in the standard time package

type Location

type Location = time.Location

type MockClock

type MockClock interface {
	// MockClock is a mock implementation of the time.Clock interface.
	Clock

	// AdvanceBy moves the current time of the mock clock forward by a
	// specified duration, triggering any timers or tickers that would have
	// been triggered during that passage of time.
	//
	// Calling this method while the clock is running will result in a panic.
	AdvanceBy(d time.Duration)

	// AdvanceTo moves the current time of the mock clock to a specific time,
	// triggering any timers or tickers that would have been triggered during
	// that passage of time.
	//
	// Calling this method while the clock is running will result in a panic.
	AdvanceTo(t time.Time)

	// CreatedAt returns the mocked time at which the clock was started when created.
	CreatedAt() time.Time

	// IsRunning returns true if the clock is in a running state.
	// In this state the clock is advanced by elapsed time whenever Now()
	// is obtained from the clock or when Update() is explicitly called.
	// AdvanceBy() and AdvanceTo() are not supported when the clock is in a
	// running state and will panic.
	//
	// This more closely mimics the behaviour of a real clock but tests using
	// a running clock may be less deterministic and run more slowly than
	// they might.
	IsRunning() bool

	// SinceCreated returns the elapsed mock time since the clock was created.
	// This is the same as calling clock.Since(clock.CreatedAt()).
	SinceCreated() time.Duration

	// Stop stops the clock from advancing automatically.  Every call to
	// Stop() must be matched with a call to Start() to resume automatic
	// advancement.
	//
	// A MockClock is initially created in stopped mode unless the StartRunning
	// option is specified when initialising the clock.
	Stop()

	// Start resumes automatic advancement of the clock.  Every call to
	// Start() must be matched with a call to Stop() to stop automatic
	// advancement.
	//
	// A MockClock is initially created in stopped mode unless the StartRunning
	// option is specified when initialising the clock.  i.e. if the clock
	// is created in stopped mode, an initial call to Start() is required to
	// start the clock.
	Start()

	// Update moves the current time of the mock clock forward by a duration
	// corresponding to the passage of real-time since it was last updated,
	// triggering any timers or tickers that would have been triggered during
	// that passage of time.
	//
	// Calling this method while the clock is stopped will result in a panic.
	Update()
}

MockClock extends the Clock interface with methods to manipulate the current time of the clock. In normal use, the underlying clock time is advanced only when explicitly directed to do so using AdvanceBy() or AdvanceTo() methods; this is "stopped" mode.

When the clock is "running" the current time is advanced semi-automatically by the passage of real-time since the last time the clock was updated. In "running" mode, the clock is advanced any time that Now() is called, or by calling Update().

It is used to simulate the passage of time in tests.

func ContextWithMockClock

func ContextWithMockClock(parent context.Context, opts ...ClockOption) (context.Context, MockClock)

ContextWithMockClock returns a new context with a mock clock configured with the given options. If the parent context already has a clock the function panics with ErrClockAlreadyExists.

This function is provided as a convenience when writing tests requiring a mock clock.

func NewMockClock

func NewMockClock(options ...ClockOption) MockClock

NewMockClock returns an instance of a mock clock.

The default settings on a new clock are:

  • inital time set to the UNIX epoch (00:00:00 UTC on Thursday, 1 Jan 1970)
  • stopped; advance with AdvanceBy() or AdvanceTo()
  • does not drop ticks
  • sleeps the calling goroutine for 1ms on various operations

When stopped, the clock must be explicitly advanced using AdvanceBy() or AdvanceTo(). When not stopped Update() may be used to advance the clock by the elapsed real-time since the last advancement.

The clock can be customised using the provided options:

  • AtNow() sets the initial time of the mock clock to the current time;

  • AtTime(t time.Time) sets the initial time of the mock clock;

  • DropsTicks() sets the clock to fire tickers only once where multiple ticks would have been triggered by a single advance of the clock

  • WithYield(d time.Duration) sets a duration for which the calling goroutine is suspended before and after each advancement of the clock.

  • StartRunning() sets the mock clock to start in a running state; in the running state the clock is advanced by elapsed time whenever Now() is obtained from the clock or when Update() is explicitly called. AdvanceBy() and AdvanceTo() are not supported in the running state and will panic.

type Month

type Month = time.Month

type ParseError

type ParseError = time.ParseError

type Ticker

type Ticker struct {
	// wraps a time.Timer in normal use; for a mock, this is non-nil but is
	// used only as a container for the <-chan time.Time read-only reference
	// to the mock timer's channel.
	*time.Ticker
	// contains filtered or unexported fields
}

Ticker represents a ticker; it may be obtained from SystemClock() or a mock obtained from a MockClock.

Usage is the same in either case and is identical to the time.Ticker type in the standard library: the time of each "tick" is read from the channel `C` provided on the Ticker.

A ticker created from a mock clock will tick when the associated mock clock is advanced to (or beyond) the next tick time.

If a mock clock is advanced by a duration that is greater than the period of the ticker, the ticker will tick at each interval unless the clock was configured to drop ticks. In that case, the Ticker will tick only once at the last time at/before the time advanced to.

Example
package main

import (
	"fmt"
	"math"

	"github.com/blugnu/time"
)

func main() {
	// using the system clock
	clock := time.SystemClock()
	start := clock.Now()

	// create a ticker that ticks every 100ms
	ticker := clock.NewTicker(100 * time.Millisecond)
	defer ticker.Stop()

	// output the number and time (since starting) of each tick
	n := 0
	go func() {
		for range ticker.C {
			n++
			fmt.Printf("fire %d @ ~%dms\n", n, 10*int(math.Trunc(float64(clock.Since(start).Milliseconds())/10)))
		}
	}()

	// wait for a little over 500ms
	clock.Sleep(510 * time.Millisecond)

}
Output:

fire 1 @ ~100ms
fire 2 @ ~200ms
fire 3 @ ~300ms
fire 4 @ ~400ms
fire 5 @ ~500ms

func NewTicker

func NewTicker(ctx context.Context, d Duration) *Ticker

func (*Ticker) Reset

func (t *Ticker) Reset(d time.Duration)

Reset resets the ticker to the specified duration.

If the Ticker has been stopped it is restarted with the new duration.

If the Ticker is already running it will be reset to the new duration; the next tick will occur at the specified duration from the current time.

the function panics if the given duration is zero or negative, or if the Ticker has not been initialized, .

func (*Ticker) Stop

func (t *Ticker) Stop()

Stop stops the ticker and prevents any further ticks from being sent to the channel; the channel is not closed.

type Time

type Time = time.Time

func Now

func Now(ctx context.Context) Time

Now returns the current time from the Clock in the given context. If there is no clock in the context, the real-time clock will be used.

type Timer

type Timer struct {
	// wraps a time.Timer in normal use; for a mock, this is non-nil but is
	// used only as a container for the <-chan time.Time read-only reference
	// to the mock timer's channel.
	*time.Timer
	// contains filtered or unexported fields
}

Timer represents a timer; it may obtained from the SystemClock() or a mock obtained from a MockClock.

Usage is the same in either case and is identical to the time.Timer type in the standard library: the time of the timer is read from the channel `C` provided on the Timer.

A timer created from a mock clock will tick when the associated mock clock is advanced to (or beyond) the time specified on the Timer.

func AfterFunc

func AfterFunc(ctx context.Context, d Duration, f func()) *Timer

func NewTimer

func NewTimer(ctx context.Context, d Duration) *Timer

func (*Timer) Reset

func (t *Timer) Reset(d time.Duration) bool

Reset modifies the timer to expire after duration d from the current time. If the timer has already expired it is re-activated.

Returns true if the timer was already active, false if the timer had expired or been stopped (and was re-activated).

func (*Timer) Stop

func (t *Timer) Stop() 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.

type Weekday

type Weekday = time.Weekday

Jump to

Keyboard shortcuts

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