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
64
65
66
67
68
69
70
|
#include <string.h>
#include <assert.h>
#include <time.h>
#include "../block.h"
#include "../any_log.h"
typedef struct {
block_text_t block;
char *format;
} block_date_t;
static const struct timespec block_date_interval = {
.tv_sec = 1,
.tv_nsec = 0,
};
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_init(block_t *block)
{
extern const block_scheme_t block_text_scheme;
block_text_scheme.init_fn(block);
block->update_interval = block_date_interval;
block->update_fn = block_date_update;
}
static void block_date_clean(block_t *block)
{
block_date_t *date = (block_date_t *)block;
free(date->format);
free(date->block.text);
}
static int block_date_validate(block_t *block, config_t *config)
{
block_date_t *date = (block_date_t *)block;
int errors = 0;
if (date->block.text == NULL) {
log_error("Block '%s' requires key '%s'", block->label, "text");
errors++;
}
date->format = strcopy(date->block.text);
return errors;
}
const block_scheme_t block_date_scheme = {
.name = "date",
.entries = NULL,
.size = sizeof(block_date_t),
.init_fn = block_date_init,
.clean_fn = block_date_clean,
.validate_fn = block_date_validate,
};
|