From 2b3d32c189ffa093eaa313f2f42c2735ef98e20a Mon Sep 17 00:00:00 2001 From: Aaron Ball Date: Thu, 28 Jan 2016 23:12:02 -0700 Subject: Added support for days (365) and hours (24) Also changed output format from mm:ss to 000 days 00h 00m 00s --- src/ctime.c | 32 +++++++++++++++++++++++++++++--- src/main.c | 2 +- 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/src/ctime.c b/src/ctime.c index 60cafb7..7ea5f2e 100644 --- a/src/ctime.c +++ b/src/ctime.c @@ -9,13 +9,39 @@ void ctime_t_new(ctime_t* t) { t->year = 0; } -int ctime_add_sec(ctime_t* t, int duration) { - t->sec += duration; +int ctime_add_sec(ctime_t* t) { + t->sec++; + if(t->sec > 59) { // Increment minute t->min++; // TODO: This doesn't work when duration > 1. Need better logic here. t->sec = 0; } - return 0; + + if(t->min == 60) { + t->hour++; + t->min = 0; + } else { + // Return early since we won't increment anything before 60 + return 0; + } + + if(t->hour == 24) { + t->day++; + t->hour = 0; + } else { + // Return early since we won't increment anything before 24 + return 0; + } + + if(t->day == 366) { + t->year++; + t->day = 0; + } else { + // Return early since we won't increment anything before 24 + return 0; + } + + return 1; } diff --git a/src/main.c b/src/main.c index cc078f4..e64afe4 100644 --- a/src/main.c +++ b/src/main.c @@ -18,7 +18,7 @@ int main(int argc, char* argv[]) { ctime_t_new(&t); while(0 == 0) { - printf("\r%02d:%02d", t.min, t.sec); + printf("\r%03d days %02dh %02dm %02ds", t.day, t.hour, t.min, t.sec); ctime_add_sec(&t, 1); fflush(stdout); i++; -- cgit v1.2.3