#include #include #include #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; } static config_status_t block_date_change(block_t *block, config_t *config, const char *key, const char *value) { block_date_t *date = (block_date_t *)block; if (!strcmp(key, "text")) { free(date->format); date->format = strcopy(value); return CONFIG_SUCCESS; } extern const config_entry_t block_text_entries[]; return config_read_entry(block_text_entries, block, NULL, "block", block->label, key, value); } 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, .change_fn = block_date_change, };