module/clock: detect when we need to update every second

Closes #12
This commit is contained in:
Daniel Eklöf 2020-09-24 15:59:46 +02:00
parent 318965b715
commit 0cde404d2a
No known key found for this signature in database
GPG key ID: 5BBD4992C116573F
2 changed files with 27 additions and 0 deletions

View file

@ -18,6 +18,8 @@
* YAML parsing error messages being replaced with a generic _“unknown * YAML parsing error messages being replaced with a generic _“unknown
error”_. error”_.
* Memory leak when a YAML parsing error was encountered. * Memory leak when a YAML parsing error was encountered.
* clock: update every second when necessary
(https://codeberg.org/dnkl/yambar/issues/12).
### Security ### Security

View file

@ -7,6 +7,7 @@
#include <sys/time.h> #include <sys/time.h>
#define LOG_MODULE "clock" #define LOG_MODULE "clock"
#define LOG_ENABLE_DBG 0
#include "../log.h" #include "../log.h"
#include "../bar/bar.h" #include "../bar/bar.h"
#include "../config.h" #include "../config.h"
@ -129,8 +130,32 @@ clock_new(struct particle *label, const char *date_format, const char *time_form
m->label = label; m->label = label;
m->date_format = strdup(date_format); m->date_format = strdup(date_format);
m->time_format = strdup(time_format); m->time_format = strdup(time_format);
static const char *const seconds_formatters[] = {
"%c",
"%s",
"%S",
"%T",
"%r",
"%X",
};
m->update_granularity = UPDATE_GRANULARITY_MINUTES; m->update_granularity = UPDATE_GRANULARITY_MINUTES;
for (size_t i = 0;
i < sizeof(seconds_formatters) / sizeof(seconds_formatters[0]);
i++)
{
if (strstr(time_format, seconds_formatters[i]) != NULL) {
m->update_granularity = UPDATE_GRANULARITY_SECONDS;
break;
}
}
LOG_DBG("using %s update granularity",
(m->update_granularity == UPDATE_GRANULARITY_MINUTES
? "minutes" : "seconds"));
struct module *mod = module_common_new(); struct module *mod = module_common_new();
mod->private = m; mod->private = m;
mod->run = &run; mod->run = &run;