You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
29 lines
570 B
29 lines
570 B
1 year ago
|
//go:build linux
|
||
|
|
||
2 years ago
|
package themis
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"fmt"
|
||
|
"os"
|
||
|
"strconv"
|
||
|
"time"
|
||
|
)
|
||
|
|
||
|
// Uptime returns the time elapsed since the start of the current process ID.
|
||
|
func Uptime() (time.Duration, error) {
|
||
|
raw, err := os.ReadFile("/proc/uptime")
|
||
|
if err != nil {
|
||
|
return 0, fmt.Errorf("failed to read uptime from OS: %w", err)
|
||
|
}
|
||
|
|
||
|
i := bytes.IndexRune(raw, ' ')
|
||
|
|
||
|
up, err := strconv.ParseFloat(string(raw[:i]), 64)
|
||
|
if err != nil {
|
||
|
return 0, fmt.Errorf("failed to parse uptime from OS: %w", err)
|
||
|
}
|
||
|
|
||
|
return time.Duration(int(up*1000) * int(time.Millisecond)), nil
|
||
|
}
|