-
Notifications
You must be signed in to change notification settings - Fork 0
/
nanotime.go
58 lines (47 loc) · 1.59 KB
/
nanotime.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (C) 2016 Arista Networks, Inc.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
// Package monotime provides a fast monotonic clock source.
package monotime
import (
"time"
_ "unsafe" // required to use //go:linkname
)
//go:noescape
//go:linkname nanotime runtime.nanotime
func nanotime() int64
// Time is a point in time represented in nanoseconds.
type Time int64
// Now returns the current time in nanoseconds from a monotonic clock.
// The time returned is based on some arbitrary platform-specific point in the
// past. The time returned is guaranteed to increase monotonically at a
// constant rate, unlike time.Now() from the Go standard library, which may
// slow down, speed up, jump forward or backward, due to NTP activity or leap
// seconds.
func Now() Time {
return Time(nanotime())
}
// Since is analogous to https://golang.org/pkg/time/#Since
func Since(t Time) time.Duration {
return time.Duration(Now() - t)
}
// Add is analogous to https://golang.org/pkg/time/#Time.Add
func (t Time) Add(d time.Duration) Time {
return t + Time(d)
}
// Sub is analogous to https://golang.org/pkg/time/#Time.Sub
func (t Time) Sub(s Time) time.Duration {
return time.Duration(t - s)
}
// Before is analogous to https://golang.org/pkg/time/#Time.Before
func (t Time) Before(u Time) bool {
return t < u
}
// After is analogous to https://golang.org/pkg/time/#Time.After
func (t Time) After(u Time) bool {
return t > u
}
// Equal is analogous to https://golang.org/pkg/time/#Time.Equal
func (t Time) Equal(u Time) bool {
return t == u
}