]> git.ipfire.org Git - thirdparty/chrony.git/commitdiff
util: add function to subtract NTP timestamps
authorMiroslav Lichvar <mlichvar@redhat.com>
Wed, 10 Nov 2021 13:28:53 +0000 (14:28 +0100)
committerMiroslav Lichvar <mlichvar@redhat.com>
Tue, 16 Nov 2021 09:23:20 +0000 (10:23 +0100)
This will be needed to work with monotonic timestamps, which don't have
a stable epoch and cannot be converted to timespec.

test/unit/util.c
util.c
util.h

index 88a623d4504662c54f91f98c437816de7143c0fb..ef4a596dc83342dad93ee5dc36bf447f31f4777e 100644 (file)
@@ -34,7 +34,7 @@ test_unit(void)
 {
   struct timespec ts, ts2, ts3, ts4;
   char buf[16], *s, *s2, *words[3];
-  NTP_int64 ntp_ts, ntp_fuzz;
+  NTP_int64 ntp_ts, ntp_ts2, ntp_fuzz;
   NTP_int32 ntp32_ts;
   struct timeval tv;
   double x, y, nan, inf;
@@ -114,6 +114,13 @@ test_unit(void)
 #endif
   TEST_CHECK(ts.tv_nsec == 999999999);
 
+  ntp_ts.hi = htonl(JAN_1970 - 1);
+  ntp_ts.lo = htonl(0xffffffff);
+  ntp_ts2.hi = htonl(JAN_1970 + 1);
+  ntp_ts2.lo = htonl(0x80000000);
+  TEST_CHECK(fabs(UTI_DiffNtp64ToDouble(&ntp_ts, &ntp_ts2) + 1.5) < 1e-9);
+  TEST_CHECK(fabs(UTI_DiffNtp64ToDouble(&ntp_ts2, &ntp_ts) - 1.5) < 1e-9);
+
   UTI_AddDoubleToTimespec(&ts, 1e-9, &ts);
 #if defined(HAVE_LONG_TIME_T) && NTP_ERA_SPLIT > 0
   TEST_CHECK(ts.tv_sec == 1 + 0x100000000LL * (1 + (NTP_ERA_SPLIT - 1) / 0x100000000LL));
diff --git a/util.c b/util.c
index a853e1659efba5a5a8255a396d8551e40d46400d..82f8de7bf10b57eaf4f1515b8a192d399af3200d 100644 (file)
--- a/util.c
+++ b/util.c
@@ -751,6 +751,16 @@ UTI_Ntp64ToTimespec(const NTP_int64 *src, struct timespec *dest)
 
 /* ================================================== */
 
+double
+UTI_DiffNtp64ToDouble(const NTP_int64 *a, const NTP_int64 *b)
+{
+  /* Don't convert to timespec to allow any epoch */
+  return (int32_t)(ntohl(a->hi) - ntohl(b->hi)) +
+         ((double)ntohl(a->lo) - (double)ntohl(b->lo)) / (1.0e9 * NSEC_PER_NTP64);
+}
+
+/* ================================================== */
+
 /* Maximum offset between two sane times */
 #define MAX_OFFSET 4294967296.0
 
diff --git a/util.h b/util.h
index b658061af866ccd67f58a944345d9b52cd06bc61..434a9b44111029b8d61b21c40d4709b358288808 100644 (file)
--- a/util.h
+++ b/util.h
@@ -155,6 +155,9 @@ extern void UTI_TimespecToNtp64(const struct timespec *src, NTP_int64 *dest,
 /* Convert an NTP timestamp into a timespec */
 extern void UTI_Ntp64ToTimespec(const NTP_int64 *src, struct timespec *dest);
 
+/* Calculate a - b in any epoch */
+extern double UTI_DiffNtp64ToDouble(const NTP_int64 *a, const NTP_int64 *b);
+
 /* Check if time + offset is sane */
 extern int UTI_IsTimeOffsetSane(const struct timespec *ts, double offset);