]> git.ipfire.org Git - thirdparty/ntp.git/commitdiff
minor cleanup in timespecops/timevalops
authorJuergen Perlinger <perlinger@ntp.org>
Sat, 23 Apr 2011 09:38:29 +0000 (11:38 +0200)
committerJuergen Perlinger <perlinger@ntp.org>
Sat, 23 Apr 2011 09:38:29 +0000 (11:38 +0200)
bk: 4db29e15-sk2uDhndXUXj9x4NkMzig

ChangeLog
libntp/timespecops.c
libntp/timevalops.c

index ed7aeb0e7e4ec31214c342c00407d51f20af1a21..f46d14d5571fb9a86b09afb03c326d9173b91fa3 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+* cleanup in timespecops.c / timevalops.c
 (4.2.7p157) 2011/04/21 Released by Harlan Stenn <stenn@ntp.org>
 * [Bug 1890] 4.2.7p156 segfault in duplicate freeaddrinfo().
 (4.2.7p156) 2011/04/19 Released by Harlan Stenn <stenn@ntp.org>
index 0fd53d37cade20fe6d0c56dd33e5582a68cda000..b45fc3bd973833e614169a73093135daadd07efd 100644 (file)
@@ -8,17 +8,10 @@
 #include <config.h>
 #include <math.h>
 
-#include "lib_strbuf.h"
-#include "ntp_calendar.h"
-
 #include "timespecops.h"
 
-/* formatting to string needs at max 31 bytes (even with 64 bit time_t),
- * so we check LIB_BUFLENGTH is big enough for our pupose.
- */
-#if LIB_BUFLENGTH < 32
-#error LIB_BUFLENGTH not big enough
-#endif
+#include "timetoa.h"
+#include "ntp_calendar.h"
 
 /* make sure we have the right definition for NANOSECONDS */
 #undef NANOSECONDS
@@ -26,7 +19,7 @@
 
 /* conversion between l_fp fractions and nanoseconds */
 #if SIZEOF_LONG >= 8
-# define MYFTOTVN(tsf,tvu)                                             \
+# define MYFTOTVN(tsf, tvu)                                            \
        ((tvu) = (int32)                                                \
                (((u_long)(tsf) * NANOSECONDS + 0x80000000) >> 32))
 # define MYTVNTOF(tvu, tsf)                                            \
        ((tsf) = (u_int32)floor((tvu) * 4.294967296 + 0.5))
 #endif
 
-/* using snprintf is troublesome with time_t. Try to resolve it. */
-#if SIZEOF_TIME_T <= SIZEOF_INT
-typedef unsigned int u_time;
-#define TIMEFMT ""
-#elif SIZEOF_TIME_T <= SIZEOF_LONG
-typedef unsigned long u_time;
-#define TIMEFMT "l"
-#elif defined(SIZEOF_LONG_LONG) && SIZEOF_TIME_T <= SIZEOF_LONG_LONG
-typedef unsigned long long u_time;
-#define TIMEFMT "ll"
-#else
-#include "GRONK: what size has a time_t here?"
-#endif
-
 
 /* copy and normalise. Used often enough to warrant a macro. */
 #define COPYNORM(dst, src)                                             \
