]> git.sur5r.net Git - i3/i3status/commitdiff
Add timezone switch
authoreplanet <emeric.planet@gmail.com>
Sat, 10 Nov 2018 09:47:05 +0000 (10:47 +0100)
committereplanet <emeric.planet@gmail.com>
Sat, 10 Nov 2018 09:47:05 +0000 (10:47 +0100)
i3status.c
include/i3status.h
man/i3status.man
src/print_time.c

index da4bc78828ce65f4327bd2319b98581ca22ab2c1..ea29a3577486e38623fe8e9c94ff434099a11e9e 100644 (file)
@@ -399,6 +399,7 @@ int main(int argc, char *argv[]) {
         CFG_STR("timezone", "", CFGF_NONE),
         CFG_STR("locale", "", CFGF_NONE),
         CFG_STR("format_time", NULL, CFGF_NONE),
         CFG_STR("timezone", "", CFGF_NONE),
         CFG_STR("locale", "", CFGF_NONE),
         CFG_STR("format_time", NULL, CFGF_NONE),
+        CFG_BOOL("only_when_tz_different", false, CFGF_NONE),
         CFG_CUSTOM_ALIGN_OPT,
         CFG_CUSTOM_MIN_WIDTH_OPT,
         CFG_CUSTOM_SEPARATOR_OPT,
         CFG_CUSTOM_ALIGN_OPT,
         CFG_CUSTOM_MIN_WIDTH_OPT,
         CFG_CUSTOM_SEPARATOR_OPT,
@@ -749,13 +750,13 @@ int main(int argc, char *argv[]) {
 
             CASE_SEC("time") {
                 SEC_OPEN_MAP("time");
 
             CASE_SEC("time") {
                 SEC_OPEN_MAP("time");
-                print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, NULL, tv.tv_sec);
+                print_time(json_gen, buffer, NULL, cfg_getstr(sec, "format"), NULL, NULL, NULL, false, tv.tv_sec);
                 SEC_CLOSE_MAP;
             }
 
             CASE_SEC_TITLE("tztime") {
                 SEC_OPEN_MAP("tztime");
                 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, "locale"), 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"), cfg_getbool(sec, "only_when_tz_different"), tv.tv_sec);
                 SEC_CLOSE_MAP;
             }
 
                 SEC_CLOSE_MAP;
             }
 
index 3a8a7932f7b18a5dd5d7f894ea282808fe9fdbf2..e377d5bf70c7fa911b0a6463baf952509d9825c7 100644 (file)
@@ -214,7 +214,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_below_threshold, 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_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_below_threshold, 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 *locale, 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, bool only_when_tz_different, 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, int family);
 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *quality_min_lenght);
 void print_ddate(yajl_gen json_gen, char *buffer, const char *format, time_t t);
 const char *get_ip_addr(const char *interface, int family);
 void print_wireless_info(yajl_gen json_gen, char *buffer, const char *interface, const char *format_up, const char *format_down, const char *quality_min_lenght);
index 7d27ae32ad043ecb8c74bd3671ba33ae86296f69..b3f71bf7782f6743fbf6a7557b28409569664504 100644 (file)
@@ -97,6 +97,7 @@ path_exists VPN {
 
 tztime local {
         format = "%Y-%m-%d %H:%M:%S"
 
 tztime local {
         format = "%Y-%m-%d %H:%M:%S"
+        only_when_tz_different = true
 }
 
 tztime berlin {
 }
 
 tztime berlin {
@@ -514,6 +515,8 @@ 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.
 +/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.
+To display time only when the timezone is different from local timezone, set
++only_when_tz_different+ to true.
 
 *Example order*: +tztime berlin+
 
 
 *Example order*: +tztime berlin+
 
@@ -533,6 +536,7 @@ tztime berlin {
        format = "<span foreground='#ffffff'>time:</span> %time"
        format_time = "%H:%M %Z"
        timezone = "Europe/Berlin"
        format = "<span foreground='#ffffff'>time:</span> %time"
        format_time = "%H:%M %Z"
        timezone = "Europe/Berlin"
+       only_when_tz_different = false
 }
 -------------------------------------------------------------
 
 }
 -------------------------------------------------------------
 
index 3a6c4cc0752757aa77170500baf4b9e6e1aa2b37..c124e1a7223624366ab2f4acce716eb967175ef4 100644 (file)
@@ -34,18 +34,28 @@ void set_timezone(const char *tz) {
     tzset();
 }
 
     tzset();
 }
 
-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_time(yajl_gen json_gen, char *buffer, const char *title, const char *format, const char *tz, const char *locale, const char *format_time, bool only_when_tz_different, time_t t) {
     const char *walk;
     char *outwalk = buffer;
     const char *walk;
     char *outwalk = buffer;
-    struct tm tm;
+    struct tm local_tm, tm;
     char timebuf[1024];
 
     if (title != NULL)
         INSTANCE(title);
 
     char timebuf[1024];
 
     if (title != NULL)
         INSTANCE(title);
 
+    set_timezone(NULL);
+    localtime_r(&t, &local_tm);
+
     set_timezone(tz);
     localtime_r(&t, &tm);
 
     set_timezone(tz);
     localtime_r(&t, &tm);
 
+    // When only_when_tz_different is true, compare local and target time to display only if different
+    time_t local_t = mktime(&local_tm);
+    double diff = difftime(local_t, t);
+    if (only_when_tz_different && diff == 0.0) {
+        return;
+    }
+
     if (locale != NULL) {
         setlocale(LC_ALL, locale);
     }
     if (locale != NULL) {
         setlocale(LC_ALL, locale);
     }