uptime.go 575 B

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