2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
8 static char elsieid
[] = "@(#)difftime.c 7.19";
9 #endif /* !defined NOID */
10 #endif /* !defined lint */
14 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
17 difftime(time1
, time0
)
22 ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
23 ** (assuming that the larger type has more precision).
24 ** This is the common real-world case circa 2004.
26 if (sizeof (double) > sizeof (time_t))
27 return (double) time1
- (double) time0
;
28 if (!TYPE_INTEGRAL(time_t)) {
30 ** time_t is floating.
34 if (!TYPE_SIGNED(time_t)) {
36 ** time_t is integral and unsigned.
37 ** The difference of two unsigned values can't overflow
38 ** if the minuend is greater than or equal to the subtrahend.
42 else return -((double) (time0
- time1
));
45 ** time_t is integral and signed.
46 ** Handle cases where both time1 and time0 have the same sign
47 ** (meaning that their difference cannot overflow).
49 if ((time1
< 0) == (time0
< 0))
52 ** time1 and time0 have opposite signs.
53 ** Punt if unsigned long is too narrow.
55 if (sizeof (unsigned long) < sizeof (time_t))
56 return (double) time1
- (double) time0
;
58 ** Stay calm...decent optimizers will eliminate the complexity below.
60 if (time1
>= 0 /* && time0 < 0 */)
61 return (unsigned long) time1
+
62 (unsigned long) (-(time0
+ 1)) + 1;
63 return -(double) ((unsigned long) time0
+
64 (unsigned long) (-(time1
+ 1)) + 1);