diff options
-rw-r--r-- | src/core/wee-command.c | 3 | ||||
-rw-r--r-- | src/core/wee-util.c | 53 | ||||
-rw-r--r-- | src/core/wee-util.h | 8 | ||||
-rw-r--r-- | src/plugins/plugin-api-info.c | 10 |
4 files changed, 42 insertions, 32 deletions
diff --git a/src/core/wee-command.c b/src/core/wee-command.c index 51c83dfbe..daf765385 100644 --- a/src/core/wee-command.c +++ b/src/core/wee-command.c @@ -6369,7 +6369,8 @@ COMMAND_CALLBACK(uptime) (void) data; (void) argv_eol; - util_get_uptime (NULL, &days, &hours, &minutes, &seconds); + util_get_time_diff (weechat_first_start_time, time (NULL), + NULL, &days, &hours, &minutes, &seconds); if ((argc >= 2) && (string_strcasecmp (argv[1], "-o") == 0)) { diff --git a/src/core/wee-util.c b/src/core/wee-util.c index 381ab69aa..68270b554 100644 --- a/src/core/wee-util.c +++ b/src/core/wee-util.c @@ -339,6 +339,35 @@ util_get_time_string (const time_t *date) } /* + * Returns difference between two times. + * + * The following variables are set, if pointer is not NULL: + * - number of total seconds between the two times (basic subtraction) + * - number of days/hours/minutes/seconds between the two times + */ + +void +util_get_time_diff (time_t time1, time_t time2, + time_t *total_seconds, + int *days, int *hours, int *minutes, int *seconds) +{ + time_t diff; + + diff = time2 - time1; + + if (total_seconds) + *total_seconds = diff; + if (days) + *days = diff / (60 * 60 * 24); + if (hours) + *hours = (diff % (60 * 60 * 24)) / (60 * 60); + if (minutes) + *minutes = ((diff % (60 * 60 * 24)) % (60 * 60)) / 60; + if (seconds) + *seconds = ((diff % (60 * 60 * 24)) % (60 * 60)) % 60; +} + +/* * Parses a string with a delay and optional unit, returns the delay in * milliseconds. * @@ -982,27 +1011,3 @@ util_version_number (const char *version) return (version_int[0] << 24) | (version_int[1] << 16) | (version_int[2] << 8) | version_int[3]; } - -/* - * Returns uptime as number of days, hours, minutes, seconds. - */ - -void -util_get_uptime (time_t *total_seconds, int *days, - int *hours, int *minutes, int *seconds) -{ - time_t running_time; - - running_time = time (NULL) - weechat_first_start_time; - - if (total_seconds) - *total_seconds = running_time; - if (days) - *days = running_time / (60 * 60 * 24); - if (hours) - *hours = (running_time % (60 * 60 * 24)) / (60 * 60); - if (minutes) - *minutes = ((running_time % (60 * 60 * 24)) % (60 * 60)) / 60; - if (seconds) - *seconds = ((running_time % (60 * 60 * 24)) % (60 * 60)) % 60; -} diff --git a/src/core/wee-util.h b/src/core/wee-util.h index 82112f917..872594935 100644 --- a/src/core/wee-util.h +++ b/src/core/wee-util.h @@ -48,6 +48,10 @@ extern void util_timeval_add (struct timeval *tv, long long interval); /* time */ extern const char *util_get_time_string (const time_t *date); +extern void util_get_time_diff (time_t time1, time_t time2, + time_t *total_seconds, + int *days, int *hours, int *minutes, + int *seconds); /* delay */ @@ -75,8 +79,4 @@ extern char *util_file_get_content (const char *filename); /* version */ extern int util_version_number (const char *version); -/* uptime */ -extern void util_get_uptime (time_t *total_seconds, int *days, - int *hours, int *minutes, int *seconds); - #endif /* WEECHAT_UTIL_H */ diff --git a/src/plugins/plugin-api-info.c b/src/plugins/plugin-api-info.c index 16dafa281..7503bbee3 100644 --- a/src/plugins/plugin-api-info.c +++ b/src/plugins/plugin-api-info.c @@ -27,6 +27,7 @@ #include <stdlib.h> #include <unistd.h> #include <string.h> +#include <time.h> #include "../core/weechat.h" #include "../core/wee-config.h" @@ -694,7 +695,8 @@ plugin_api_info_uptime_cb (const void *pointer, void *data, if (!arguments || !arguments[0]) { /* return uptime with format: "days:hh:mm:ss" */ - util_get_uptime (NULL, &days, &hours, &minutes, &seconds); + util_get_time_diff (weechat_first_start_time, time (NULL), + NULL, &days, &hours, &minutes, &seconds); snprintf (value, sizeof (value), "%d:%02d:%02d:%02d", days, hours, minutes, seconds); return strdup (value); @@ -703,7 +705,8 @@ plugin_api_info_uptime_cb (const void *pointer, void *data, if (strcmp (arguments, "days") == 0) { /* return the number of days */ - util_get_uptime (NULL, &days, NULL, NULL, NULL); + util_get_time_diff (weechat_first_start_time, time (NULL), + NULL, &days, NULL, NULL, NULL); snprintf (value, sizeof (value), "%d", days); return strdup (value); } @@ -711,7 +714,8 @@ plugin_api_info_uptime_cb (const void *pointer, void *data, if (strcmp (arguments, "seconds") == 0) { /* return the number of seconds */ - util_get_uptime (&total_seconds, NULL, NULL, NULL, NULL); + util_get_time_diff (weechat_first_start_time, time (NULL), + &total_seconds, NULL, NULL, NULL, NULL); snprintf (value, sizeof (value), "%lld", (long long)total_seconds); return strdup (value); } |