From bb80900ca6bc242536bbd386e6bd4644465cef5a Mon Sep 17 00:00:00 2001 From: Kevin Lyles Date: Thu, 19 Jun 2008 07:04:11 +0000 Subject: [PATCH] Refactored format_seconds and format_seconds_short git-svn-id: https://conky.svn.sourceforge.net/svnroot/conky/trunk/conky1@1165 7f574dfc-610e-0410-a909-a81674777703 --- src/common.c | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/common.c b/src/common.c index d28d58c..82d16bf 100644 --- a/src/common.c +++ b/src/common.c @@ -213,26 +213,43 @@ void update_dns_data(void) fclose(fp); } -void format_seconds(char *buf, unsigned int n, long t) +void format_seconds(char *buf, unsigned int n, long seconds) { - if (t >= 24 * 60 * 60) { /* hours necessary when there are days? */ - snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24, (t / 60 / 60) % 24, - (t / 60) % 60); - } else if (t >= 60 * 60) { - snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60); + long days; + int hours, minutes; + + days = seconds / 86400; + seconds %= 86400; + hours = seconds / 3600; + seconds %= 3600; + minutes = seconds / 60; + seconds %= 60; + + if (days > 0) { + snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes); } else { - snprintf(buf, n, "%ldm %lds", t / 60, t % 60); + snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds); } } -void format_seconds_short(char *buf, unsigned int n, long t) +void format_seconds_short(char *buf, unsigned int n, long seconds) { - if (t >= 24 * 60 * 60) { - snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24, (t / 60 / 60) % 24); - } else if (t >= 60 * 60) { - snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60); + long days; + int hours, minutes; + + days = seconds / 86400; + seconds %= 86400; + hours = seconds / 3600; + seconds %= 3600; + minutes = seconds / 60; + seconds %= 60; + + if (days > 0) { + snprintf(buf, n, "%ldd %dh", days, hours); + } else if (hours > 0) { + snprintf(buf, n, "%dh %dm", hours, minutes); } else { - snprintf(buf, n, "%ldm", t / 60); + snprintf(buf, n, "%dm %lds", minutes, seconds); } } -- 1.7.9.5