[Git][NTPsec/ntpsec][master] 4 commits: add d_to_tspec(). Does what it says, converts a double to a timespec.

Gary E. Miller gitlab at mg.gitlab.com
Fri Mar 10 01:35:47 UTC 2017


Gary E. Miller pushed to branch master at NTPsec / ntpsec


Commits:
196f378e by Gary E. Miller at 2017-03-09T16:57:14-08:00
add d_to_tspec().  Does what it says, converts a double to a timespec.

It also rounds to the nano second and normalizes the timespec.

- - - - -
e3692e94 by Gary E. Miller at 2017-03-09T17:17:22-08:00
Stop round tripping from timepsec to l_fp NTP epoch and back again.

Converting from timespec to l_fp with NTP Epoch and back to timespec led
to one overflow and one underflow.  This only worked due to the smoke
and mirrors of the 'pivot'.

- - - - -
32851020 by Gary E. Miller at 2017-03-09T17:23:36-08:00
CFLAGS: add -Wunused

Surprisingly no new warnings popped up. so let's keep it that way.

- - - - -
c7043d17 by Gary E. Miller at 2017-03-09T17:33:59-08:00
Remove the smoke and mirrors that was the pivot.

Surprisingly, the code now looks 2038 clean, if your timespec
has a 64 bit time_t.

- - - - -


3 changed files:

- include/timespecops.h
- libntp/systime.c
- wafhelpers/configure.py


Changes:

=====================================
include/timespecops.h
=====================================
--- a/include/timespecops.h
+++ b/include/timespecops.h
@@ -114,6 +114,22 @@ normalize_tspec(
 	return x;
 }
 
+/* convert a double to a rounded and normalized timespec */
+static inline struct timespec
+d_to_tspec(
+	double d
+	)
+{
+	struct timespec	x;
+
+	d += 0.5e-9;	/* round on LSB */
+	x.tv_sec = (time_t) d;
+	x.tv_nsec = (long)((d - (double)x.tv_sec) * 1e9);
+
+        /* unlikely, but ensure it is normalized */
+	return normalize_tspec(x);
+}
+
 /* x = a + b */
 static inline struct timespec
 add_tspec(


=====================================
libntp/systime.c
=====================================
--- a/libntp/systime.c
+++ b/libntp/systime.c
@@ -307,6 +307,9 @@ adj_systime(
 
 /*
  * step_systime - step the system clock.
+ *
+ * if your timespec has a 64 bit time_t then you are 2038 ready.
+ * if your timespec has a 32 bit time_t, be sure to duck in 2038
  */
 
 bool
@@ -315,71 +318,19 @@ step_systime(
 	int (*settime)(struct timespec *)
 	)
 {
-	time_t pivot; /* for ntp era unfolding */
 	struct timespec timets, tslast, tsdiff;
-	struct calendar jd;
-	l_fp fp_ofs, fp_sys; /* offset and target system time in FP */
+	struct timespec ofs_ts; /* desired offset as teimspec */
 
-	/*
-	 * Get pivot time for NTP era unfolding. Since we don't step
-	 * very often, we can afford to do the whole calculation from
-	 * scratch. And we're not in the time-critical path yet.
-	 */
-#if NTP_SIZEOF_TIME_T > 4
-	/*
-	 * This code makes sure the resulting time stamp for the new
-	 * system time is in the 2^32 seconds starting at 1970-01-01,
-	 * 00:00:00 UTC.
-	 */
-	pivot = 0x80000000;
-#if USE_COMPILETIME_PIVOT
-	/*
-	 * Add the compile time minus 10 years to get a possible target
-	 * area of (compile time - 10 years) to (compile time + 126
-	 * years).  This should be sufficient for a given binary of
-	 * NTPD.
-	 */
-	if (ntpcal_get_build_date(&jd)) {
-		jd.year -= 10;
-		pivot += ntpcal_date_to_time(&jd);
-	} else {
-		msyslog(LOG_ERR,
-			"step_systime: assume 1970-01-01 as build date");
-	}
-#else
-	UNUSED_LOCAL(jd);
-#endif /* USE_COMPILETIME_PIVOT */
-#else
-	UNUSED_LOCAL(jd);
-	/* This makes sure the resulting time stamp is on or after
-	 * 1969-12-31/23:59:59 UTC and gives us additional two years,
-	 * from the change of NTP era in 2036 to the UNIX rollover in
-	 * 2038. (Minus one second, but that won't hurt.) We *really*
-	 * need a 64-bit 'time_t' after that!  Or a different
-	 * baseline, but that would cause other serious trouble, too.
-	 *
-	 * 2017 update: The comment above was written before the
-	 * 2008-2009 64-bit transition and assumed a 32-bit time_t
-	 * It now appears much less likely that there still be 
-	 * platforms with 32-bit time running in 2038.
-	 */
-	pivot = 0x7FFFFFFF;
-#endif
-
-	/* get the complete jump distance as l_fp */
-	fp_ofs = dtolfp(step) + dtolfp(sys_residual);
+	/* get the complete jump distance as timespec */
+        ofs_ts = d_to_tspec((step + sys_residual + 0.5e-9) * 1e9);
 
 	/* ---> time-critical path starts ---> */
 
-	/* get the current time as l_fp (without fuzz) and as struct timespec */
+	/* get the current time as, without fuzz, as struct timespec */
 	get_ostime(&timets);
-	fp_sys = tspec_stamp_to_lfp(timets);
-
-	/* get the target time as l_fp */
-	fp_sys += fp_ofs;
 
-	/* unfold the new system time */
-	timets = lfp_stamp_to_tspec(fp_sys, &pivot);
+	/* add offset */
+	timets = add_tspec(timets, ofs_ts);
 
 	/* now set new system time */
 	if (settime(&timets) != 0) {


=====================================
wafhelpers/configure.py
=====================================
--- a/wafhelpers/configure.py
+++ b/wafhelpers/configure.py
@@ -247,6 +247,7 @@ def cmd_configure(ctx, config):
         "-Wall",
         "-Wextra",
         "-Wstrict-prototypes",
+        "-Wunused",
         ]
 
     # Check which linker flags are supported



View it on GitLab: https://gitlab.com/NTPsec/ntpsec/compare/790ed291833e993bf9165b8c50c4f5af6d4595ad...c7043d17c618971fb6edf8a564c1fe665e9e6a54
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.ntpsec.org/pipermail/vc/attachments/20170310/3edb34c4/attachment.html>


More information about the vc mailing list