fix(cron): fix some bugs

Jef Roosens 2023-01-14 20:46:03 +01:00
parent d6b7ce98c1
commit 2e6ac5cda6
3 changed files with 17 additions and 17 deletions

View File

@ -102,7 +102,8 @@ void ce_next_from_now(cron_simple_time *out, cron_expression *ce) {
gmtime_r(&t, &gm); gmtime_r(&t, &gm);
cron_simple_time ref = { cron_simple_time ref = {
.year = gm.tm_year, // tm_year contains years since 1900
.year = 1900 + gm.tm_year,
// tm_mon goes from 0 to 11 // tm_mon goes from 0 to 11
.month = gm.tm_mon + 1, .month = gm.tm_mon + 1,
.day = gm.tm_mday, .day = gm.tm_mday,

View File

@ -40,18 +40,6 @@ const uint8_t max_parts = 4;
* - a-b/c * - a-b/c
*/ */
cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) { cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max) {
// The * expression means "every possible value"
if (s[0] == '*') {
// A '*' is only valid on its own
if (s[1] != '\0') {
return cron_parse_invalid_expression;
}
*out = ~0;
return cron_parse_ok;
}
size_t slash_index = 0; size_t slash_index = 0;
size_t dash_index = 0; size_t dash_index = 0;
size_t i = 0; size_t i = 0;
@ -74,15 +62,18 @@ cron_parse_error ce_parse_range(uint64_t *out, char *s, uint8_t min, uint8_t max
} }
// Parse the three possible numbers in the pattern // Parse the three possible numbers in the pattern
uint8_t start = 0; uint8_t start = min;
uint8_t end = max; uint8_t end = max;
uint8_t interval = 1; uint8_t interval = 1;
// * simply sets start as min and end as max
if (!(s[0] == '*' && strlen(s) == 1)) {
SAFE_ATOI(start, s, min, max); SAFE_ATOI(start, s, min, max);
if (dash_index > 0) { if (dash_index > 0) {
SAFE_ATOI(end, &s[dash_index + 1], min, max); SAFE_ATOI(end, &s[dash_index + 1], min, max);
} }
}
if (slash_index > 0) { if (slash_index > 0) {
SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min); SAFE_ATOI(interval, &s[slash_index + 1], 1, max - min);

View File

@ -34,3 +34,11 @@ fn test_next_simple() ! {
//// Overlap to next year //// Overlap to next year
util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')! util_test_time('0 3', '2002-12-31 04:00:00', '2003-01-01 03:00:00')!
} }
fn test_leading_star() {
mut x := false
parse_expression('*5 8') or { x = true }
assert x
}