aboutsummaryrefslogtreecommitdiff
path: root/src/blocks/date.c
blob: 01c62c3298a1b2e4ef838ac5c2ce8f58a481b040 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
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,
};