original netbsd's tr(1)
[minix.git] / commands / zoneinfo / difftime.c
blobc5efbcae74f7e6d1b8b2bf4c6525bfabf288b07b
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
4 */
6 #ifndef lint
7 #ifndef NOID
8 static char elsieid[] = "@(#)difftime.c 7.19";
9 #endif /* !defined NOID */
10 #endif /* !defined lint */
12 /*LINTLIBRARY*/
14 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
16 double
17 difftime(time1, time0)
18 const time_t time1;
19 const time_t 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.
32 return time1 - time0;
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.
40 if (time1 >= time0)
41 return time1 - time0;
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))
50 return time1 - time0;
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);