Crinit -- Configurable Rootfs Init
Loading...
Searching...
No Matches
timer.c File Reference

(/usr/bin/git 2026-02-27, commit: 96e4505b)

Implementation of functions related to a single timer. More...

#include "timer.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "logio.h"
Include dependency graph for timer.c:

Functions

static bool crinitIsLeapYear (uint16_t year)
 
static const char * crinitMonthToStr (uint8_t m)
 
static int crinitMonthLength (uint8_t month, uint16_t year)
 
static bool crinitNext (int last, uint8_t interval[2], int start, int limit, int *res, long long int *offs)
 
static bool crinitNextYear (int last, uint16_t interval[2], int *res, long long int *offs)
 
static int crinitDaysToMonth (uint8_t m, uint16_t year)
 
void crinitTimerSetDefault (crinitTimerDef_t *td)
 
int crinitSPrintTimerDef (char *s, crinitTimerDef_t *td)
 
void crinitPrintTimerDef (crinitTimerDef_t *td)
 
bool crinitCheckTimerDef (crinitTimerDef_t *td)
 
bool crinitCheckTimerTime (struct timespec ts, crinitTimerDef_t *td)
 
struct tm * crinitZonedTimeR (const time_t *time, int8_t timezone[2], struct tm *restrict result)
 
struct timespec crinitTimerNextTime (struct timespec *last, crinitTimerDef_t *td)
 

Variables

static const char *const crinitMonthLookup []
 
static const int crinitDayToMonthLookup []
 

Detailed Description

Implementation of functions related to a single timer.

Function Documentation

◆ crinitCheckTimerDef()

bool crinitCheckTimerDef ( crinitTimerDef_t td)

Check if a timer definition gives a valid timer ie. month between 1 and 12, day in 1 to 31, hours 0 to 23, ...

Parameters
tdthe timer definition to set check
Returns
true if the timer definition is valid, and false if not

◆ crinitCheckTimerTime()

bool crinitCheckTimerTime ( struct timespec  ts,
crinitTimerDef_t td 
)

Check if a timerstamp is valid for a timer

Parameters
tsthe timestamp to check.
tdthe crinit timer definition to check against
Returns
true if the timer should trigger at that timestamp, false otherwise

◆ crinitDaysToMonth()

static int crinitDaysToMonth ( uint8_t  m,
uint16_t  year 
)
static

Returns the number of days from start of the year to the first of the specified month ie. 0 for 1 (Jan), 31 for 2 (Feb), 59 (or 60 in a leap year) for 3 (Mar) and so on

Parameters
mthe month to get how many days the year had before the fist of it
yearthe year to check in
Returns
the number of days the year had before the first of that month

◆ crinitIsLeapYear()

static bool crinitIsLeapYear ( uint16_t  year)
static

check if a year is a leap year

Parameters
yearthe year to check
Returns
true if it's a leap year, false if not

◆ crinitMonthLength()

static int crinitMonthLength ( uint8_t  month,
uint16_t  year 
)
static

Get the number of days a month is long.

Parameters
monththe month to get the length of
yearthe year to know if Feb is 28 or 29 days long
Returns
the number of days the month is long

◆ crinitMonthToStr()

static const char * crinitMonthToStr ( uint8_t  m)
static

get a static string with the short month name

Parameters
mthe month starting at 1 for Jan
Returns
"Jan" for 1, "Feb" for 2, ...

◆ crinitNext()

static bool crinitNext ( int  last,
uint8_t  interval[2],
int  start,
int  limit,
int *  res,
long long int *  offs 
)
static

Get the next value for a range.

Parameters
lastthe value to get the next from
intervalthe interval in which the next needs to be
startthe lowest possible value
limitthe highest possible value
resnext value after last
offsthe offset to the last value
Returns
true if the res is after last and false if the next interval needs to be updated as well

◆ crinitNextYear()

static bool crinitNextYear ( int  last,
uint16_t  interval[2],
int *  res,
long long int *  offs 
)
static

get the next year

Parameters
lastthe year from which to start finding the next
intervalthe interval in which the next year needs to be in
resthe resulting next year
offsthe offset the next year has to the last
Returns
true if a next year is found/possible in the interval, false if not

◆ crinitPrintTimerDef()

void crinitPrintTimerDef ( crinitTimerDef_t td)

print a crinitTimerDef_t as crinitInfoPrint message

Parameters
tdthe crinitTimerDef_t to print

◆ crinitSPrintTimerDef()

int crinitSPrintTimerDef ( char *  s,
crinitTimerDef_t td 
)

print a crinitTimerDef_t into a string

Parameters
sthe string to print to
tdthe crinitTimerDef_t to print
Returns
the number of bytes written to s excluding the terminating 0 byte

◆ crinitTimerNextTime()

struct timespec crinitTimerNextTime ( struct timespec *  last,
crinitTimerDef_t td 
)

Calculate the next time the timer should trigger.

Parameters
lastthe last timestamp to calculate the next from
tdthe timer definition to calculate the next time from
Returns
the timestamp the timer is fullfiled next

◆ crinitTimerSetDefault()

void crinitTimerSetDefault ( crinitTimerDef_t td)

Set a timer to the default value: Mon..Sun-0000..65535-01..12-01..31-00:00:00+0000

Parameters
tdthe crinitTimerDef_t to set to default

◆ crinitZonedTimeR()

struct tm * crinitZonedTimeR ( const time_t *  time,
int8_t  timezone[2],
struct tm *restrict  result 
)

Get a struct tm similar to gmtime_r with a specific timezone.

Parameters
timethe timestamp to transform into a tm
timezonethe timezone with hour and minute
resultthe resulting tm struct
Returns
a tm representation in the specyfied timezone

Variable Documentation

◆ crinitDayToMonthLookup

const int crinitDayToMonthLookup[]
static
Initial value:
= {
[1] = 0, [2] = 31, [3] = 59, [4] = 90, [5] = 120, [6] = 151,
[7] = 181, [8] = 212, [9] = 243, [10] = 273, [11] = 304, [12] = 334,
}

◆ crinitMonthLookup

const char* const crinitMonthLookup[]
static
Initial value:
= {
[0] = "", [1] = "Jan", [2] = "Feb", [3] = "Mar", [4] = "Apr", [5] = "May", [6] = "Jun",
[7] = "Jul", [8] = "Aug", [9] = "Sep", [10] = "Oct", [11] = "Nov", [12] = "Dec",
}