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