uptime.go 590 B

123456789101112131415161718192021222324252627282930313233343536
  1. //go:build aix
  2. // +build aix
  3. package perfstat
  4. /*
  5. #include "c_helpers.h"
  6. */
  7. import "C"
  8. import (
  9. "fmt"
  10. "time"
  11. )
  12. func timeSince(ts uint64) uint64 {
  13. return uint64(time.Now().Unix()) - ts
  14. }
  15. // BootTime() returns the time of the last boot in UNIX seconds
  16. func BootTime() (uint64, error) {
  17. sec := C.boottime()
  18. if sec == -1 {
  19. return 0, fmt.Errorf("Can't determine boot time")
  20. }
  21. return uint64(sec), nil
  22. }
  23. // UptimeSeconds() calculates uptime in seconds
  24. func UptimeSeconds() (uint64, error) {
  25. boot, err := BootTime()
  26. if err != nil {
  27. return 0, err
  28. }
  29. return timeSince(boot), nil
  30. }