diff options
-rw-r--r-- | src/ctime.c | 32 | ||||
-rw-r--r-- | 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; } @@ -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++; |