]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: fix how we set $TZ
authorLennart Poettering <lennart@poettering.net>
Tue, 12 Nov 2019 16:52:35 +0000 (17:52 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Wed, 13 Nov 2019 11:30:22 +0000 (12:30 +0100)
According to tzset(3) we need to prefix timezone names with ":". Let's
do so hence, to avoid any ambiguities and follow documented behaviour.

man/systemd.time.xml
src/basic/time-util.c
src/shared/calendarspec.c
src/test/test-calendarspec.c
src/test/test-time-util.c
src/timedate/timedatectl.c

index c7d5f24b3c236c951b4291a1c722bfa7e41a8a70..dc1e78a18752b7cf7c9ce6918df2d5152f8e2227 100644 (file)
     evaluated relative to the UNIX time epoch 1st Jan, 1970,
     00:00.</para>
 
-    <para>Examples for valid timestamps and their normalized form
-    (assuming the current time was 2012-11-23 18:15:22 and the timezone
-    was UTC+8, for example TZ=Asia/Shanghai):</para>
+    <para>Examples for valid timestamps and their normalized form (assuming the current time was 2012-11-23
+    18:15:22 and the timezone was UTC+8, for example <literal>TZ=:Asia/Shanghai</literal>):</para>
 
     <programlisting>  Fri 2012-11-23 11:12:13 → Fri 2012-11-23 11:12:13
       2012-11-23 11:12:13 → Fri 2012-11-23 11:12:13
index f5be619ad66511fcdda2067d777cb1439d1f9f77..e2c9c47e572dd5be20966c490a23e8501716ec11 100644 (file)
@@ -832,8 +832,12 @@ int parse_timestamp(const char *t, usec_t *usec) {
         }
         if (r == 0) {
                 bool with_tz = true;
+                char *colon_tz;
 
-                if (setenv("TZ", tz, 1) != 0) {
+                /* tzset(3) says $TZ should be prefixed with ":" if we reference timezone files */
+                colon_tz = strjoina(":", tz);
+
+                if (setenv("TZ", colon_tz, 1) != 0) {
                         shared->return_value = negative_errno();
                         _exit(EXIT_FAILURE);
                 }
index aa0d6a8224f8c9b5b21d67b4f91e27201ed1e0bb..217ab3fbaf47eea8a862126edac9b21b6abef8b0 100644 (file)
@@ -1352,7 +1352,12 @@ int calendar_spec_next_usec(const CalendarSpec *spec, usec_t usec, usec_t *ret_n
                 return r;
         }
         if (r == 0) {
-                if (setenv("TZ", spec->timezone, 1) != 0) {
+                char *colon_tz;
+
+                /* tzset(3) says $TZ should be prefixed with ":" if we reference timezone files */
+                colon_tz = strjoina(":", spec->timezone);
+
+                if (setenv("TZ", colon_tz, 1) != 0) {
                         shared->return_value = negative_errno();
                         _exit(EXIT_FAILURE);
                 }
index 899e4b5d27775767ad10b5be9c526fb263aad42a..9c2be7f44566cc342583d6865ef93d68e888a2e9 100644 (file)
@@ -43,9 +43,12 @@ static void test_next(const char *input, const char *new_tz, usec_t after, usec_
         if (old_tz)
                 old_tz = strdupa(old_tz);
 
-        if (new_tz)
-                assert_se(setenv("TZ", new_tz, 1) >= 0);
-        else
+        if (new_tz) {
+                char *colon_tz;
+
+                colon_tz = strjoina(":", new_tz);
+                assert_se(setenv("TZ", colon_tz, 1) >= 0);
+        } else
                 assert_se(unsetenv("TZ") >= 0);
         tzset();
 
index d05bb614290e1ff9479b725f4ed23bb37adbee49..a422cc8ddc63e5a4e1a5c5a686cf65e7b6400241 100644 (file)
@@ -475,7 +475,7 @@ static void test_in_utc_timezone(void) {
         assert_se(timezone == 0);
         assert_se(daylight == 0);
 
-        assert_se(setenv("TZ", "Europe/Berlin", 1) >= 0);
+        assert_se(setenv("TZ", ":Europe/Berlin", 1) >= 0);
         assert_se(!in_utc_timezone());
         assert_se(streq(tzname[0], "CET"));
         assert_se(streq(tzname[1], "CEST"));
index 2f9073c3dc5d5ee693ab496e35b19f85e2496b90..2a98c48987a3e0fb2911e6e6ff768e389e8dfd1d 100644 (file)
@@ -46,7 +46,7 @@ typedef struct StatusInfo {
 } StatusInfo;
 
 static void print_status_info(const StatusInfo *i) {
-        const char *old_tz = NULL, *tz;
+        const char *old_tz = NULL, *tz, *tz_colon;
         bool have_time = false;
         char a[LINE_MAX];
         struct tm tm;
@@ -62,7 +62,8 @@ static void print_status_info(const StatusInfo *i) {
                 old_tz = strdupa(tz);
 
         /* Set the new $TZ */
-        if (setenv("TZ", isempty(i->timezone) ? "UTC" : i->timezone, true) < 0)
+        tz_colon = strjoina(":", isempty(i->timezone) ? "UTC" : i->timezone);
+        if (setenv("TZ", tz_colon, true) < 0)
                 log_warning_errno(errno, "Failed to set TZ environment variable, ignoring: %m");
         else
                 tzset();