diff options
author | Aaron Ball <nullspoon@iohq.net> | 2016-01-28 23:12:02 -0700 |
---|---|---|
committer | Aaron Ball <nullspoon@iohq.net> | 2016-01-28 23:12:02 -0700 |
commit | 2b3d32c189ffa093eaa313f2f42c2735ef98e20a (patch) | |
tree | 3fc25ef397b37bf16c65377a00ed575db7b4c73b /src | |
parent | c16519973a0f26048716362a946ce91d5d450ed1 (diff) | |
download | ctimer-2b3d32c189ffa093eaa313f2f42c2735ef98e20a.tar.gz ctimer-2b3d32c189ffa093eaa313f2f42c2735ef98e20a.tar.xz |
Added support for days (365) and hours (24)
Also changed output format from
mm:ss
to
000 days 00h 00m 00s
Diffstat (limited to 'src')
-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++; |