nowish

package module
v1.1.2 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2022 License: MIT Imports: 4 Imported by: 2

README

go-nowish

Simply Go libary with useful time utilies:

  • Clock: blazing-fast clock giving good (ish) representations of the now time.
  • Timeout: a reusable timeout structure useful for extending timeouts.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Clock

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

Clock provides a clock capable of returning current times up to a certain level of precision, far faster than using time.Now(). It does this by performing this regularly and caching the result, same for the formatted time string. All operations are atomic and protected by mutexes where necessary :).

func Start

func Start(precision time.Duration) (*Clock, func())

Start returns a new Clock instance initialized and started with the provided precision, along with the stop function for it's underlying timer.

func (*Clock) Now

func (c *Clock) Now() time.Time

Now returns a good (ish) estimate of the current 'now' time.

func (*Clock) NowFormat

func (c *Clock) NowFormat() string

NowFormat returns the formatted "now" time, cached until next tick and "now" updates.

func (*Clock) SetFormat

func (c *Clock) SetFormat(format string)

SetFormat sets the time format string used by .NowFormat().

func (*Clock) Start

func (c *Clock) Start(precision time.Duration) func()

Start starts the clock with the provided precision, the returned function is the stop function for the underlying timer. For >= 2ms, actual precision is usually within AT LEAST 10% of requested precision, less than this and the actual precision very quickly deteriorates.

type Timeout

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

Timeout provides a reusable structure for enforcing timeouts with a cancel.

func NewTimeout

func NewTimeout() Timeout

NewTimeout returns a new Timeout instance.

func (*Timeout) Cancel

func (t *Timeout) Cancel()

Cancel cancels the currently running timer. If a cancel is achieved, then this function will return after the timeout goroutine is finished.

func (*Timeout) Extend added in v1.1.0

func (t *Timeout) Extend(d time.Duration) bool

Extend will attempt to extend the timeout runner's time, returns false if not running.

func (*Timeout) Start

func (t *Timeout) Start(d time.Duration, hook func())

Start starts the timer with supplied timeout. If timeout is reached before cancel then supplied timeout hook will be called. Panic will be called if Timeout is already running when calling this function.

Jump to

Keyboard shortcuts

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