aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTomasz Kramkowski <tk@the-tk.com>2015-06-11 22:04:50 +0100
committerTomasz Kramkowski <tk@the-tk.com>2015-06-11 22:04:50 +0100
commit2ca65abe1cf7825184116982fedb6aa991540d24 (patch)
tree890db5e9cf751aaeabc6b939a25c770a88c6dc9e
parent7bf25fb8f0e4643a67894417a95d39e5901b1824 (diff)
downloadc-stuff-2ca65abe1cf7825184116982fedb6aa991540d24.tar.gz
c-stuff-2ca65abe1cf7825184116982fedb6aa991540d24.tar.xz
c-stuff-2ca65abe1cf7825184116982fedb6aa991540d24.zip
timer.c: Made years, months and days verbal.
Years, months and days now display in a verbal fashion and only appear if the values are above 0. Additionally, plural and singular values are also displayed correctly.
-rw-r--r--timer.c28
1 files changed, 25 insertions, 3 deletions
diff --git a/timer.c b/timer.c
index d32c9b1..875cb5c 100644
--- a/timer.c
+++ b/timer.c
@@ -88,17 +88,39 @@ void clear_line(void)
void print_time(unsigned long total_sec, bool show_colon)
{
unsigned long years, months, days, hours, minutes, seconds;
- char colon = show_colon ? ':' : ' ';
+ char colon = show_colon ? ':' : ' ', syears[128], smonths[16],
+ sdays[16];
years = total_sec / SEC_YEAR;
total_sec %= SEC_YEAR;
+ if (years == 1)
+ snprintf(syears, sizeof syears, "%lu year, ", years);
+ else if (years > 1)
+ snprintf(syears, sizeof syears, "%lu years, ", years);
+ else
+ syears[0] = '\0';
+
months = total_sec / SEC_MONTH;
total_sec %= SEC_MONTH;
+ if (months == 1)
+ snprintf(smonths, sizeof smonths, "%lu month, ", months);
+ else if (months > 1)
+ snprintf(smonths, sizeof smonths, "%lu months, ", months);
+ else
+ smonths[0] = '\0';
+
days = total_sec / SEC_DAY;
total_sec %= SEC_DAY;
+ if (days == 1)
+ snprintf(sdays, sizeof sdays, "%lu day, ", days);
+ else if (days > 1)
+ snprintf(sdays, sizeof sdays, "%lu days, ", days);
+ else
+ sdays[0] = '\0';
+
hours = total_sec / SEC_HOUR;
total_sec %= SEC_HOUR;
@@ -111,8 +133,8 @@ void print_time(unsigned long total_sec, bool show_colon)
if (total_sec != 0)
error(1, 0, "An error occured during time formatting");
- printf(" %luY %luM %luD %.2lu%c%.2lu%c%.2lu\r", years, months,
- days, hours, colon, minutes, colon, seconds);
+ printf(" %s%s%s%.2lu%c%.2lu%c%.2lu\r", syears, smonths, sdays, hours,
+ colon, minutes, colon, seconds);
if (fflush(stdout) != 0)
error(1, errno, "Failed to flush stdout");