From be87c5ac3881886b2274dc270535d34c4eb82bf3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Lindstr=C3=B6m?= Date: Mon, 24 Oct 2016 13:43:04 +0700 Subject: [PATCH] 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. --- i3status.c | 5 +++-- include/i3status.h | 2 +- man/i3status.man | 3 +++ src/print_time.c | 11 ++++++++++- 4 files changed, 17 insertions(+), 4 deletions(-) diff --git a/i3status.c b/i3status.c index 239729e..85ada1b 100644 --- a/i3status.c +++ b/i3status.c @@ -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; } diff --git a/include/i3status.h b/include/i3status.h index d168f74..d2b7064 100644 --- a/include/i3status.h +++ b/include/i3status.h @@ -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); diff --git a/man/i3status.man b/man/i3status.man index cb1c248..10b9d7f 100644 --- a/man/i3status.man +++ b/man/i3status.man @@ -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. diff --git a/src/print_time.c b/src/print_time.c index 9fa6642..c8da9d6 100644 --- a/src/print_time.c +++ b/src/print_time.c @@ -3,6 +3,7 @@ #include #include #include +#include #include #include @@ -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); }