From f8d95fd733ebeacab9b2f31d21ecd56db3153004 Mon Sep 17 00:00:00 2001 From: randomuser Date: Fri, 11 Jun 2021 00:58:16 -0500 Subject: [PATCH] dedicated functions for calculating hours, minutes, seconds --- progs/timer.c | 5 +++-- progs/timerlib.c | 16 ++++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/progs/timer.c b/progs/timer.c index 78bf949..3ad9967 100644 --- a/progs/timer.c +++ b/progs/timer.c @@ -31,8 +31,9 @@ int timerissettings(struct timer *t) { char *timerdisp(struct timer *t) { char *str = malloc(20); if(s.f) snprintf(str, 20, "%02i:%02i:%02i", - ((t->s / 60) / 60), (t->s / 60) % 60, t->s % 60); - else snprintf(str, 20, "%02i:%02i", t->s / 60, t->s % 60); + hours(t->s), minutes(t->s), seconds(t->s)); + /* TODO: give minute(...) and minutes(...) better names */ + else snprintf(str, 20, "%02i:%02i", minute(t->s), seconds(t->s)); return str; } diff --git a/progs/timerlib.c b/progs/timerlib.c index 6d02d66..3e8e39c 100644 --- a/progs/timerlib.c +++ b/progs/timerlib.c @@ -39,6 +39,22 @@ int timerzero(struct timer *t) { return 0; } +int seconds(int t) { + return t % 60; +} + +int minutes(int t) { + return (t / 60) % 60; +} + +int minute(int t) { + return t / 60; +} + +int hours(int t) { + return (t / 60) / 60; +} + struct timer *timerinit(void) { struct timer *t = malloc(sizeof *t); t->s = 0;