Make getting the up-time more portable across unix-like systems

This commit is contained in:
qiu-x 2021-11-06 10:03:51 +01:00
parent 41f8406484
commit 18215381dd
3 changed files with 34 additions and 4 deletions

View File

@ -4,7 +4,6 @@ import (
"regexp"
"strconv"
"strings"
"syscall"
"time"
"github.com/liamg/darktile/internal/app/darktile/termutil"
@ -52,7 +51,5 @@ func (h *DmesgTimestampHinter) Click(api HintAPI) error {
}
func setSysStartTime() {
sysInfo := &syscall.Sysinfo_t{}
_ = syscall.Sysinfo(sysInfo)
sysStart = time.Now().Local().Add(time.Duration(int(sysInfo.Uptime*-1)) * time.Second)
sysStart = time.Now().Local().Add(time.Duration(int(getUptime()*-1)) * time.Second)
}

View File

@ -0,0 +1,22 @@
//go:build cgo && (!linux || unix)
package hinters
/*
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/timespec.h>
time_t getuptime() {
struct timespec tp;
clock_gettime(CLOCK_UPTIME, &tp);
return tp.tv_sec;
}
*/
import "C"
func getUptime() int64 {
time := C.getuptime()
return int64(time)
}

View File

@ -0,0 +1,11 @@
package hinters
import (
"syscall"
)
func getUptime() int64 {
sysInfo := &syscall.Sysinfo_t{}
_ = syscall.Sysinfo(sysInfo)
return sysInfo.Uptime
}