add files fom from zcode2012e.tar.gz
[platform/upstream/tzdata.git] / difftime.c
1 /*
2 ** This file is in the public domain, so clarified as of
3 ** 1996-06-05 by Arthur David Olson.
4 */
5
6 /*LINTLIBRARY*/
7
8 #include "private.h"    /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
9
10 double
11 difftime(time1, time0)
12 const time_t    time1;
13 const time_t    time0;
14 {
15         /*
16         ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
17         ** (assuming that the larger type has more precision).
18         ** This is the common real-world case circa 2004.
19         */
20         if (sizeof (double) > sizeof (time_t))
21                 return (double) time1 - (double) time0;
22         if (!TYPE_INTEGRAL(time_t)) {
23                 /*
24                 ** time_t is floating.
25                 */
26                 return time1 - time0;
27         }
28         if (!TYPE_SIGNED(time_t)) {
29                 /*
30                 ** time_t is integral and unsigned.
31                 ** The difference of two unsigned values can't overflow
32                 ** if the minuend is greater than or equal to the subtrahend.
33                 */
34                 if (time1 >= time0)
35                         return time1 - time0;
36                 else    return -((double) (time0 - time1));
37         }
38         /*
39         ** time_t is integral and signed.
40         ** Handle cases where both time1 and time0 have the same sign
41         ** (meaning that their difference cannot overflow).
42         */
43         if ((time1 < 0) == (time0 < 0))
44                 return time1 - time0;
45         /*
46         ** time1 and time0 have opposite signs.
47         ** Punt if unsigned long is too narrow.
48         */
49         if (sizeof (unsigned long) < sizeof (time_t))
50                 return (double) time1 - (double) time0;
51         /*
52         ** Stay calm...decent optimizers will eliminate the complexity below.
53         */
54         if (time1 >= 0 /* && time0 < 0 */)
55                 return (unsigned long) time1 +
56                         (unsigned long) (-(time0 + 1)) + 1;
57         return -(double) ((unsigned long) time0 +
58                 (unsigned long) (-(time1 + 1)) + 1);
59 }