util: add function to subtract NTP timestamps

This will be needed to work with monotonic timestamps, which don't have
a stable epoch and cannot be converted to timespec.
This commit is contained in:
Miroslav Lichvar
2021-11-10 14:28:53 +01:00
parent a97ca73704
commit a652ce7d0e
3 changed files with 21 additions and 1 deletions

10
util.c
View File

@@ -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