...
1
2
3
4
19
20 package util
21
22 import (
23 "fmt"
24 "syscall"
25 "time"
26 "unsafe"
27
28 "golang.org/x/sys/unix"
29 )
30
31
32 func GetBootTime() (time.Time, error) {
33 output, err := unix.SysctlRaw("kern.boottime")
34 if err != nil {
35 return time.Time{}, err
36 }
37 var timeval syscall.Timeval
38 if len(output) != int(unsafe.Sizeof(timeval)) {
39 return time.Time{}, fmt.Errorf("unexpected output when calling syscall kern.bootime. Expected len(output) to be %v, but got %v",
40 int(unsafe.Sizeof(timeval)), len(output))
41 }
42 timeval = *(*syscall.Timeval)(unsafe.Pointer(&output[0]))
43 sec, nsec := timeval.Unix()
44 return time.Unix(sec, nsec).Truncate(time.Second), nil
45 }
46
View as plain text