]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/timeutils: add time_diff()
authorKarel Zak <kzak@redhat.com>
Tue, 6 Aug 2024 12:07:43 +0000 (14:07 +0200)
committerKarel Zak <kzak@redhat.com>
Wed, 8 Jan 2025 12:57:42 +0000 (13:57 +0100)
Eliminate redundant code and define time_diff() only once.

Signed-off-by: Karel Zak <kzak@redhat.com>
include/timeutils.h
libsmartcols/samples/continuous.c
sys-utils/dmesg.c
sys-utils/hwclock-rtc.c
sys-utils/hwclock.c
sys-utils/hwclock.h

index 27261abd87dbcdb177119cc833818cb7b4877b60..e3cd252ba56e7634b6353ac1fa2ed608145eead1 100644 (file)
@@ -112,4 +112,8 @@ static inline bool is_timespecset(const struct timespec *t)
        return t->tv_sec || t->tv_nsec;
 }
 
+static inline double time_diff(const struct timeval *a, const struct timeval *b)
+{
+       return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / (double) USEC_PER_SEC;
+}
 #endif /* UTIL_LINUX_TIME_UTIL_H */
index 72ecdf22453f931eafb56d7606bae376bd8af70c..9821329febb9142ebbec740234dee44a04f880a8 100644 (file)
@@ -14,6 +14,7 @@
 #include "c.h"
 #include "nls.h"
 #include "strutils.h"
+#include "timeutils.h"
 #include "xalloc.h"
 
 #include "libsmartcols.h"
 
 enum { COL_NUM, COL_DATA, COL_TIME };
 
-static double time_diff(struct timeval *a, struct timeval *b)
-{
-       return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / 1E6;
-}
-
 /* add columns to the @tb */
 static void setup_columns(struct libscols_table *tb)
 {
index a8369799f388314de99c0c333a8100b6d7fc490e..0b1a1681b005fadc4c308b61f964a1512adb61f4 100644 (file)
@@ -602,11 +602,6 @@ static const char *parse_kmsg_timestamp(const char *str0, struct timeval *tv)
        return end + 1; /* skip separator */
 }
 
-static double time_diff(struct timeval *a, struct timeval *b)
-{
-       return (a->tv_sec - b->tv_sec) + (a->tv_usec - b->tv_usec) / (double) USEC_PER_SEC;
-}
-
 static int get_syslog_buffer_size(void)
 {
        int n = klogctl(SYSLOG_ACTION_SIZE_BUFFER, NULL, 0);
index 7acf09d4359be8dfb724fae4b8cd40c5068df01f..e1ee6a05d13d78cfcd34c56a771f77e12d064378 100644 (file)
@@ -212,7 +212,7 @@ static int busywait_for_rtc_clock_tick(const struct hwclock_control *ctl,
                if (rc || start_time.tm_sec != nowtime.tm_sec)
                        break;
                gettime_monotonic(&now);
-               if (time_diff(now, begin) > 1.5) {
+               if (time_diff(&now, &begin) > 1.5) {
                        warnx(_("Timed out waiting for time change."));
                        return 1;
                }
index 2714775f99ec7887b4108256f987be173b22a3e7..9977e0600173b0f1f94bc616af311255d4038767 100644 (file)
@@ -170,15 +170,6 @@ static struct timeval t2tv(time_t timet)
        return rettimeval;
 }
 
-/*
- * The difference in seconds between two times in "timeval" format.
- */
-double time_diff(struct timeval subtrahend, struct timeval subtractor)
-{
-       return (subtrahend.tv_sec - subtractor.tv_sec)
-           + (subtrahend.tv_usec - subtractor.tv_usec) / 1E6;
-}
-
 /*
  * The time, in "timeval" format, which is <increment> seconds after the
  * time <addend>. Of course, <increment> may be negative.
@@ -572,8 +563,8 @@ set_hardware_clock_exact(const struct hwclock_control *ctl,
                ON_DBG(RANDOM_SLEEP, up_to_1000ms_sleep());
 
                gettimeofday(&nowsystime, NULL);
-               deltavstarget = time_diff(nowsystime, targetsystime);
-               ticksize = time_diff(nowsystime, prevsystime);
+               deltavstarget = time_diff(&nowsystime, &targetsystime);
+               ticksize = time_diff(&nowsystime, &prevsystime);
                prevsystime = nowsystime;
 
                if (ticksize < 0) {
@@ -624,7 +615,7 @@ set_hardware_clock_exact(const struct hwclock_control *ctl,
        }
 
        newhwtime = sethwtime
-                   + round(time_diff(nowsystime, refsystime)
+                   + round(time_diff(&nowsystime, &refsystime)
                            - delay /* don't count this */);
        if (ctl->verbose)
                printf(_("%"PRId64".%06"PRId64" is close enough to %"PRId64".%06"PRId64" (%.6f < %.6f)\n"
@@ -821,8 +812,8 @@ adjust_drift_factor(const struct hwclock_control *ctl,
                 * hclocktime is fully corrected with the current drift factor.
                 * Its difference from nowtime is the missed drift correction.
                 */
-               factor_adjust = time_diff(nowtime, hclocktime) /
-                               (time_diff(nowtime, last_calib) / sec_per_day);
+               factor_adjust = time_diff(&nowtime, &hclocktime) /
+                               (time_diff(&nowtime, &last_calib) / sec_per_day);
 
                drift_factor = adjtime_p->drift_factor + factor_adjust;
                if (fabs(drift_factor) > MAX_DRIFT) {
@@ -838,8 +829,8 @@ adjust_drift_factor(const struct hwclock_control *ctl,
                                         "%f seconds\nin spite of a drift factor of "
                                         "%f seconds/day.\n"
                                         "Adjusting drift factor by %f seconds/day\n"),
-                                      time_diff(nowtime, hclocktime),
-                                      time_diff(nowtime, last_calib),
+                                      time_diff(&nowtime, &hclocktime),
+                                      time_diff(&nowtime, &last_calib),
                                       adjtime_p->drift_factor, factor_adjust);
                }
 
@@ -1101,7 +1092,7 @@ manipulate_clock(const struct hwclock_control *ctl, const time_t set_time,
                        hclocktime = time_inc(tdrift, hclocktime.tv_sec);
 
                startup_hclocktime =
-                time_inc(hclocktime, time_diff(startup_time, read_time));
+                time_inc(hclocktime, time_diff(&startup_time, &read_time));
        }
        if (ctl->show || ctl->get) {
                return display_time(startup_hclocktime);
index 4cbbff957fc20eba43fa719806013916fcfb33ba..7073bc31c2edfc1bf31c24ad6514edf6344c2f8e 100644 (file)
@@ -81,9 +81,6 @@ struct clock_ops {
 extern const struct clock_ops *probe_for_cmos_clock(void);
 extern const struct clock_ops *probe_for_rtc_clock(const struct hwclock_control *ctl);
 
-/* hwclock.c */
-extern double time_diff(struct timeval subtrahend, struct timeval subtractor);
-
 /* rtc.c */
 #if defined(__linux__) && defined(__alpha__)
 extern int get_epoch_rtc(const struct hwclock_control *ctl, unsigned long *epoch);