From: Karel Zak Date: Tue, 6 Aug 2024 12:07:43 +0000 (+0200) Subject: include/timeutils: add time_diff() X-Git-Tag: v2.42-start~97^2~27 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=30e61b122f1ee89576d0f852ea93adc9e381d8fa;p=thirdparty%2Futil-linux.git include/timeutils: add time_diff() Eliminate redundant code and define time_diff() only once. Signed-off-by: Karel Zak --- diff --git a/include/timeutils.h b/include/timeutils.h index 27261abd8..e3cd252ba 100644 --- a/include/timeutils.h +++ b/include/timeutils.h @@ -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 */ diff --git a/libsmartcols/samples/continuous.c b/libsmartcols/samples/continuous.c index 72ecdf224..9821329fe 100644 --- a/libsmartcols/samples/continuous.c +++ b/libsmartcols/samples/continuous.c @@ -14,6 +14,7 @@ #include "c.h" #include "nls.h" #include "strutils.h" +#include "timeutils.h" #include "xalloc.h" #include "libsmartcols.h" @@ -22,11 +23,6 @@ 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) { diff --git a/sys-utils/dmesg.c b/sys-utils/dmesg.c index a8369799f..0b1a1681b 100644 --- a/sys-utils/dmesg.c +++ b/sys-utils/dmesg.c @@ -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); diff --git a/sys-utils/hwclock-rtc.c b/sys-utils/hwclock-rtc.c index 7acf09d43..e1ee6a05d 100644 --- a/sys-utils/hwclock-rtc.c +++ b/sys-utils/hwclock-rtc.c @@ -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; } diff --git a/sys-utils/hwclock.c b/sys-utils/hwclock.c index 2714775f9..9977e0600 100644 --- a/sys-utils/hwclock.c +++ b/sys-utils/hwclock.c @@ -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 seconds after the * time . Of course, 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); diff --git a/sys-utils/hwclock.h b/sys-utils/hwclock.h index 4cbbff957..7073bc31c 100644 --- a/sys-utils/hwclock.h +++ b/sys-utils/hwclock.h @@ -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);