File: uptime_linux.go

package info (click to toggle)
golang-github-containers-buildah 1.39.3%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,724 kB
  • sloc: sh: 2,398; makefile: 236; perl: 187; asm: 16; awk: 12; ansic: 1
file content (28 lines) | stat: -rw-r--r-- 477 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package util

import (
	"bytes"
	"errors"
	"os"
	"time"
)

func ReadUptime() (time.Duration, error) {
	buf, err := os.ReadFile("/proc/uptime")
	if err != nil {
		return 0, err
	}
	f := bytes.Fields(buf)
	if len(f) < 1 {
		return 0, errors.New("invalid uptime")
	}

	// Convert uptime in seconds to a human-readable format
	up := string(f[0])
	upSeconds := up + "s"
	upDuration, err := time.ParseDuration(upSeconds)
	if err != nil {
		return 0, err
	}
	return upDuration, nil
}