util: add conversion between intervals and NTP 64-bit format

This will be needed to save PTP correction in NTP timestamp format.
This commit is contained in:
Miroslav Lichvar
2023-09-21 14:02:11 +02:00
parent e08a0ee668
commit bf616eafa1
3 changed files with 72 additions and 0 deletions

27
util.c
View File

@@ -818,6 +818,33 @@ UTI_DiffNtp64ToDouble(const NTP_int64 *a, const NTP_int64 *b)
/* ================================================== */
double
UTI_Ntp64ToDouble(NTP_int64 *src)
{
NTP_int64 zero;
UTI_ZeroNtp64(&zero);
return UTI_DiffNtp64ToDouble(src, &zero);
}
/* ================================================== */
void
UTI_DoubleToNtp64(double src, NTP_int64 *dest)
{
int32_t hi;
src = CLAMP(INT32_MIN, src, INT32_MAX);
hi = round(src);
if (hi > src)
hi -= 1;
dest->hi = htonl(hi);
dest->lo = htonl((src - hi) * (1.0e9 * NSEC_PER_NTP64));
}
/* ================================================== */
/* Maximum offset between two sane times */
#define MAX_OFFSET 4294967296.0