summaryrefslogtreecommitdiffstats
path: root/src/shared/clock-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2021-02-10 12:09:38 +0100
committerLennart Poettering <lennart@poettering.net>2021-02-10 14:14:37 +0100
commit505061bb4fdecd06bdfbb1e0341316ab6ca1918b (patch)
treea322694b1f5c80b5b1707d41741b91f1564e9473 /src/shared/clock-util.c
parentFix coverity warning in test-string-util.c (diff)
downloadsystemd-505061bb4fdecd06bdfbb1e0341316ab6ca1918b.tar.xz
systemd-505061bb4fdecd06bdfbb1e0341316ab6ca1918b.zip
clock-util: modernize settimeofday() timezone calls
Let's use structured initialization, and avoid the weird `tv_null` indirection. No changes in behaviour, just some clean-ups.
Diffstat (limited to '')
-rw-r--r--src/shared/clock-util.c47
1 files changed, 21 insertions, 26 deletions
diff --git a/src/shared/clock-util.c b/src/shared/clock-util.c
index 2caa70fa52..ec67b054b4 100644
--- a/src/shared/clock-util.c
+++ b/src/shared/clock-util.c
@@ -96,8 +96,7 @@ int clock_is_localtime(const char* adjtime_path) {
return false;
}
-int clock_set_timezone(int *min) {
- const struct timeval *tv_null = NULL;
+int clock_set_timezone(int *ret_minutesdelta) {
struct timespec ts;
struct tm tm;
int minutesdelta;
@@ -107,36 +106,32 @@ int clock_set_timezone(int *min) {
assert_se(localtime_r(&ts.tv_sec, &tm));
minutesdelta = tm.tm_gmtoff / 60;
- tz.tz_minuteswest = -minutesdelta;
- tz.tz_dsttime = 0; /* DST_NONE */
+ tz = (struct timezone) {
+ .tz_minuteswest = -minutesdelta,
+ .tz_dsttime = 0, /* DST_NONE */
+ };
- /*
- * If the RTC does not run in UTC but in local time, the very first
- * call to settimeofday() will set the kernel's timezone and will warp the
- * system clock, so that it runs in UTC instead of the local time we
- * have read from the RTC.
- */
- if (settimeofday(tv_null, &tz) < 0)
- return negative_errno();
+ /* If the RTC does not run in UTC but in local time, the very first call to settimeofday() will set
+ * the kernel's timezone and will warp the system clock, so that it runs in UTC instead of the local
+ * time we have read from the RTC. */
+ if (settimeofday(NULL, &tz) < 0)
+ return -errno;
+
+ if (ret_minutesdelta)
+ *ret_minutesdelta = minutesdelta;
- if (min)
- *min = minutesdelta;
return 0;
}
int clock_reset_timewarp(void) {
- const struct timeval *tv_null = NULL;
- struct timezone tz;
-
- tz.tz_minuteswest = 0;
- tz.tz_dsttime = 0; /* DST_NONE */
-
- /*
- * The very first call to settimeofday() does time warp magic. Do a
- * dummy call here, so the time warping is sealed and all later calls
- * behave as expected.
- */
- if (settimeofday(tv_null, &tz) < 0)
+ static const struct timezone tz = {
+ .tz_minuteswest = 0,
+ .tz_dsttime = 0, /* DST_NONE */
+ };
+
+ /* The very first call to settimeofday() does time warp magic. Do a dummy call here, so the time
+ * warping is sealed and all later calls behave as expected. */
+ if (settimeofday(NULL, &tz) < 0)
return -errno;
return 0;