libvieter/include/vieter_cron.h

72 lines
1.9 KiB
C
Raw Normal View History

2023-01-17 23:09:06 +01:00
#ifndef VIETER_CRON
#define VIETER_CRON
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
2023-01-18 13:53:50 +01:00
typedef enum vieter_cron_parse_error {
vieter_cron_parse_ok = 0,
vieter_cron_parse_invalid_expression = 1,
vieter_cron_parse_invalid_number = 2,
vieter_cron_parse_out_of_range = 3,
vieter_cron_parse_too_many_parts = 4,
vieter_cron_parse_not_enough_parts = 5
} vieter_cron_parse_error;
typedef struct vieter_cron_expression {
uint8_t *minutes;
uint8_t *hours;
uint8_t *days;
uint8_t *months;
uint8_t minute_count;
uint8_t hour_count;
uint8_t day_count;
uint8_t month_count;
2023-01-18 13:53:50 +01:00
} vieter_cron_expression;
2023-01-17 23:09:06 +01:00
2023-01-18 13:53:50 +01:00
typedef struct vieter_cron_simple_time {
int year;
int month;
int day;
int hour;
int minute;
2023-01-18 13:53:50 +01:00
} vieter_cron_simple_time;
2023-01-17 23:09:06 +01:00
/*
* Allocate and initialize a new empty cron expression.
*/
2023-01-27 20:48:51 +01:00
vieter_cron_expression *vieter_cron_expr_init();
2023-01-17 23:09:06 +01:00
/*
* Deallocate a cron expression.
*/
2023-01-18 14:12:02 +01:00
void vieter_cron_expr_free(vieter_cron_expression *ce);
2023-01-17 23:09:06 +01:00
/*
* Given a cron expression and a reference time, calculate the next time after
* the reference time that this expression matches.
*/
2023-01-18 14:12:02 +01:00
void vieter_cron_expr_next(vieter_cron_simple_time *out,
vieter_cron_expression *ce,
vieter_cron_simple_time *ref);
2023-01-17 23:09:06 +01:00
/*
* Convencience wrapper around vieter_cron_expr_next that uses the current time
* as the reference time.
*/
2023-01-18 14:12:02 +01:00
void vieter_cron_expr_next_from_now(vieter_cron_simple_time *out,
vieter_cron_expression *ce);
2023-01-17 23:09:06 +01:00
/*
* Try to parse a string into a cron expression. Note that the cron expression
* is updated in-place, meaning it can contain invalid information if the
* function returns an error. The cron expression should only be used if the
* function succeeded.
*/
vieter_cron_parse_error vieter_cron_expr_parse(vieter_cron_expression *out,
const char *expression);
2023-01-17 23:09:06 +01:00
#endif