diff options
author | Douglas Christman <DouglasChristman@gmail.com> | 2016-11-24 21:44:14 -0500 |
---|---|---|
committer | Douglas Christman <DouglasChristman@gmail.com> | 2016-11-25 11:21:21 -0500 |
commit | 7c2503218e0944947a55d02ef59a8676ffe1df9b (patch) | |
tree | 4ba38b6bf2af0a1de6ab56f412c020e52a50f01c | |
parent | c58b1b3abf829e00fa6aa01f0b57b8e6a3041511 (diff) |
calendarspec: refactor format_chain()
Factor out repeated references to usec and remove nested ifs.
-rw-r--r-- | src/basic/calendarspec.c | 29 |
1 files changed, 12 insertions, 17 deletions
diff --git a/src/basic/calendarspec.c b/src/basic/calendarspec.c index 329fd213c8..8b57de4744 100644 --- a/src/basic/calendarspec.c +++ b/src/basic/calendarspec.c @@ -256,6 +256,7 @@ static void format_weekdays(FILE *f, const CalendarSpec *c) { static void format_chain(FILE *f, int space, const CalendarComponent *c, bool usec) { const CalendarComponent *n, *p; + int d = usec ? (int) USEC_PER_SEC : 1; assert(f); @@ -265,21 +266,15 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us } assert(c->value >= 0); - if (!usec) - fprintf(f, "%0*i", space, c->value); - else if (c->value % USEC_PER_SEC == 0) - fprintf(f, "%0*i", space, (int) (c->value / USEC_PER_SEC)); - else - fprintf(f, "%0*i.%06i", space, (int) (c->value / USEC_PER_SEC), (int) (c->value % USEC_PER_SEC)); - - if (c->repeat > 0) { - if (!usec) - fprintf(f, "/%i", c->repeat); - else if (c->repeat % USEC_PER_SEC == 0) - fprintf(f, "/%i", (int) (c->repeat / USEC_PER_SEC)); - else - fprintf(f, "/%i.%06i", (int) (c->repeat / USEC_PER_SEC), (int) (c->repeat % USEC_PER_SEC)); - } + + fprintf(f, "%0*i", space, c->value / d); + if (c->value % d != 0) + fprintf(f, ".%06i", c->value % d); + + if (c->repeat != 0) + fprintf(f, "/%i", c->repeat / d); + if (c->repeat % d != 0) + fprintf(f, ".%06i", c->repeat % d); p = c; for (;;) { @@ -288,13 +283,13 @@ static void format_chain(FILE *f, int space, const CalendarComponent *c, bool us if (!n || n->repeat || p->repeat) break; - if (n->value - p->value != (usec ? (int) USEC_PER_SEC : 1)) + if (n->value - p->value != d) break; p = n; } - if (p->value - c->value >= 2 * (usec ? (int) USEC_PER_SEC : 1)) { + if (p->value - c->value >= 2 * d) { fputs("..", f); format_chain(f, space, p, usec); } else if (c->next) { |