From f1f89a745fb46c4b387f3cb6c094e9dd38edf9f6 Mon Sep 17 00:00:00 2001 From: Federico Angelilli Date: Sun, 8 Sep 2024 22:41:53 +0200 Subject: Add date block --- src/blocks/date.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 src/blocks/date.c (limited to 'src/blocks/date.c') diff --git a/src/blocks/date.c b/src/blocks/date.c new file mode 100644 index 0000000..01c62c3 --- /dev/null +++ b/src/blocks/date.c @@ -0,0 +1,53 @@ +#include +#include +#include + +#include "scheme.h" + +#include "../any_log.h" + +static void block_date_update(block_t *block) +{ + time_t epoch = time(NULL); + struct tm *loc_time = localtime(&epoch); + + char buffer[64]; + strftime(buffer, sizeof(buffer), block->state, loc_time); + + free(block->text.text); + block->text.text = strcopy(buffer); + assert(block->text.text != NULL); +} + +static void block_date_finalize(block_t *block) +{ + free(block->state); +} + +static bool block_date_validate(block_t *block, const block_scheme_t *scheme) +{ + if (block->text.text == NULL) { + log_error("Block '%s' requires key '%s'", block->label, "text"); + return false; + } + + block->state = block->text.text; + block->text.text = NULL; + return true; +} + +const block_scheme_t block_date_scheme = { + .name = "date", + .block = { + .type = BLOCK_TEXT, + .update_interval = { + .tv_sec = 1, + .tv_nsec = 0, + }, + .update_cb = block_date_update, + .finalize_cb = block_date_finalize, + }, + .size = sizeof(char *), + .entries = NULL, + .validate = block_date_validate, +}; -- cgit v1.2.3