Skip to content

Commit

Permalink
Merge pull request tsoding#12 from kolumb/9
Browse files Browse the repository at this point in the history
(tsoding#9) Add support for human readable time intervals for countdown mode
  • Loading branch information
rexim authored Oct 19, 2020
2 parents fa2b80a + 4d1ef50 commit 4260a00
Showing 1 changed file with 31 additions and 1 deletion.
32 changes: 31 additions & 1 deletion main.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,36 @@ typedef enum {
MODE_CLOCK,
} Mode;

float parse_time(const char *time)
{
float result = 0.0f;

while (*time) {
char *endptr = NULL;
float x = strtof(time, &endptr);

if (time == endptr) {
fprintf(stderr, "`%s` is not a number\n", time);
exit(1);
}

switch (*endptr) {
case '\0':
case 's': result += x; break;
case 'm': result += x * 60.0f; break;
case 'h': result += x * 60.0f * 60.0f; break;
default:
fprintf(stderr, "`%c` is an unknown time unit\n", *endptr);
exit(1);
}

time = endptr;
if (*time) time += 1;
}

return result;
}

int main(int argc, char **argv)
{
Mode mode = MODE_ASCENDING;
Expand All @@ -139,7 +169,7 @@ int main(int argc, char **argv)
mode = MODE_CLOCK;
} else {
mode = MODE_COUNTDOWN;
displayed_time = strtof(argv[i], NULL);
displayed_time = parse_time(argv[i]);
}
}

Expand Down

0 comments on commit 4260a00

Please sign in to comment.