github.com/gavv/monotime@v0.0.0-20190418164738-30dba4353424/README.md (about)

     1  # monotime [![GoDoc](https://godoc.org/github.com/gavv/monotime?status.svg)](https://godoc.org/github.com/gavv/monotime) [![Travis](https://img.shields.io/travis/gavv/monotime.svg)](https://travis-ci.org/gavv/monotime)
     2  
     3  This tiny Go package is a standalone and slightly enhanced version of [`goarista/monotime`](https://github.com/aristanetworks/goarista#monotime).
     4  
     5  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.
     6  
     7  ## Not needed for Go 1.9+
     8  
     9  Starting from Go 1.9, the standard `time` package transparently uses [Monotonic Clocks](https://golang.org/pkg/time/#hdr-Monotonic_Clocks) when necessary, so this package is no longer relevant.
    10  
    11  This repository has been archived and is no longer maintained.
    12  
    13  ## Synopsis
    14  
    15  In Go versions before 1.9, `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.
    16  
    17  For time measurements, *monotonic time* (`CLOCK_MONOTONIC` or `CLOCK_MONOTONIC_RAW` on Linux) is often preferred, which is strictly increasing, without (notable) jumps.
    18  
    19  ## Documentation
    20  
    21  See [GoDoc](https://godoc.org/github.com/gavv/monotime).
    22  
    23  ## Usage example
    24  
    25  ```go
    26  package main
    27  
    28  import (
    29      "fmt"
    30      "time"
    31  
    32      "github.com/gavv/monotime"
    33  )
    34  
    35  func main() {
    36      var start, elapsed time.Duration
    37  
    38      start = monotime.Now()
    39      time.Sleep(time.Millisecond)
    40      elapsed = monotime.Since(start)
    41  
    42      fmt.Println(elapsed)
    43      // Prints: 1.062759ms
    44  }
    45  ```
    46  
    47  ## Similar packages
    48  
    49  * [`aristanetworks/goarista/monotime`](https://github.com/aristanetworks/goarista#monotime) (this package is based on it)
    50  * [`spacemonkeygo/monotime`](https://github.com/spacemonkeygo/monotime) (current `runtime.nanotime()` is more complete)
    51  * [`davecheney/junk/clock`](https://github.com/davecheney/junk/tree/master/clock) (Linux-only)
    52  * [`jaracil/clk`](https://github.com/jaracil/clk) (Linux-only)
    53  
    54  ## License
    55  
    56  [Apache 2.0](https://github.com/gavv/monotime/blob/master/LICENSE)