Make getting the up-time more portable across unix-like systems (#326)

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

* Fix the build on NetBSD

* Update Go version in workflows
This commit is contained in:
Qiu 2021-12-17 09:39:48 +01:00 committed by GitHub
parent 47c91e0002
commit 97b6a41eb3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 39 additions and 6 deletions

View File

@ -16,7 +16,7 @@ jobs:
- uses: actions/setup-go@v2 - uses: actions/setup-go@v2
with: with:
go-version: '^1.16.6' go-version: '^1.17'
- run: go version - run: go version
- name: Release - name: Release

View File

@ -4,7 +4,7 @@ jobs:
test: test:
strategy: strategy:
matrix: matrix:
go-version: [1.16.x] go-version: [1.17.x]
os: [ubuntu-latest] os: [ubuntu-latest]
runs-on: ${{ matrix.os }} runs-on: ${{ matrix.os }}
steps: steps:

View File

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

View File

@ -0,0 +1,23 @@
//go:build cgo && (freebsd || openbsd)
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,13 @@
//go:build cgo && (linux || netbsd)
package hinters
import (
"syscall"
)
func getUptime() int64 {
sysInfo := &syscall.Sysinfo_t{}
_ = syscall.Sysinfo(sysInfo)
return sysInfo.Uptime
}