uptime.c 650 B

12345678910111213141516171819202122232425262728293031323334
  1. /* See LICENSE file for copyright and license details. */
  2. #include <stdint.h>
  3. #include <stdio.h>
  4. #include <time.h>
  5. #include "../util.h"
  6. #if defined(CLOCK_BOOTTIME)
  7. #define UPTIME_FLAG CLOCK_BOOTTIME
  8. #elif defined(CLOCK_UPTIME)
  9. #define UPTIME_FLAG CLOCK_UPTIME
  10. #else
  11. #define UPTIME_FLAG CLOCK_MONOTONIC
  12. #endif
  13. const char *
  14. uptime(void)
  15. {
  16. char warn_buf[256];
  17. uintmax_t h, m;
  18. struct timespec uptime;
  19. if (clock_gettime(UPTIME_FLAG, &uptime) < 0) {
  20. snprintf(warn_buf, 256, "clock_gettime %d", UPTIME_FLAG);
  21. warn(warn_buf);
  22. return NULL;
  23. }
  24. h = uptime.tv_sec / 3600;
  25. m = uptime.tv_sec % 3600 / 60;
  26. return bprintf("%juh %jum", h, m);
  27. }