"Fossies" - the Fresh Open Source Software Archive

Member "difftime.c" (16 Jul 2018, 1436 Bytes) of package /linux/misc/tzcode2018i.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "difftime.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 2018e_vs_2018f.

    1 /* Return the difference between two timestamps.  */
    2 
    3 /*
    4 ** This file is in the public domain, so clarified as of
    5 ** 1996-06-05 by Arthur David Olson.
    6 */
    7 
    8 /*LINTLIBRARY*/
    9 
   10 #include "private.h"    /* for time_t and TYPE_SIGNED */
   11 
   12 /* Return -X as a double.  Using this avoids casting to 'double'.  */
   13 static double
   14 dminus(double x)
   15 {
   16   return -x;
   17 }
   18 
   19 double
   20 difftime(time_t time1, time_t time0)
   21 {
   22     /*
   23     ** If double is large enough, simply convert and subtract
   24     ** (assuming that the larger type has more precision).
   25     */
   26     if (sizeof (time_t) < sizeof (double)) {
   27       double t1 = time1, t0 = time0;
   28       return t1 - t0;
   29     }
   30 
   31     /*
   32     ** The difference of two unsigned values can't overflow
   33     ** if the minuend is greater than or equal to the subtrahend.
   34     */
   35     if (!TYPE_SIGNED(time_t))
   36       return time0 <= time1 ? time1 - time0 : dminus(time0 - time1);
   37 
   38     /* Use uintmax_t if wide enough.  */
   39     if (sizeof (time_t) <= sizeof (uintmax_t)) {
   40       uintmax_t t1 = time1, t0 = time0;
   41       return time0 <= time1 ? t1 - t0 : dminus(t0 - t1);
   42     }
   43 
   44     /*
   45     ** Handle cases where both time1 and time0 have the same sign
   46     ** (meaning that their difference cannot overflow).
   47     */
   48     if ((time1 < 0) == (time0 < 0))
   49       return time1 - time0;
   50 
   51     /*
   52     ** The values have opposite signs and uintmax_t is too narrow.
   53     ** This suffers from double rounding; attempt to lessen that
   54     ** by using long double temporaries.
   55     */
   56     {
   57       long double t1 = time1, t0 = time0;
   58       return t1 - t0;
   59     }
   60 }