#include <sys/time.h>
#include "timestat.h"
#include "libtop.h"
#include "generic.h"
#include "preferences.h"
static bool time_insert_cell(struct statistic *s, const void *sample) {
const libtop_psamp_t *psamp = sample;
struct timeval tv;
unsigned int usec, sec, min, hour, day;
char buf[GENERIC_INT_SIZE];
if(STATMODE_ACCUM == top_prefs_get_mode()) {
timersub(&psamp->total_time, &psamp->b_total_time, &tv);
} else {
tv = psamp->total_time;
}
usec = tv.tv_usec;
sec = tv.tv_sec;
min = sec / 60;
hour = min / 60;
day = hour / 24;
if(min < 100) {
snprintf(buf, sizeof(buf), "%02u:%02u.%02u", min, sec % 60, usec / 10000);
} else if(hour < 100) {
snprintf(buf, sizeof(buf), "%02u:%02u:%02u", hour, min % 60, sec % 60);
} else {
snprintf(buf, sizeof(buf), "%u hrs", hour);
}
#if 0
if(sec < 60) {
snprintf(buf, sizeof(buf), "%u.%02us", sec, usec / 10000);
} else if(min < 60) {
snprintf(buf, sizeof(buf), "%um%02us", min, sec - min * 60);
} else if(hour < 24) {
snprintf(buf, sizeof(buf), "%uh%02um", hour, min - hour * 60);
} else if(day < 100) {
snprintf(buf, sizeof(buf), "%ud%02uh", day, hour - day * 24);
} else {
snprintf(buf, sizeof(buf), "%ud", day);
}
#endif
return generic_insert_cell(s, buf);
}
static struct statistic_callbacks callbacks = {
.draw = generic_draw,
.resize_cells = generic_resize_cells,
.move_cells = generic_move_cells,
.get_request_size = generic_get_request_size,
.get_minimum_size = generic_get_minimum_size,
.insert_cell = time_insert_cell,
.reset_insertion = generic_reset_insertion
};
struct statistic *top_time_create(WINDOW *parent, const char *name) {
return create_statistic(STATISTIC_TIME, parent, NULL, &callbacks, name);
}