#ifndef C8_CLOCK_H
#define C8_CLOCK_H
#include <langinfo.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
/*
* The clock setting, [general] clock in config.ini: "auto" shows the
* time as the locale does (LC_TIME must be set from the environment),
* "12" or "24" force it. The same file is in c8-shell, c8-widgets and c8-weather.
*/
static inline bool clock_12h(const char *mode) {
if (mode && strcmp(mode, "12") == 0) {
return true;
}
if (mode && strcmp(mode, "24") == 0) {
return false;
}
const char *fmt = nl_langinfo(T_FMT);
return fmt && (strstr(fmt, "%I") || strstr(fmt, "%l") ||
strstr(fmt, "%r") || strstr(fmt, "%p"));
}
/* The time to the minute: 19:05, or 7:05 (with_ampm: 7:05 pm, as the
* locale spells it). Big clocks leave the am/pm out, as Windows 8 did. */
static inline void clock_format(char *buf, size_t len, const struct tm *tm,
const char *mode, bool with_ampm) {
if (!clock_12h(mode)) {
strftime(buf, len, "%H:%M", tm);
} else if (with_ampm) {
strftime(buf, len, "%-I:%M %p", tm);
} else {
strftime(buf, len, "%-I:%M", tm);
}
}
#endif