github.com/gavv/monotime@v0.0.0-20190418164738-30dba4353424/monotime.go (about) 1 // Copyright (C) 2016 Arista Networks, Inc. 2 // Use of this source code is governed by the Apache License 2.0 3 // that can be found in the COPYING file. 4 5 // Package monotime provides functions to access monotonic clock source. 6 package monotime 7 8 import ( 9 "time" 10 _ "unsafe" // required to use //go:linkname 11 ) 12 13 //go:noescape 14 //go:linkname nanotime runtime.nanotime 15 func nanotime() int64 16 17 // Now returns the current time in nanoseconds from a monotonic clock. 18 // 19 // The time returned is based on some arbitrary platform-specific point in the 20 // past. The time returned is guaranteed to increase monotonically without 21 // notable jumps, unlike time.Now() from the Go standard library, which may 22 // jump forward or backward significantly due to system time changes or leap 23 // seconds. 24 // 25 // It's implemented using runtime.nanotime(), which uses CLOCK_MONOTONIC on 26 // Linux. Note that unlike CLOCK_MONOTONIC_RAW, CLOCK_MONOTONIC is affected 27 // by time changes. However, time changes never cause clock jumps; instead, 28 // clock frequency is adjusted slowly. 29 func Now() time.Duration { 30 return time.Duration(nanotime()) 31 } 32 33 // Since returns the time elapsed since t, obtained previously using Now. 34 func Since(t time.Duration) time.Duration { 35 return Now() - t 36 }