suckless/slstatus

components/uptime.c in master
Repositories | Summary | Log | Files | README | LICENSE

uptime.c (702B) download


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