mirror of
https://github.com/kovidgoyal/kitty.git
synced 2026-07-03 11:12:30 +08:00
Only use raw monotonic time on Linux and macOS
This commit is contained in:
18
tools/utils/clock_with_raw.go
Normal file
18
tools/utils/clock_with_raw.go
Normal file
@@ -0,0 +1,18 @@
|
||||
//go:build linux || darwin
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func MonotonicRaw() (time.Time, error) {
|
||||
ts := unix.Timespec{}
|
||||
if err := unix.ClockGettime(unix.CLOCK_MONOTONIC_RAW, &ts); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
s, ns := ts.Unix()
|
||||
return time.Unix(s, ns), nil
|
||||
}
|
||||
18
tools/utils/clock_without_raw.go
Normal file
18
tools/utils/clock_without_raw.go
Normal file
@@ -0,0 +1,18 @@
|
||||
//go:build !linux && !darwin
|
||||
|
||||
package utils
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func MonotonicRaw() (time.Time, error) {
|
||||
ts := unix.Timespec{}
|
||||
if err := unix.ClockGettime(unix.CLOCK_MONOTONIC, &ts); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
s, ns := ts.Unix()
|
||||
return time.Unix(s, ns), nil
|
||||
}
|
||||
@@ -10,11 +10,9 @@ import (
|
||||
"reflect"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"golang.org/x/exp/constraints"
|
||||
"golang.org/x/exp/slices"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
var _ = fmt.Print
|
||||
@@ -312,16 +310,3 @@ func FunctionName(a any) string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func MonotonicRaw() (time.Time, error) {
|
||||
ts := unix.Timespec{}
|
||||
var clock_id int32 = unix.CLOCK_MONOTONIC
|
||||
if runtime.GOOS == "linux" {
|
||||
clock_id = unix.CLOCK_MONOTONIC_RAW
|
||||
}
|
||||
if err := unix.ClockGettime(clock_id, &ts); err != nil {
|
||||
return time.Time{}, err
|
||||
}
|
||||
s, ns := ts.Unix()
|
||||
return time.Unix(s, ns), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user