Skip to content

Commit 377d394

Browse files
committed
fix Now overflow on windows
1 parent 586f5fa commit 377d394

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module github.com/loov/hrtime
22

3-
go 1.12
3+
go 1.24.0

now_windows.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package hrtime
22

33
import (
4+
"math/bits"
45
"syscall"
56
"time"
67
"unsafe"
@@ -19,7 +20,7 @@ var (
1920
// getFrequency returns frequency in ticks per second.
2021
func getFrequency() int64 {
2122
var freq int64
22-
r1, _, _ := syscall.Syscall(procFreq.Addr(), 1, uintptr(unsafe.Pointer(&freq)), 0, 0)
23+
r1, _, _ := syscall.SyscallN(procFreq.Addr(), uintptr(unsafe.Pointer(&freq)))
2324
if r1 == 0 {
2425
panic("call failed")
2526
}
@@ -29,7 +30,7 @@ func getFrequency() int64 {
2930
// getCount returns counter ticks.
3031
func getCount() int64 {
3132
var qpc int64
32-
syscall.Syscall(procCounter.Addr(), 1, uintptr(unsafe.Pointer(&qpc)), 0, 0)
33+
syscall.SyscallN(procCounter.Addr(), uintptr(unsafe.Pointer(&qpc)))
3334
return qpc
3435
}
3536

@@ -38,7 +39,10 @@ func getCount() int64 {
3839
// Now returns time offset from a specific time.
3940
// The values aren't comparable between computer restarts or between computers.
4041
func Now() time.Duration {
41-
return time.Duration(getCount()-qpcBase) * time.Second / (time.Duration(qpcFrequency) * time.Nanosecond)
42+
delta := uint64(getCount() - qpcBase)
43+
hi, lo := bits.Mul64(delta, uint64(time.Second))
44+
quo, _ := bits.Div64(hi, lo, uint64(qpcFrequency))
45+
return time.Duration(quo)
4246
}
4347

4448
// NowPrecision returns maximum possible precision for Now in nanoseconds.

0 commit comments

Comments
 (0)