summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAaron Ball <nullspoon@iohq.net>2016-01-28 23:12:02 -0700
committerAaron Ball <nullspoon@iohq.net>2016-01-28 23:12:02 -0700
commit2b3d32c189ffa093eaa313f2f42c2735ef98e20a (patch)
tree3fc25ef397b37bf16c65377a00ed575db7b4c73b
parentc16519973a0f26048716362a946ce91d5d450ed1 (diff)
downloadctimer-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
-rw-r--r--src/ctime.c32
-rw-r--r--src/main.c2
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++;

Generated by cgit