From 7d6f1d695c5760d6eb403a1829e6b0cc7e9b0ed8 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Sun, 28 Sep 2025 10:10:47 +0900 Subject: [PATCH] timedatectl,test: use SAVE_TIMEZONE at more places --- src/test/test-calendarspec.c | 8 +------- src/timedate/timedatectl.c | 13 ++----------- 2 files changed, 3 insertions(+), 18 deletions(-) diff --git a/src/test/test-calendarspec.c b/src/test/test-calendarspec.c index 9abbde622f0..0b1cbacd625 100644 --- a/src/test/test-calendarspec.c +++ b/src/test/test-calendarspec.c @@ -44,12 +44,9 @@ static void _test_one(int line, const char *input, const char *output) { static void _test_next(int line, const char *input, const char *new_tz, usec_t after, usec_t expect) { _cleanup_(calendar_spec_freep) CalendarSpec *c = NULL; usec_t u; - char *old_tz; int r; - old_tz = getenv("TZ"); - if (old_tz) - old_tz = strdupa_safe(old_tz); + SAVE_TIMEZONE; if (!isempty(new_tz) && !strchr(new_tz, ',')) new_tz = strjoina(":", new_tz); @@ -68,9 +65,6 @@ static void _test_next(int line, const char *input, const char *new_tz, usec_t a assert_se(r >= 0 && u == expect); else assert_se(r == -ENOENT); - - assert_se(set_unset_env("TZ", old_tz, true) == 0); - tzset(); } #define test_next(input, new_tz, after, expect) _test_next(__LINE__, input,new_tz,after,expect) diff --git a/src/timedate/timedatectl.c b/src/timedate/timedatectl.c index 0bbc740d469..f3a8046bede 100644 --- a/src/timedate/timedatectl.c +++ b/src/timedate/timedatectl.c @@ -58,7 +58,7 @@ typedef struct StatusInfo { static int print_status_info(const StatusInfo *i) { _cleanup_(table_unrefp) Table *table = NULL; - const char *old_tz = NULL, *tz, *tz_colon; + const char *tz_colon; char a[LINE_MAX]; TableCell *cell; struct tm tm; @@ -79,9 +79,7 @@ static int print_status_info(const StatusInfo *i) { (void) table_set_ellipsize_percent(table, cell, 100); /* Save the old $TZ */ - tz = getenv("TZ"); - if (tz) - old_tz = strdupa_safe(tz); + SAVE_TIMEZONE; /* Set the new $TZ */ tz_colon = strjoina(":", isempty(i->timezone) ? "UTC" : i->timezone); @@ -158,13 +156,6 @@ static int print_status_info(const StatusInfo *i) { if (r < 0) return table_log_add_error(r); - /* Restore the $TZ */ - r = set_unset_env("TZ", old_tz, true); - if (r < 0) - log_warning_errno(r, "Failed to set TZ environment variable, ignoring: %m"); - else - tzset(); - r = table_add_many(table, TABLE_FIELD, "System clock synchronized", TABLE_BOOLEAN, i->ntp_synced, -- 2.47.3