github.com/psiphon-Labs/psiphon-tunnel-core@v2.0.28+incompatible/psiphon/common/monotime/nanotime.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 a fast 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 // Time is a point in time represented in nanoseconds. 18 type Time int64 19 20 // Now returns the current time in nanoseconds from a monotonic clock. 21 // The time returned is based on some arbitrary platform-specific point in the 22 // past. The time returned is guaranteed to increase monotonically at a 23 // constant rate, unlike time.Now() from the Go standard library, which may 24 // slow down, speed up, jump forward or backward, due to NTP activity or leap 25 // seconds. 26 func Now() Time { 27 return Time(nanotime()) 28 } 29 30 // Since is analogous to https://golang.org/pkg/time/#Since 31 func Since(t Time) time.Duration { 32 return time.Duration(Now() - t) 33 } 34 35 // Add is analogous to https://golang.org/pkg/time/#Time.Add 36 func (t Time) Add(d time.Duration) Time { 37 return t + Time(d) 38 } 39 40 // Sub is analogous to https://golang.org/pkg/time/#Time.Sub 41 func (t Time) Sub(s Time) time.Duration { 42 return time.Duration(t - s) 43 } 44 45 // Before is analogous to https://golang.org/pkg/time/#Time.Before 46 func (t Time) Before(u Time) bool { 47 return t < u 48 } 49 50 // After is analogous to https://golang.org/pkg/time/#Time.After 51 func (t Time) After(u Time) bool { 52 return t > u 53 } 54 55 // Equal is analogous to https://golang.org/pkg/time/#Time.Equal 56 func (t Time) Equal(u Time) bool { 57 return t == u 58 }