Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions host/host.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"fmt"
"os"
"runtime"
"time"

"github.com/shirou/gopsutil/v4/internal/common"
)
Expand Down Expand Up @@ -146,11 +145,3 @@ func Virtualization() (string, string, error) {
func KernelVersion() (string, error) {
return KernelVersionWithContext(context.Background())
}

func timeSince(ts uint64) uint64 {
return uint64(time.Now().Unix()) - ts
}

func timeSinceMillis(ts uint64) uint64 {
return uint64(time.Now().UnixMilli()) - ts
}
4 changes: 3 additions & 1 deletion host/host_bsd.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import (
"sync/atomic"

"golang.org/x/sys/unix"

"github.com/shirou/gopsutil/v4/internal/common"
)

// cachedBootTime must be accessed via atomic.Load/StoreUint64
Expand Down Expand Up @@ -37,5 +39,5 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
if err != nil {
return 0, err
}
return timeSince(boot), nil
return common.TimeSince(boot), nil
}
2 changes: 1 addition & 1 deletion host/host_solaris.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func UptimeWithContext(ctx context.Context) (uint64, error) {
if err != nil {
return 0, err
}
return timeSince(bootTime), nil
return common.TimeSince(bootTime), nil
}

func UsersWithContext(_ context.Context) ([]UserStat, error) {
Expand Down
2 changes: 1 addition & 1 deletion host/host_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ func BootTimeWithContext(_ context.Context) (uint64, error) {
if err != nil {
return 0, err
}
t := uint64((time.Duration(timeSinceMillis(up)) * time.Millisecond).Seconds())
t := uint64((time.Duration(common.TimeSinceMillis(up)) * time.Millisecond).Seconds())
if enableBootTimeCache {
atomic.StoreUint64(&cachedBootTime, t)
}
Expand Down
6 changes: 5 additions & 1 deletion internal/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,10 @@ func Round(val float64, n int) float64 {
return math.Round(val*pow10) / pow10
}

func timeSince(ts uint64) uint64 {
func TimeSince(ts uint64) uint64 {
return uint64(time.Now().Unix()) - ts
}

func TimeSinceMillis(ts uint64) uint64 {
return uint64(time.Now().UnixMilli()) - ts
}
2 changes: 1 addition & 1 deletion internal/common/common_aix.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func BootTimeWithContext(ctx context.Context, invoke Invoker) (btime uint64, err
return 0, errors.New("uptime was not set, so cannot calculate boot time from it")
}

return timeSince(ut), nil
return TimeSince(ut), nil
}

// Uses ps to get the elapsed time for PID 1 in DAYS-HOURS:MINUTES:SECONDS format.
Expand Down
Loading