-
Notifications
You must be signed in to change notification settings - Fork 18.3k
Description
This will be short a suite as it's pretty basic.
There is appetite to have access to Monotonic Time in Go from previous issues, proposals and requests.
Proposed Additions
I propose adding a new type to the time
package called Instant
which will represent an opaque type that can only be compared with one another and allows measuring of duration. An existing implementation can be found here exert posted below for convenience.
package timeext
import "time"
// Instant represents a monotonic instant in time.
//
// Instants are opaque types that can only be compared with one another and allows measuring of duration.
type Instant int64
// NewInstant returns a new Instant.
func NewInstant() Instant {
return Instant(NanoTime())
}
// Elapsed returns the duration since the instant was created.
func (i Instant) Elapsed() time.Duration {
return time.Duration(NewInstant() - i)
}
// Since returns the duration elapsed from another Instant, or zero is that Instant is later than this one.
func (i Instant) Since(instant Instant) time.Duration {
if instant > i {
return 0
}
return time.Duration(i - instant)
}
This will allow access to Monotonic Time from Go, that already exists, but yet unexposed except through time.Time
. The reason this is necessary is that time.Time
is a very heavy structure and has a bunch of overhead that is not necessary if only measuring monotonically increasing time such as when used in Benchmarking and very hot code paths.