-
Notifications
You must be signed in to change notification settings - Fork 11
/
now_windows.go
47 lines (40 loc) · 1.22 KB
/
now_windows.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
package hrtime
import (
"syscall"
"time"
"unsafe"
)
// precision timing
var (
modkernel32 = syscall.NewLazyDLL("kernel32.dll")
procFreq = modkernel32.NewProc("QueryPerformanceFrequency")
procCounter = modkernel32.NewProc("QueryPerformanceCounter")
qpcFrequency = getFrequency()
qpcBase = getCount()
)
// getFrequency returns frequency in ticks per second.
func getFrequency() int64 {
var freq int64
r1, _, _ := syscall.Syscall(procFreq.Addr(), 1, uintptr(unsafe.Pointer(&freq)), 0, 0)
if r1 == 0 {
panic("call failed")
}
return freq
}
// getCount returns counter ticks.
func getCount() int64 {
var qpc int64
syscall.Syscall(procCounter.Addr(), 1, uintptr(unsafe.Pointer(&qpc)), 0, 0)
return qpc
}
// Now returns current time.Duration with best possible precision.
//
// Now returns time offset from a specific time.
// The values aren't comparable between computer restarts or between computers.
func Now() time.Duration {
return time.Duration(getCount()-qpcBase) * time.Second / (time.Duration(qpcFrequency) * time.Nanosecond)
}
// NowPrecision returns maximum possible precision for Now in nanoseconds.
func NowPrecision() float64 {
return float64(time.Second) / (float64(qpcFrequency) * float64(time.Nanosecond))
}