aboutsummaryrefslogtreecommitdiff
path: root/src/blocks/date.c
diff options
context:
space:
mode:
authorFederico Angelilli <code@fedang.net>2024-09-08 22:41:53 +0200
committerFederico Angelilli <code@fedang.net>2024-09-08 22:41:53 +0200
commitf1f89a745fb46c4b387f3cb6c094e9dd38edf9f6 (patch)
treee696fbcd8e3142c7e64e4b1e3148b16f96b79730 /src/blocks/date.c
parentee8f2ac000a577f20a1a38c5ddd20831a8e38311 (diff)
Add date block
Diffstat (limited to 'src/blocks/date.c')
-rw-r--r--src/blocks/date.c53
1 files changed, 53 insertions, 0 deletions
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 <string.h>
+#include <assert.h>
+#include <time.h>
+
+#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,
+};