monotime

Measuring time intervals corretly in Go (monotonic clock).

monotime GoDoc Travis

This tiny Go package is a standalone and slightly enhanced version of goarista/monotime.

It provides monotime.Now() function, which returns current time from monotonic clock source. It’s implemented using unexported runtime.nanotime() function from Go runtime. It works on all platforms.

Why?

time.Now() function from standard library returns real time (CLOCK_REALTIME in POSIX) which can jump forwards and backwards as the system time is changed.

For time measurements, monotonic time (CLOCK_MONOTONIC or CLOCK_MONOTONIC_RAW on Linux) is often preferred, which is strictly increasing, without (notable) jumps.

Documentation

See GoDoc.

Usage example

package main

import (
    "fmt"
    "time"

    "github.com/gavv/monotime"
)

func main() {
    var start, elapsed time.Duration

    start = monotime.Now()
    time.Sleep(time.Millisecond)
    elapsed = monotime.Since(start)

    fmt.Println(elapsed)
    // Prints: 1.062759ms
}

Similar packages

License

Apache 2.0

Related Repositories

monotime

monotime

Measuring time intervals corretly in Go (monotonic clock). ...

monotime

monotime

monotonic timers for Go 1.2 ...

monotime

monotime

Money in reference to time ...


Top Contributors

gavv EricLagergren