From b9f1dc728376c898c7d7aaeed5ba4bb694ec57a0 Mon Sep 17 00:00:00 2001 From: Phil Sutter Date: Thu, 19 Nov 2009 23:39:08 +0100 Subject: [PATCH] cleanup times_in_seconds enhancement This also adds code to unset the variable before reloading the config. --- src/common.c | 11 +++++++++++ src/conky.c | 42 ++++++++++++++---------------------------- src/conky.h | 2 +- src/timeinfo.c | 17 +++++++++++++++++ src/timeinfo.h | 4 ++++ src/users.c | 12 ++++-------- 6 files changed, 51 insertions(+), 37 deletions(-) diff --git a/src/common.c b/src/common.c index f691a929..c4099d0c 100644 --- a/src/common.c +++ b/src/common.c @@ -34,6 +34,7 @@ #include "logging.h" #include "net_stat.h" #include "specials.h" +#include "timeinfo.h" #include #include #include @@ -218,6 +219,11 @@ void format_seconds(char *buf, unsigned int n, long seconds) long days; int hours, minutes; + if (times_in_seconds()) { + snprintf(buf, n, "%ld", seconds); + return; + } + days = seconds / 86400; seconds %= 86400; hours = seconds / 3600; @@ -237,6 +243,11 @@ void format_seconds_short(char *buf, unsigned int n, long seconds) long days; int hours, minutes; + if (times_in_seconds()) { + snprintf(buf, n, "%ld", seconds); + return; + } + days = seconds / 86400; seconds %= 86400; hours = seconds / 3600; diff --git a/src/conky.c b/src/conky.c index 7561601c..6b798ea5 100644 --- a/src/conky.c +++ b/src/conky.c @@ -167,7 +167,6 @@ int top_io; int top_running; #endif int output_methods; -char times_in_seconds = 0; static int extra_newline; enum x_initialiser_state x_initialised = NO; static volatile int g_signal_pending; @@ -727,6 +726,11 @@ static inline void format_media_player_time(char *buf, const int size, { int days, hours, minutes; + if (times_in_seconds()) { + snprintf(buf, size, "%d", seconds); + return; + } + days = seconds / (24 * 60 * 60); seconds %= (24 * 60 * 60); hours = seconds / (60 * 60); @@ -1508,11 +1512,7 @@ void generate_text_internal(char *p, int p_max_size, generate_text_internal(buf, max_user_text, *obj->sub, cur); obj->data.s = buf; - if(times_in_seconds) { - print_format_time(obj, p, p_max_size); - } else { - NORM_ERR("Enable \"times_in_seconds\" to use $format_time"); - } + print_format_time(obj, p, p_max_size); } /* mail stuff */ OBJ(mails) { @@ -1939,18 +1939,10 @@ void generate_text_internal(char *p, int p_max_size, } #endif /* X11 */ OBJ(uptime_short) { - if(times_in_seconds) { - snprintf(p, p_max_size, "%d", (int) cur->uptime); - } else { - format_seconds_short(p, p_max_size, (int) cur->uptime); - } + format_seconds_short(p, p_max_size, (int) cur->uptime); } OBJ(uptime) { - if(times_in_seconds) { - snprintf(p, p_max_size, "%d", (int) cur->uptime); - } else { - format_seconds(p, p_max_size, (int) cur->uptime); - } + format_seconds(p, p_max_size, (int) cur->uptime); } #ifdef __linux__ OBJ(user_names) { @@ -1963,7 +1955,7 @@ void generate_text_internal(char *p, int p_max_size, snprintf(p, p_max_size, "%s", cur->users.times); } OBJ(user_time) { - update_user_time(obj->data.s, times_in_seconds); + update_user_time(obj->data.s); snprintf(p, p_max_size, "%s", cur->users.ctime); } OBJ(user_number) { @@ -2027,18 +2019,10 @@ void generate_text_internal(char *p, int p_max_size, OBJ(mpd_status) mpd_printf("%s", status); OBJ(mpd_elapsed) { - if(times_in_seconds) { - snprintf(p, p_max_size, "%d", mpd_get_info()->elapsed); - } else { - format_media_player_time(p, p_max_size, mpd_get_info()->elapsed); - } + format_media_player_time(p, p_max_size, mpd_get_info()->elapsed); } OBJ(mpd_length) { - if(times_in_seconds) { - snprintf(p, p_max_size, "%d", mpd_get_info()->length); - } else { - format_media_player_time(p, p_max_size, mpd_get_info()->length); - } + format_media_player_time(p, p_max_size, mpd_get_info()->length); } OBJ(mpd_percent) { percent_print(p, p_max_size, (int)(mpd_get_info()->progress * 100)); @@ -4459,6 +4443,8 @@ static void set_default_configurations(void) stuff_in_uppercase = 0; info.users.number = 1; + set_times_in_seconds(0); + #ifdef TCP_PORT_MONITOR /* set default connection limit */ tcp_portmon_set_max_connections(0); @@ -4923,7 +4909,7 @@ char load_config_file(const char *f) } #endif /* X11 */ CONF("times_in_seconds") { - times_in_seconds = string_to_bool(value); + set_times_in_seconds(string_to_bool(value)); } CONF("out_to_console") { if(string_to_bool(value)) { diff --git a/src/conky.h b/src/conky.h index 0f47b1e0..78782884 100644 --- a/src/conky.h +++ b/src/conky.h @@ -309,7 +309,7 @@ extern struct information info; /* defined in users.c */ void update_users(void); -void update_user_time(char *tty, char times_in_seconds); +void update_user_time(char *tty); /* defined in conky.c */ extern double current_update_time, last_update_time, update_interval; diff --git a/src/timeinfo.c b/src/timeinfo.c index 1cb76281..9e5288b2 100644 --- a/src/timeinfo.c +++ b/src/timeinfo.c @@ -36,11 +36,23 @@ #include #include +char print_times_in_seconds = 0; + struct tztime_s { char *tz; /* timezone variable */ char *fmt; /* time display formatting */ }; +void set_times_in_seconds(char val) +{ + print_times_in_seconds = val; +} + +char times_in_seconds(void) +{ + return print_times_in_seconds; +} + void scan_time(struct text_object *obj, const char *arg) { obj->data.opaque = strndup(arg ? arg : "%F %T", text_buffer_size); @@ -150,6 +162,11 @@ void print_format_time(struct text_object *obj, char *p, unsigned int p_max_size int minutes, hours, days, weeks; char show_minutes = 0, show_hours = 0, show_days = 0, show_weeks = 0, hidestring; + if (!times_in_seconds()) { + NORM_ERR("Enable \"times_in_seconds\" to use $format_time"); + return; + } + errno = 0; seconds = strtod(obj->data.s, ¤tchar); if(errno == 0 && obj->data.s != currentchar) { diff --git a/src/timeinfo.h b/src/timeinfo.h index 8aa505f0..7988cda9 100644 --- a/src/timeinfo.h +++ b/src/timeinfo.h @@ -30,6 +30,10 @@ #ifndef _TIMEINFO_H #define _TIMEINFO_H +/* for the times_in_seconds configuration variable and it's users */ +void set_times_in_seconds(char); +char times_in_seconds(void); + /* since time and utime are quite equal, certain functions * are shared in between both text object types. */ diff --git a/src/users.c b/src/users.c index 1b97725e..73e8d08a 100644 --- a/src/users.c +++ b/src/users.c @@ -93,7 +93,7 @@ static void user_time(char *ptr) } } } -static void tty_user_time(char *ptr, char *tty, char times_in_seconds) +static void tty_user_time(char *ptr, char *tty) { time_t real, diff, log_in; char buf[BUFLEN] = ""; @@ -111,11 +111,7 @@ static void tty_user_time(char *ptr, char *tty, char times_in_seconds) time(&real); diff = difftime(real, log_in); - if(times_in_seconds) { - snprintf(buf, BUFLEN, "%d", (int) diff); - } else { - format_seconds(buf, BUFLEN, diff); - } + format_seconds(buf, BUFLEN, diff); strncpy(ptr, buf, BUFLEN-1); } @@ -133,7 +129,7 @@ static void users_alloc(struct information *ptr) } } -void update_user_time(char *tty, char times_in_seconds) +void update_user_time(char *tty) { struct information *current_info = &info; char temp[BUFLEN] = ""; @@ -142,7 +138,7 @@ void update_user_time(char *tty, char times_in_seconds) current_info->users.ctime = malloc(text_buffer_size); } - tty_user_time(temp, tty, times_in_seconds); + tty_user_time(temp, tty); if (temp != NULL) { if (current_info->users.ctime) {