github.com/ScaleFT/monotime@v0.0.0-20180209174256-2da272f3dad0/README.md (about) 1 # monotime 2 3 [![GoDoc](https://godoc.org/github.com/ScaleFT/monotime?status.svg)](https://godoc.org/github.com/ScaleFT/monotime) 4 [![Build Status](https://travis-ci.org/ScaleFT/monotime.svg?branch=master)](https://travis-ci.org/ScaleFT/monotime) 5 6 `monotime` provides a portable monotonic timer for Go / Golang. 7 8 - Linux: Uses `runtime.nanotime` 9 - Windows: Uses `QueryPerformanceCounter` [docs](https://msdn.microsoft.com/en-us/library/windows/desktop/ms644904(v=vs.85).aspx) 10 - macOS: Uses `mach_absolute_time` [docs](https://developer.apple.com/library/content/qa/qa1398/_index.html) (requires CGO) 11 - FreeBSD: Uses `clock_gettime(CLOCK_MONOTONIC)` [docs](https://www.freebsd.org/cgi/man.cgi?query=clock_gettime) 12 13 ## About `runtime.nanotime` 14 15 The Go standard library provides `time.Now` for getting the current time, 16 but this timestamp is not monotonic -- it can jump forward or backwards depending 17 on the operating systems' time synchronization. The standard library 18 does not provide a public API for getting the monotonic time from the operating system, 19 but on some operating systems `runtime.nanotime`, a private method, is available, and 20 generally is the fastest method to get a monotonic time. However, on many operating systems 21 including macOS and Windows, `runtime.nanotime` doesn't actually implement a monotonic timer. 22 23 When possible this library uses `runtime.nanotime`, but on platforms where it is not available 24 `monotime` may use other methods. 25 26 See the following `golang/go` issues for more background: 27 28 - [#12914: runtime: time: expose monotonic clock source](https://github.com/golang/go/issues/12914) 29 - [#17610: runtime: use mach_absolute_time for runtime.nanotime](https://github.com/golang/go/issues/17610) 30 - [#6007: time: use monotonic time on netbsd](https://github.com/golang/go/issues/6007)