@@ -75,7 +54,7 @@ timespec_norm(
         * involved. On the other hand, division by constant should be
         * fast enough; so we do a division of the nanoseconds in that
         * case. The floor adjustment step follows with the standard
-        * normalisation loops.  And 'labs' is intentinally not used
+        * normalisation loops.  And 'labs' is intentinally not used
         * here: its has implementation defined behaviour when applied
         * to LONG_MIN. */
        if (x->tv_nsec < -3l * NANOSECONDS ||
@@ -188,7 +167,7 @@ timespec_neg(
 
 /*
  * x = abs(a)
- * returns nonzero if negation was needed
+ * return value is nonzero if negation was needed
  */
 int
 timespec_abs(
@@ -216,7 +195,7 @@ timespec_abs(
 
 /*
  * compare previously-normalised a and b
- * return 1 / 0 / -1 if         a < / == / > b
+ * return -1 / 0 / 1 if         a < / == / > b
  */
 int
 timespec_cmp_fast(
@@ -236,7 +215,7 @@ timespec_cmp_fast(
 
 /*
  * compare possibly denormal a and b
- * return 1 / 0 / -1 if         a < / == / > b
+ * return -1 / 0 / 1 if         a < / == / > b
  */
 int
 timespec_cmp(
@@ -260,7 +239,7 @@ timespec_cmp(
 
 /*
  * test previously-normalised a
- * return 1 / 0 / -1 if         a < / == / > 0
+ * return -1 / 0 / 1 if         a < / == / > 0
  */
 int
 timespec_test_fast(
@@ -278,15 +257,15 @@ timespec_test_fast(
 
 /*
  * test possibly denormal a
- * return 1 / 0 / -1 if         a < / == / > 0
+ * return -1 / 0 / 1 if         a < / == / > 0
  */
 int
 timespec_test(
        const struct timespec *a
        )
 {
-       int             r;
        struct timespec A;
+       int             r;
 
        COPYNORM(&A, a);
        r = (A.tv_sec > 0) - (A.tv_sec < 0);
@@ -296,49 +275,13 @@ timespec_test(
        return r;
 }
 
+/* return LIB buffer ptr to string rep */
 const char*
 timespec_tostr(
        const struct timespec *x
        )
 {
-       /*
-        * Formatting a 'time_t' to a string of decimal digits poses
-        * some interesting portability problems regarding the size of a
-        * time_t and the handling of signed integer overflow, which is
-        * undefined and must be avoided.
-        *
-        * Even with 64 bit time_t, 32 chars will suffice. Hopefully,
-        * LIB_BUFLENGTH is big enough; the current definiton checks
-        * this by the preprocessor just at the top of this file. */
-       static const char *fmt = "-%" TIMEFMT "u.%09lu";
-       
-       struct timespec v;
-       char           *cp;
-       int             notneg;
-       u_time          itmp;
-       u_long          ftmp;
-       
-       /* normalise and get absolute value into unsigned values. Since
-        * the negation of TIME_T_MIN (if it existed) is implementation
-        * defined, we try to avoid it. */
-       COPYNORM(&v, x);
-       notneg = v.tv_sec >= 0;
-       if (notneg != 0) {
-               itmp = (u_time)v.tv_sec;
-               ftmp = (u_long)v.tv_nsec;
-       } else if (v.tv_nsec != 0) {
-               itmp = (u_time)-(v.tv_sec + 1);
-               ftmp = (u_long)(NANOSECONDS - v.tv_nsec);
-       } else {
-               itmp = ((u_time) -(v.tv_sec + 1)) + 1;
-               ftmp = 0;
-       }
-
-       /* get buffer and format data */
-       LIB_GETBUF(cp);
-       snprintf(cp, LIB_BUFLENGTH, fmt + notneg, itmp, ftmp);
-       
-       return cp;
+       return format_time_fraction(x->tv_sec, x->tv_nsec, 9);
 }
 
 void
index 63a842f70fe0130735b9b1c7397ac705158feb55..d524c87b7cdf5b76980f7d32a3d3984eeadc288f 100644 (file)
@@ -186,8 +186,9 @@ timeval_abs(
        return r;
 }
 
-/* compare a <--> b
- * return 1 / 0 / -1 if         a < / == / > b
+/*
+ * compare previously-normalised a and b
+ * return -1 / 0 / 1 if         a < / == / > b
  */
 int
 timeval_cmp_fast(
@@ -205,6 +206,10 @@ timeval_cmp_fast(
        return r;
 }
 
+/*
+ * compare possibly denormal a and b
+ * return -1 / 0 / 1 if         a < / == / > b
+ */
 int
 timeval_cmp(
        const struct timeval *a,
@@ -227,7 +232,7 @@ timeval_cmp(
 
 /*
  * test previously-normalised a
- * return 1 / 0 / -1 if         a < / == / > 0
+ * return -1 / 0 / 1 if         a < / == / > 0
  */
 int
 timeval_test_fast(
@@ -245,7 +250,7 @@ timeval_test_fast(
 
 /*
  * test possibly denormal a
- * return 1 / 0 / -1 if         a < / == / > 0
+ * return -1 / 0 / 1 if         a < / == / > 0
  */
 int
 timeval_test(