chronometry

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 1 Imported by: 2

README

Chronometry

Package for time-keeping purposes.

Features

Stopwatch

A monotonically stable stopwatch implementation in Golang which resembles a real-life stopwatch, best explained with some sample code:

sw := chronometry.NewStartedStopwatch()
time.Sleep(time.Second)
sw.TotalTime() // 1 second
sw.TakeLapTime() // 1 second
time.Sleep(time.Second)
sw.Stop()
sw.TotalTime() // 2 seconds
sw.TakeLapTime() // 1 second
time.Sleep(time.Second)
sw.TotalTime() // 2 seconds
sw.TakeLapTime() // 0 seconds
sw.Resume()
time.Sleep(time.Second)
sw.Stop()
time.Sleep(time.Second)
sw.Resume()
time.Sleep(time.Second)
sw.TotalTime() // 4 seconds
sw.TakeLapTime() // 2 seconds
sw.Restart()
sw.TotalTime() // 0 seconds

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NowFunc = time.Now

NowFunc specifies the function used to retrieve the current time. It is changed from time.Now only during tests.

Functions

This section is empty.

Types

type Stopwatch

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

Stopwatch implements a stopwatch which can be stopped, resumed and reset and can report its current reading of time with TotalTime() as well as allow for convenient measuring of consecutive intervals using TakeLapTime().

func NewStartedStopwatch

func NewStartedStopwatch() *Stopwatch

NewStartedStopwatch returns a started stopwatch, ready to report TotalTime() and TakeLapTime().

func NewStoppedStopwatch

func NewStoppedStopwatch() *Stopwatch

NewStoppedStopwatch returns a stopwatch which needs to have Restart() or Resume() called on it in order to begin counting time.

func (*Stopwatch) CurrentSegmentDuration

func (sw *Stopwatch) CurrentSegmentDuration() time.Duration

CurrentSegmentDuration returns the duration since the stopwatch was last started/restarted, reset, resumed or had TakeLapTime() called.

func (*Stopwatch) IsRunning

func (sw *Stopwatch) IsRunning() bool

IsRunning reports whether the stopwatch is running, i.e. not stopped.

func (*Stopwatch) IsStopped

func (sw *Stopwatch) IsStopped() bool

IsStopped reports whether the stopwatch is stopped, i.e. not running.

func (*Stopwatch) MakeSplit

func (sw *Stopwatch) MakeSplit() (lapTime, totalTime time.Duration)

MakeSplit does the same as TakeLapTime(), but also returns the exact according total time.

func (*Stopwatch) ReadingTime

func (sw *Stopwatch) ReadingTime() time.Time

ReadingTime returns the time at which the stopwatch was last started/restarted, reset, resumed or had TakeLapTime() called.

func (*Stopwatch) Reset

func (sw *Stopwatch) Reset()

Reset sets the stopwatch reading back to zero.

func (*Stopwatch) Restart

func (sw *Stopwatch) Restart()

Restart resets and resumes the stopwatch.

func (*Stopwatch) Resume

func (sw *Stopwatch) Resume()

Resume resumes the stopwatch when it is stopped.

func (*Stopwatch) StartTime

func (sw *Stopwatch) StartTime() time.Time

StartTime returns the time at which the stopwatch was last started/restarted, reset or resumed.

func (*Stopwatch) Stop

func (sw *Stopwatch) Stop() time.Duration

Stop stops the stopwatch, causing it to stop observing the passing of time. It can be resumed with Resume().

func (*Stopwatch) StopTime

func (sw *Stopwatch) StopTime() time.Time

StopTime returns the time at which the stopwatch was last stopped, started/restarted or reset.

func (*Stopwatch) TakeLapTime

func (sw *Stopwatch) TakeLapTime() time.Duration

TakeLapTime makes a split, returning the change in TotalTime() since last calling this function or starting/restarting or resetting the stopwatch.

func (*Stopwatch) TotalTime

func (sw *Stopwatch) TotalTime() time.Duration

TotalTime returns the current stopwatch reading, i.e. the total passed time observed by the stopwatch while not stopped.

Jump to

Keyboard shortcuts

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