tree-wide: fix how we set $TZ

According to tzset(3) we need to prefix timezone names with ":". Let's
do so hence, to avoid any ambiguities and follow documented behaviour.
This commit is contained in:
Lennart Poettering 2019-11-12 17:52:35 +01:00 committed by Zbigniew Jędrzejewski-Szmek
parent d5fc5b2f8d
commit 437f48a471
6 changed files with 23 additions and 11 deletions

View File

@ -136,9 +136,8 @@
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

View 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);
}

View 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);
}

View 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();

View 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"));

View 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();