diff options
author | Sébastien Helleu <flashcode@flashtux.org> | 2020-02-02 13:57:39 +0100 |
---|---|---|
committer | Sébastien Helleu <flashcode@flashtux.org> | 2020-02-02 13:57:39 +0100 |
commit | 27266ccd02b8495420e7c2387e2a6a6d48cfe010 (patch) | |
tree | b1a6a8c6a6ff810f11bde5208f58487aea9855d4 /src/core | |
parent | 4e2718d234eb14f8b262b006912ae0dc5f067f09 (diff) | |
download | weechat-27266ccd02b8495420e7c2387e2a6a6d48cfe010.zip |
core: rename function util_get_uptime to util_get_time_diff
The two times must be sent as parameters, which makes the function not specific
to uptime. It is now easier to test the function in unit tests.
Diffstat (limited to 'src/core')
-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 |
3 files changed, 35 insertions, 29 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 */ |