Merge pull request #401 from Stunkymonkey/format_placeholder-time

use format_placeholder for time
This commit is contained in:
Ingo Bürk 2020-04-03 08:38:30 +02:00 committed by GitHub
commit 1858cd8dfb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -10,6 +10,8 @@
#include "i3status.h" #include "i3status.h"
#define STRING_SIZE 50
static bool local_timezone_init = false; static bool local_timezone_init = false;
static const char *local_timezone = NULL; static const char *local_timezone = NULL;
static const char *current_timezone = NULL; static const char *current_timezone = NULL;
@ -36,10 +38,8 @@ void set_timezone(const char *tz) {
} }
void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t) { void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool hide_if_equals_localtime, time_t t) {
const char *walk;
char *outwalk = buffer; char *outwalk = buffer;
struct tm local_tm, tm; struct tm local_tm, tm;
char timebuf[1024];
if (title != NULL) if (title != NULL)
INSTANCE(title); INSTANCE(title);
@ -61,23 +61,18 @@ void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *
setlocale(LC_ALL, locale); setlocale(LC_ALL, locale);
} }
char string_time[STRING_SIZE];
if (format_time == NULL) { if (format_time == NULL) {
strftime(timebuf, sizeof(timebuf), format, &tm); strftime(string_time, sizeof(string_time), format, &tm);
maybe_escape_markup(timebuf, &outwalk); maybe_escape_markup(string_time, &outwalk);
} else { } else {
for (walk = format; *walk != '\0'; walk++) { strftime(string_time, sizeof(string_time), format_time, &tm);
if (*walk != '%') { placeholder_t placeholders[] = {
*(outwalk++) = *walk; {.name = "%time", .value = string_time}};
} else if (BEGINS_WITH(walk + 1, "time")) { const size_t num = sizeof(placeholders) / sizeof(placeholder_t);
strftime(timebuf, sizeof(timebuf), format_time, &tm); buffer = format_placeholders(format_time, &placeholders[0], num);
maybe_escape_markup(timebuf, &outwalk);
walk += strlen("time");
} else {
*(outwalk++) = '%';
}
}
} }
if (locale != NULL) { if (locale != NULL) {