Setting of custom locale in tztime configuration. (#168)

To be able to show my birth country's time zone in that country's locale, and my local time in my current locale.
This commit is contained in:
Björn Lindström 2016-10-24 13:43:04 +07:00 committed by Michael Stapelberg
parent 707ceffc8b
commit be87c5ac38
4 changed files with 17 additions and 4 deletions

View File

@ -385,6 +385,7 @@ int main(int argc, char *argv[]) {
cfg_opt_t tztime_opts[] = {
CFG_STR("format", "%Y-%m-%d %H:%M:%S %Z", CFGF_NONE),
CFG_STR("timezone", "", CFGF_NONE),
CFG_STR("locale", "", CFGF_NONE),
CFG_STR("format_time", NULL, CFGF_NONE),
CFG_CUSTOM_ALIGN_OPT,
CFG_CUSTOM_MIN_WIDTH_OPT,
@ -694,13 +695,13 @@ int main(int argc, char *argv[]) {
CASE_SEC("time") {
SEC_OPEN_MAP("time");
print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, tv.tv_sec);
print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, NULL, tv.tv_sec);
SEC_CLOSE_MAP;
}
CASE_SEC_TITLE("tztime") {
SEC_OPEN_MAP("tztime");
print_time(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), cfg_getstr(sec, "format_time"), tv.tv_sec);
print_time(json_gen, buffer, title, cfg_getstr(sec, "format"), cfg_getstr(sec, "timezone"), cfg_getstr(sec, "locale"), cfg_getstr(sec, "format_time"), tv.tv_sec);
SEC_CLOSE_MAP;
}

View File

@ -207,7 +207,7 @@ const char *first_eth_interface(const net_type_t type);
void print_ipv6_info(yajl_gen json_gen, char *buffer, const char *format_up, const char *format_down);
void print_disk_info(yajl_gen json_gen, char *buffer, const char *path, const char *format, const char *format_not_mounted, const char *prefix_type, const char *threshold_type, const double low_threshold);
void print_battery_info(yajl_gen json_gen, char *buffer, int number, const char *path, const char *format, const char *format_down, const char *status_chr, const char *status_bat, const char *status_unk, const char *status_full, int low_threshold, char *threshold_type, bool last_full_capacity, bool integer_battery_capacity, bool hide_seconds);
void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *format_time, 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, time_t t);
void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
const char *get_ip_addr(const char *interface);
void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down);

View File

@ -439,6 +439,7 @@ The system's timezone database is usually installed in +/usr/share/zoneinfo+.
Files below that path make for valid timezone strings, e.g. for
+/usr/share/zoneinfo/Europe/Berlin+ you can set timezone to +Europe/Berlin+
in the +tztime+ module.
To override the locale settings of your environment, set the +locale+ option.
*Example order*: +tztime berlin+
@ -446,6 +447,8 @@ in the +tztime+ module.
*Example timezone*: +Europe/Berlin+
*Example locale*: +de_DE.UTF-8+
If you would like to use markup in this section, there is a separate
+format_time+ option that is automatically escaped. Its output then replaces
%time in the format string.

View File

@ -3,6 +3,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <locale.h>
#include <yajl/yajl_gen.h>
#include <yajl/yajl_version.h>
@ -33,7 +34,7 @@ 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 *format_time, 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, time_t t) {
const char *walk;
char *outwalk = buffer;
struct tm tm;
@ -45,6 +46,10 @@ void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *
set_timezone(tz);
localtime_r(&t, &tm);
if (locale != NULL) {
setlocale(LC_ALL, locale);
}
if (format_time == NULL) {
strftime(timebuf, sizeof(timebuf), format, &tm);
maybe_escape_markup(timebuf, &outwalk);
@ -63,6 +68,10 @@ void print_time(yajl_gen json_gen, char *buffer, const char *title, const char *
}
}
if (locale != NULL) {
setlocale(LC_ALL, "");
}
*outwalk = '\0';
OUTPUT_FULL_TEXT(buffer);
}