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
54
55
56
57
58
59
60
61
62
63
|
#include <string.h>
#include <assert.h>
#include <time.h>
#include "scheme.h"
#include "../any_log.h"
typedef struct {
block_text_t block;
char *format;
} block_date_t;
static void block_date_update(block_t *block)
{
block_date_t *date = (block_date_t *)block;
time_t epoch = time(NULL);
struct tm *loc_time = localtime(&epoch);
char buffer[64];
strftime(buffer, sizeof(buffer), date->format, loc_time);
free(date->block.text);
date->block.text = strcopy(buffer);
assert(date->block.text != NULL);
}
static void block_date_clean(block_t *block)
{
block_date_t *date = (block_date_t *)block;
free(date->format);
}
static bool block_date_validate(block_t *block, const block_scheme_t *scheme)
{
block_date_t *date = (block_date_t *)block;
if (date->block.text == NULL) {
log_error("Block '%s' requires key '%s'", block->label, "text");
return false;
}
date->format = date->block.text;
date->block.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_fn = block_date_update,
.clean_fn = block_date_clean,
},
.size = sizeof(block_date_t),
.entries = NULL,
.validate = block_date_validate,
};
|