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_tm_to_dt(const struct tm *tm_time, lwdtc_dt_t *dt)

Convert struct tm to lwdtc_dt_t datetime structure.

Parameters
  • tm_time[in] Datetime structure from time.h header to be converted

  • dt[in] Datetime structure to write converted data to

Returns

lwdtcOK on success, member of lwdtcr_t otherwise

lwdtcr_t lwdtc_dt_to_tm(const lwdtc_dt_t *dt, struct tm *tm_time)

Convert lwdtc_dt_t datetime structure to struct tm data type from time.h library.

Parameters
  • dt[in] Datetime structure to write converted data

  • tm_time[in] Datetime structure from time.h header to be converted

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

struct lwdtc_dt_t
#include <lwdtc.h>

Date and time structure of LwDTC library.

Not used for the moment, but defined as placeholder for the future.

Public Members

uint8_t sec

Seconds in a minute. Value between LWDTC_SEC_MIN and LWDTC_SEC_MAX

uint8_t min

Minutes in a hour. Value between LWDTC_MIN_MIN and LWDTC_MIN_MAX

uint8_t hour

Hours in a day. Value between LWDTC_HOUR_MIN and LWDTC_HOUR_MAX

uint8_t mday

Day in a month. Value between LWDTC_MDAY_MIN and LWDTC_MDAY_MAX

uint8_t mon

Month in a year. Value between LWDTC_MON_MIN and LWDTC_MON_MAX

uint8_t wday

Week day, between Sunday and Saturday. Value between LWDTC_WDAY_MIN and LWDTC_WDAY_MAX

uint8_t year

Year, starting with 2000. Value between LWDTC_YEAR_MIN and LWDTC_YEAR_MAX

Note

This is different versus struct tm from time.h library, where years start from 1900, hence year 2022 is written as 122 in struct tm, while same year is set as 22 this field