Skip to content

Commit

Permalink
change: generate field now do */n, m/n repeats
Browse files Browse the repository at this point in the history
  • Loading branch information
exander77 committed Nov 8, 2024
1 parent dc22f00 commit b75e334
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
42 changes: 29 additions & 13 deletions ccronexpr.c
Original file line number Diff line number Diff line change
Expand Up @@ -585,21 +585,37 @@ static int do_nextprev(cron_expr* expr, struct tm* calendar, int dot, int offset

static int generate_field(char *dest, uint8_t *bits, int min, int max, int offset, int buffer_len) {
char buf[32];
int first = 1, from = -1, len = 0, i, bit;

for (i = min; i <= max; i++) {
bit = (i < max) ? cron_get_bit(bits, i + offset) : 0;
if (bit) { if (from == -1) from = i; }
else if (from != -1) {
if (!first) STRCATC(dest, ",", 1);
first = 0;
if (from == min && i - 1 == max - 1) len += sprintf(buf, "*");
else if (from == i - 1) len += sprintf(buf, "%d", from);
else len += sprintf(buf, "%d-%d", from, i - 1);
STRCATC(dest, buf, 0);
from = -1;
int len = 0, first = 1, i, fs = -1, ls = -1, d = -1, ns = 0, from, dt;
for (i = min; i < max; i++) {
if (cron_get_bit(bits, i + offset)) {
ns++;
if (fs == -1) fs = i;
else {
dt = i - ls;
if (d == -1) d = dt;
else if (d != dt) { d = -1; break; }
}
ls = i;
}
}
if (ns == max - min) len += sprintf(buf, "*");
else if (d < 2 || ns < 3) {
from = -1;
for (i = min; i <= max; i++) {
if ((i < max) ? cron_get_bit(bits, i + offset) : 0) { if (from == -1) from = i; }
else if (from != -1) {
if (!first) STRCATC(dest, ",", 1);
first = 0;
len += from == i-1 ? sprintf(buf, "%d", from) : sprintf(buf, "%d-%d", from, i-1);
from = -1;
STRCATC(dest, buf, 0);
}
}
return len;
} else if (fs == min && ls == max - 1 && min % d == 0 || fs == 0) len += sprintf(buf, "*/%d", d);
else if (((max - fs - 1) / d) + 1 != ns) len += sprintf(buf, "%d-%d/%d", fs, ls, d);
else len += sprintf(buf, "%d/%d", fs, d);
STRCATC(dest, buf, 0);
return len;
}

Expand Down
3 changes: 3 additions & 0 deletions ccronexpr_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,9 @@ void test_parse() {
check_expr_valid("0 0 12 ? 1,3,5,7,9,11 *"); /* Every odd month at noon. */
check_expr_valid("0 0 0 ? 2,4,6,8,10,12 *"); /* Every even month at midnight. */
check_expr_valid("0 0 12 * * ? 2027"); /* Every day at noon in the year 2027. */

check_expr_valid("0 6,9,12 0 * 4,6,8 *");
check_expr_valid("0 6,9,12,16,20,24 0 * 2,4,6,8,9,10 *");
}

void test_bits() {
Expand Down

0 comments on commit b75e334

Please sign in to comment.