github.com/mkevac/monotime@v0.0.0-20161125163931-92ece95d55b6/monotime.go (about) 1 // Based on code from: https://github.com/aristanetworks/goarista/blob/46272bfb1c042fc2825d312fe33d494e9d13dd6b/atime/nanotime.go 2 3 // Copyright (C) 2016 Arista Networks, Inc. 4 // Use of this source code is governed by the Apache License 2.0 5 // that can be found in the COPYING file. 6 7 // Package monotime provides a fast monotonic clock source. 8 package monotime 9 10 import ( 11 "time" 12 "unsafe" 13 ) 14 15 // Make goimports import the unsafe package, which is required to be able 16 // to use //go:linkname 17 var _ = unsafe.Sizeof(0) 18 19 //go:noescape 20 //go:linkname nanotime runtime.nanotime 21 func nanotime() int64 22 23 // Nanoseconds returns the current time in nanoseconds from a monotonic clock. 24 // The time returned is based on some arbitrary platform-specific point in the 25 // past. The time returned is guaranteed to increase monotonically at a 26 // constant rate, unlike time.Now() from the Go standard library, which may 27 // slow down, speed up, jump forward or backward, due to NTP activity or leap 28 // seconds. 29 func Nanoseconds() uint64 { 30 return uint64(nanotime()) 31 } 32 33 // Now returns current time from a monotonic clock 34 // i.e. the result of Nanoseconds() as time.Time 35 func Now() time.Time { 36 t := time.Time{} 37 return t.Add(time.Duration(Nanoseconds()) * time.Nanosecond) 38 }