LwDTC

group LWDTC

Lightweight Date, Time & Cron utility.

Defines

LWDTC_SEC_MIN

Minimum value for seconds field

LWDTC_SEC_MAX

Maximum value for seconds field

LWDTC_MIN_MIN

Minimum value for minutes field

LWDTC_MIN_MAX

Maximum value for minutes field

LWDTC_HOUR_MIN

Minimum value for hours field

LWDTC_HOUR_MAX

Maximum value for hours field

LWDTC_MDAY_MIN

Minimum value for day in month field

LWDTC_MDAY_MAX

Maximum value for day in month field

LWDTC_MON_MIN

Minimum value for month field

LWDTC_MON_MAX

Maximum value for month field

LWDTC_WDAY_MIN

Minimum value for week day field (min = Sunday, max = Saturday)

LWDTC_WDAY_MAX

Maximum value for week day field (min = Sunday, max = Saturday)

LWDTC_YEAR_MIN

Minimum value for year field

LWDTC_YEAR_MAX

Maximum value for year field

LWDTC_ARRAYSIZE(x)

Calculate size of statically allocated array.

Parameters
  • x[in] Array

Returns

Number of elements

Enums

enum lwdtcr_t

Result enumeration.

Values:

enumerator lwdtcOK = 0x00

Everything is OK

enumerator lwdtcERR

Generic error

enumerator lwdtcERRPAR

Invalid parameter passed to a function

enumerator lwdtcERRTOKEN

Token value is not valid

Functions

lwdtcr_t lwdtc_cron_parse_with_len(lwdtc_cron_ctx_t *ctx, const char *cron_str, size_t cron_str_len)

Parse string with linux crontab-like syntax, optionally enriched according to configured settings.

Parameters
  • ctx[in] Cron context variable used for storing parsed result

  • cron_str[in] Input cron string to parse data, using valid cron format recognized by the lib

  • cron_str_len[in] Length of input cron string, not counting potential NULL termination character

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_cron_parse(lwdtc_cron_ctx_t *ctx, const char *cron_str)

Parse string with linux crontab-like syntax, optionally enriched according to configured settings.

Parameters
  • ctx[in] Cron context variable used for storing parsed result

  • cron_str[in] NULL terminated cron string, using valid cron format recognized by the lib

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_cron_parse_multi(lwdtc_cron_ctx_t *cron_ctx, const char **cron_strs, size_t ctx_len, size_t *fail_index)

Parse multiple CRON strins at the same time. It returns immediately on first failed CRON.

Parameters
  • cron_ctx[in] Cron context variable used for storing parsed result

  • cron_strs[in] Pointer to array of string pointers with cron strings

  • ctx_len[in] Number of elements to process

  • fail_index[out] Optional pointer to output variable to store array index of failed CRON. Used only if function doesn’t return lwdtcOK, otherwise pointer doesn’t get modified

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_cron_is_valid_for_time(const struct tm *tm_time, const lwdtc_cron_ctx_t *cron_ctx)

Check if cron is active at specific moment of time, provided as parameter.

Parameters
  • tm_time[in] Current time to check if cron works for it. Function assumes values in the structure are within valid boundaries and does not perform additional check

  • cron_ctx[in] Cron context object with valid structure

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_cron_is_valid_for_time_multi_or(const struct tm *tm_time, const lwdtc_cron_ctx_t *cron_ctx, size_t ctx_len)

Check if current time fits to at least one of provided context arrays (OR operation)

Parameters
  • tm_time[in] Current time to check if cron works for it. Function assumes values in the structure are within valid boundaries and does not perform additional check

  • cron_ctx[in] Pointer to array of cron ctx objects

  • ctx_len[in] Number of context array length

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_cron_is_valid_for_time_multi_and(const struct tm *tm_time, const lwdtc_cron_ctx_t *cron_ctx, size_t ctx_len)

Check if current time fits to all provided cron context arrays (AND operation)

Parameters
  • tm_time[in] Current time to check if cron works for it. Function assumes values in the structure are within valid boundaries and does not perform additional check

  • cron_ctx[in] Pointer to array of cron ctx objects

  • ctx_len[in] Number of context array length

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_cron_next(const lwdtc_cron_ctx_t *cron_ctx, time_t curr_time, time_t *new_time)

Get next time of fire for specific cron object.

This is a dirty implementation and could be improved in the future. For now, we start with one second after current time, and do the roll over all values until we have a match.

Parameters
  • cron_ctx – CRON context object

  • curr_time – Current time, used as reference to get new time

  • new_time[out] Pointer to new time value

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

struct lwdtc_cron_ctx_t
#include <lwdtc.h>

Cron context variable with parsed information.

It is a bit-field of ones and zeros, indicating a match (or not) for date-time comparison to determine if needs to run (or not) a task

Public Members

uint32_t flags

List of all sort of flags for internal use

uint8_t sec[8]

Seconds field. Must support bits from 0 to 59

uint8_t min[8]

Minutes field. Must support bits from 0 to 59

uint8_t hour[3]

Hours field. Must support bits from 0 to 23

uint8_t mday[4]

Day number in a month. Must support bits from 0 to 30

uint8_t mon[2]

Month field. Must support bits from 0 to 11

uint8_t wday[1]

Week day. Must support bits from 0 (Sunday) to 6 (Saturday)

uint8_t year[13]

Year from 0 - 100, indicating 2000 - 2100. Must support bits 0 to 100