libvieter/include/vieter_cron.h

72 lines
1.9 KiB
C

#ifndef VIETER_CRON
#define VIETER_CRON
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
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;
} vieter_cron_expression;
typedef struct vieter_cron_simple_time {
int year;
int month;
int day;
int hour;
int minute;
} vieter_cron_simple_time;
/*
* Allocate and initialize a new empty cron expression.
*/
vieter_cron_expression *vieter_cron_expr_init();
/*
* Deallocate a cron expression.
*/
void vieter_cron_expr_free(vieter_cron_expression *ce);
/*
* Given a cron expression and a reference time, calculate the next time after
* the reference time that this expression matches.
*/
void vieter_cron_expr_next(vieter_cron_simple_time *out,
vieter_cron_expression *ce,
vieter_cron_simple_time *ref);
/*
* Convencience wrapper around vieter_cron_expr_next that uses the current time
* as the reference time.
*/
void vieter_cron_expr_next_from_now(vieter_cron_simple_time *out,
vieter_cron_expression *ce);
/*
* 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);
#endif