chiark / gitweb /
timedatectl: fix when queried system has differn't timezone
[elogind.git] / src / timedate / timedatectl.c
index 1f85536cf2a60feb1f281424564ceaa747ed67d1..44d329ecc2817c4408bd158139e43f731b500a62 100644 (file)
 
 #include <stdlib.h>
 #include <stdbool.h>
-#include <unistd.h>
 #include <getopt.h>
 #include <locale.h>
-#include <string.h>
-#include <sys/timex.h>
 
 #include "sd-bus.h"
 #include "bus-util.h"
@@ -38,8 +35,6 @@
 #include "pager.h"
 #include "time-dst.h"
 
-#define xstrftime(buf, fmt, tm) assert_se(strftime(buf, sizeof(buf), fmt, tm) > 0)
-
 static bool arg_no_pager = false;
 static bool arg_ask_password = true;
 static BusTransport arg_transport = BUS_TRANSPORT_LOCAL;
@@ -111,14 +106,21 @@ static void print_status_info(const StatusInfo *i) {
 
         /* Enforce the values of /etc/localtime */
         if (getenv("TZ")) {
-                fprintf(stderr, "Warning: Ignoring the TZ variable. Reading the system's time zone setting only.\n\n");
+                fprintf(stderr, "Warning: Ignoring the TZ variable.\n\n");
                 unsetenv("TZ");
         }
 
+        r = setenv("TZ", i->timezone, false);
+        if (r < 0) {
+                log_error_errno(errno, "Failed to set TZ environment variable: %m");
+                exit(EXIT_FAILURE);
+        }
+        tzset();
+
         if (i->time != 0) {
                 sec = (time_t) (i->time / USEC_PER_SEC);
                 have_time = true;
-        } else if (arg_transport == BUS_TRANSPORT_LOCAL) {
+        } else if (IN_SET(arg_transport, BUS_TRANSPORT_REMOTE, BUS_TRANSPORT_MACHINE)) {
                 sec = time(NULL);
                 have_time = true;
         } else