summaryrefslogtreecommitdiff
path: root/src/timedate
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-04-07 17:31:34 +0200
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2021-04-08 13:00:50 +0200
commita03e335b86a44285c38514be2f2be9883ea7d294 (patch)
tree65168655ccb4be7aae21e1c3ce5d05dcecfffca4 /src/timedate
parent46cbdcd9fe8e6f30f1e3ec3ce4d0360ac4f55243 (diff)
downloadsystemd-a03e335b86a44285c38514be2f2be9883ea7d294.tar.gz
timedatectl: rework handling of conditions in print_status_info()
gcc-11.0.1-0.3.fc34.x86_64 was complaining that n might be unset with --optimization=1. It was wrong, but let's rework the code to make it obvious that it is always set.
Diffstat (limited to 'src/timedate')
-rw-r--r--src/timedate/timedatectl.c25
1 files changed, 9 insertions, 16 deletions
diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c
index fb08f9ad26..4cab8acbd9 100644
--- a/src/timedate/timedatectl.c
+++ b/src/timedate/timedatectl.c
@@ -96,21 +96,17 @@ static int print_status_info(const StatusInfo *i) {
} else
log_warning("Could not get time from timedated and not operating locally, ignoring.");
- if (have_time)
- n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm));
-
+ n = have_time ? strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S %Z", localtime_r(&sec, &tm)) : 0;
r = table_add_many(table,
TABLE_STRING, "Local time:",
- TABLE_STRING, have_time && n > 0 ? a : "n/a");
+ TABLE_STRING, n > 0 ? a : "n/a");
if (r < 0)
return table_log_add_error(r);
- if (have_time)
- n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm));
-
+ n = have_time ? strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S UTC", gmtime_r(&sec, &tm)) : 0;
r = table_add_many(table,
TABLE_STRING, "Universal time:",
- TABLE_STRING, have_time && n > 0 ? a : "n/a");
+ TABLE_STRING, n > 0 ? a : "n/a");
if (r < 0)
return table_log_add_error(r);
@@ -119,26 +115,23 @@ static int print_status_info(const StatusInfo *i) {
rtc_sec = (time_t) (i->rtc_time / USEC_PER_SEC);
n = strftime(a, sizeof a, "%a %Y-%m-%d %H:%M:%S", gmtime_r(&rtc_sec, &tm));
- }
-
+ } else
+ n = 0;
r = table_add_many(table,
TABLE_STRING, "RTC time:",
- TABLE_STRING, i->rtc_time > 0 && n > 0 ? a : "n/a");
+ TABLE_STRING, n > 0 ? a : "n/a");
if (r < 0)
return table_log_add_error(r);
- if (have_time)
- n = strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm));
-
r = table_add_cell(table, NULL, TABLE_STRING, "Time zone:");
if (r < 0)
return table_log_add_error(r);
- r = table_add_cell_stringf(table, NULL, "%s (%s)", strna(i->timezone), have_time && n > 0 ? a : "n/a");
+ n = have_time ? strftime(a, sizeof a, "%Z, %z", localtime_r(&sec, &tm)) : 0;
+ r = table_add_cell_stringf(table, NULL, "%s (%s)", strna(i->timezone), n > 0 ? a : "n/a");
if (r < 0)
return table_log_add_error(r);
-
/* Restore the $TZ */
r = set_unset_env("TZ", old_tz, true);
if (r < 0)