time-util: introduce SAVE_TIMEZONE macro

This commit is contained in:
Yu Watanabe
2025-09-27 11:51:06 +09:00
parent cb19dbb97f
commit e3f561a624
3 changed files with 49 additions and 43 deletions
+21
View File
@@ -7,6 +7,7 @@
#include <unistd.h>
#include "alloc-util.h"
#include "env-util.h"
#include "errno-util.h"
#include "extract-word.h"
#include "fd-util.h"
@@ -1592,6 +1593,26 @@ int verify_timezone(const char *name, int log_level) {
return 0;
}
void reset_timezonep(char **p) {
assert(p);
(void) set_unset_env("TZ", *p, /* overwrite = */ true);
tzset();
*p = mfree(*p);
}
char* save_timezone(void) {
const char *e = getenv("TZ");
if (!e)
return NULL;
char *s = strdup(e);
if (!s)
log_debug("Failed to save $TZ=%s, unsetting the environment variable.", e);
return s;
}
bool clock_supported(clockid_t clock) {
struct timespec ts;
+6
View File
@@ -163,6 +163,12 @@ static inline bool timezone_is_valid(const char *name, int log_level) {
return verify_timezone(name, log_level) >= 0;
}
void reset_timezonep(char **p);
char* save_timezone(void);
#define SAVE_TIMEZONE \
_unused_ _cleanup_(reset_timezonep) \
char *_saved_timezone_ = save_timezone()
bool clock_supported(clockid_t clock);
usec_t usec_shift_clock(usec_t, clockid_t from, clockid_t to);