From f35c81c871764e480d94f730004817c1aea85ea9 Mon Sep 17 00:00:00 2001 From: Miroslav Lichvar Date: Wed, 11 Oct 2017 16:57:10 +0200 Subject: [PATCH] refclock: improve TAI-UTC conversion Instead of using the TAI-UTC offset which corresponds to the current system time, get the offset for the reference time. This allows the clock to be accurately stepped from a time with different TAI-UTC offset. --- refclock.c | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/refclock.c b/refclock.c index b2bf5c5..0791d47 100644 --- a/refclock.c +++ b/refclock.c @@ -357,6 +357,28 @@ RCL_GetDriverOption(RCL_Instance instance, char *name) return NULL; } +static int +convert_tai_offset(struct timespec *sample_time, double *offset) +{ + struct timespec tai_ts, utc_ts; + int tai_offset; + + /* Get approximate TAI-UTC offset for the reference time in TAI */ + UTI_AddDoubleToTimespec(sample_time, *offset, &tai_ts); + tai_offset = REF_GetTaiOffset(&tai_ts); + + /* Get TAI-UTC offset for the reference time in UTC +/- 1 second */ + UTI_AddDoubleToTimespec(&tai_ts, -tai_offset, &utc_ts); + tai_offset = REF_GetTaiOffset(&utc_ts); + + if (!tai_offset) + return 0; + + *offset -= tai_offset; + + return 1; +} + int RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset, int leap) { @@ -386,15 +408,9 @@ RCL_AddSample(RCL_Instance instance, struct timespec *sample_time, double offset return 0; } - if (instance->tai) { - int tai_offset = REF_GetTaiOffset(sample_time); - - if (!tai_offset) { - DEBUG_LOG("refclock sample ignored unknown TAI offset"); - return 0; - } - - offset -= tai_offset; + if (instance->tai && !convert_tai_offset(sample_time, &offset)) { + DEBUG_LOG("refclock sample ignored unknown TAI offset"); + return 0; } filter_add_sample(&instance->filter, &cooked_time, offset - correction + instance->offset, dispersion);