1 /* 2 ** This file is in the public domain, so clarified as of 3 ** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov). 4 */ 5 6 #if defined(LIBC_SCCS) && !defined(lint) && !defined(NOID) 7 static char elsieid[] = "@(#)difftime.c 7.18"; 8 static char rcsid[] = "$OpenBSD: difftime.c,v 1.7 2005/07/05 13:40:51 millert Exp $"; 9 #endif /* LIBC_SCCS and not lint */ 10 11 /*LINTLIBRARY*/ 12 13 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */ 14 15 double 16 difftime(time1, time0) 17 const time_t time1; 18 const time_t time0; 19 { 20 /* 21 ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract 22 ** (assuming that the larger type has more precision). 23 ** This is the common real-world case circa 2004. 24 */ 25 if (sizeof (double) > sizeof (time_t)) 26 return (double) time1 - (double) time0; 27 if (!TYPE_INTEGRAL(time_t)) { 28 /* 29 ** time_t is floating. 30 */ 31 return time1 - time0; 32 } 33 if (!TYPE_SIGNED(time_t)) { 34 /* 35 ** time_t is integral and unsigned. 36 ** The difference of two unsigned values can't overflow 37 ** if the minuend is greater than or equal to the subtrahend. 38 */ 39 if (time1 >= time0) 40 return time1 - time0; 41 else return -((double) (time0 - time1)); 42 } 43 /* 44 ** time_t is integral and signed. 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 ** time1 and time0 have opposite signs. 52 ** Punt if unsigned long is too narrow. 53 */ 54 if (sizeof (unsigned long) < sizeof (time_t)) 55 return (double) time1 - (double) time0; 56 /* 57 ** Stay calm...decent optimizers will eliminate the complexity below. 58 */ 59 if (time1 >= 0 /* && time0 < 0 */) 60 return (unsigned long) time1 + 61 (unsigned long) (-(time0 + 1)) + 1; 62 return -(double) ((unsigned long) time0 + 63 (unsigned long) (-(time1 + 1)) + 1); 64 } 65