[systemd-devel] [PATCH] timedatectl: check for getenv("TZDIR")

Shawn Landden shawn at churchofgit.com
Tue Mar 24 11:45:56 PDT 2015


I liked having the DST information. It is a pity glibc doesn't export
this information.

avoid calling gmtime_r() and localtime_r() twice
deduplicate some strings

v2
---
 src/timedate/timedatectl.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c
index ab5c8a1..8073111 100644
--- a/src/timedate/timedatectl.c
+++ b/src/timedate/timedatectl.c
@@ -74,7 +74,7 @@ typedef struct StatusInfo {
 
 static void print_status_info(const StatusInfo *i) {
         char a[FORMAT_TIMESTAMP_MAX];
-        struct tm tm;
+        struct tm localtime, gmtime;
         time_t sec;
         bool have_time = false;
         _cleanup_free_ char *zc = NULL, *zn = NULL;
@@ -84,10 +84,15 @@ static void print_status_info(const StatusInfo *i) {
 
         /* Enforce the values of /etc/localtime */
         if (getenv("TZ")) {
-                fprintf(stderr, "Warning: Ignoring the TZ variable.\n\n");
+                fprintf(stderr, "Warning: Ignoring the %s variable.\n\n", "TZ");
                 unsetenv("TZ");
         }
 
+        if (getenv("TZDIR")) {
+                fprintf(stderr, "Warning: Ignoring the %s variable.\n\n", "TZDIR");
+                unsetenv("TZDIR");
+        }
+
         r = setenv("TZ", i->timezone, false);
         if (r < 0) {
                 log_error_errno(errno, "Failed to set TZ environment variable: %m");
@@ -105,27 +110,30 @@ static void print_status_info(const StatusInfo *i) {
                 fprintf(stderr, "Warning: Could not get time from timedated and not operating locally.\n\n");
 
         if (have_time) {
-                xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
+                localtime_r(&sec, &localtime);
+                gmtime_r(&sec, &gmtime);
+
+                xstrftime(a, "%a %Y-%m-%d %H:%M:%S %Z", localtime);
                 printf("      Local time: %.*s\n", (int) sizeof(a), a);
 
-                xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
+                xstrftime(a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime);
                 printf("  Universal time: %.*s\n", (int) sizeof(a), a);
         } else {
-                printf("      Local time: %s\n", "n/a");
-                printf("  Universal time: %s\n", "n/a");
+                printf("      Local time: %.*s\n", (int) strlen("n/a"), "n/a");
+                printf("  Universal time: %.*s\n", (int) strlen("n/a"), "n/a");
         }
 
         if (i->rtc_time > 0) {
                 time_t rtc_sec;
 
                 rtc_sec = (time_t)(i->rtc_time / USEC_PER_SEC);
-                xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
+                xstrftime(a, "%a %Y-%m-%d %H:%M:%S", gmtime);
                 printf("        RTC time: %.*s\n", (int) sizeof(a), a);
         } else
                 printf("        RTC time: %s\n", "n/a");
 
         if (have_time)
-                xstrftime(a, "%Z, %z", localtime_r(&sec, &tm));
+                xstrftime(a, "%Z, %z", localtime);
 
         printf("       Time zone: %s (%.*s)\n"
                "     NTP enabled: %s\n"
-- 
2.2.1.209.g41e5f3a



More information about the systemd-devel mailing list