1984263bcSMatthew Dillon /* 2984263bcSMatthew Dillon ** This file is in the public domain, so clarified as of 3*0e2a9149SSascha Wildner ** 1996-06-05 by Arthur David Olson. 4984263bcSMatthew Dillon ** 5*0e2a9149SSascha Wildner ** @(#)difftime.c 7.7 6984263bcSMatthew Dillon ** $FreeBSD: src/lib/libc/stdtime/difftime.c,v 1.4.8.1 2001/03/05 11:37:21 obrien Exp $ 7*0e2a9149SSascha Wildner ** $DragonFly: src/lib/libc/stdtime/difftime.c,v 1.5 2008/10/19 20:15:58 swildner Exp $ 81de703daSMatthew Dillon */ 9984263bcSMatthew Dillon /*LINTLIBRARY*/ 10984263bcSMatthew Dillon 1117ea2221SMatthew Dillon #include "namespace.h" 12984263bcSMatthew Dillon #include "private.h" 1317ea2221SMatthew Dillon #include "un-namespace.h" 14984263bcSMatthew Dillon 15984263bcSMatthew Dillon double 161fccf464SSascha Wildner difftime(const time_t time1, const time_t time0) 17984263bcSMatthew Dillon { 18*0e2a9149SSascha Wildner /* 19*0e2a9149SSascha Wildner ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract 20*0e2a9149SSascha Wildner ** (assuming that the larger type has more precision). 21*0e2a9149SSascha Wildner ** This is the common real-world case circa 2004. 22*0e2a9149SSascha Wildner */ 23*0e2a9149SSascha Wildner if (sizeof (double) > sizeof (time_t)) 24984263bcSMatthew Dillon return (double) time1 - (double) time0; 25*0e2a9149SSascha Wildner if (!TYPE_INTEGRAL(time_t)) { 26984263bcSMatthew Dillon /* 27*0e2a9149SSascha Wildner ** time_t is floating. 28984263bcSMatthew Dillon */ 29*0e2a9149SSascha Wildner return time1 - time0; 30*0e2a9149SSascha Wildner } 31*0e2a9149SSascha Wildner if (!TYPE_SIGNED(time_t)) { 32984263bcSMatthew Dillon /* 33*0e2a9149SSascha Wildner ** time_t is integral and unsigned. 34*0e2a9149SSascha Wildner ** The difference of two unsigned values can't overflow 35*0e2a9149SSascha Wildner ** if the minuend is greater than or equal to the subtrahend. 36984263bcSMatthew Dillon */ 37*0e2a9149SSascha Wildner if (time1 >= time0) 38*0e2a9149SSascha Wildner return time1 - time0; 39*0e2a9149SSascha Wildner else return -((double) (time0 - time1)); 40*0e2a9149SSascha Wildner } 41984263bcSMatthew Dillon /* 42*0e2a9149SSascha Wildner ** time_t is integral and signed. 43*0e2a9149SSascha Wildner ** Handle cases where both time1 and time0 have the same sign 44*0e2a9149SSascha Wildner ** (meaning that their difference cannot overflow). 45984263bcSMatthew Dillon */ 46*0e2a9149SSascha Wildner if ((time1 < 0) == (time0 < 0)) 47*0e2a9149SSascha Wildner return time1 - time0; 48*0e2a9149SSascha Wildner /* 49*0e2a9149SSascha Wildner ** time1 and time0 have opposite signs. 50*0e2a9149SSascha Wildner ** Punt if unsigned long is too narrow. 51*0e2a9149SSascha Wildner */ 52*0e2a9149SSascha Wildner if (sizeof (unsigned long) < sizeof (time_t)) 53*0e2a9149SSascha Wildner return (double) time1 - (double) time0; 54*0e2a9149SSascha Wildner /* 55*0e2a9149SSascha Wildner ** Stay calm...decent optimizers will eliminate the complexity below. 56*0e2a9149SSascha Wildner */ 57*0e2a9149SSascha Wildner if (time1 >= 0 /* && time0 < 0 */) 58*0e2a9149SSascha Wildner return (unsigned long) time1 + 59*0e2a9149SSascha Wildner (unsigned long) (-(time0 + 1)) + 1; 60*0e2a9149SSascha Wildner return -(double) ((unsigned long) time0 + 61*0e2a9149SSascha Wildner (unsigned long) (-(time1 + 1)) + 1); 62984263bcSMatthew Dillon } 63