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