The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | ** This file is in the public domain, so clarified as of |
| 3 | ** 1996-06-05 by Arthur David Olson. |
| 4 | */ |
| 5 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 6 | /*LINTLIBRARY*/ |
| 7 | |
Elliott Hughes | e0d0b15 | 2013-09-27 00:04:30 -0700 | [diff] [blame] | 8 | #include "private.h" /* for time_t and TYPE_SIGNED */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 9 | |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 10 | /* Return -X as a double. Using this avoids casting to 'double'. */ |
| 11 | static double |
| 12 | dminus(double x) |
| 13 | { |
| 14 | return -x; |
| 15 | } |
| 16 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 17 | double ATTRIBUTE_CONST |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 18 | difftime(time_t time1, time_t time0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 19 | { |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 20 | /* |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 21 | ** If double is large enough, simply convert and subtract |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 22 | ** (assuming that the larger type has more precision). |
| 23 | */ |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 24 | if (sizeof (time_t) < sizeof (double)) { |
| 25 | double t1 = time1, t0 = time0; |
| 26 | return t1 - t0; |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 27 | } |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | ** The difference of two unsigned values can't overflow |
| 31 | ** if the minuend is greater than or equal to the subtrahend. |
| 32 | */ |
| 33 | if (!TYPE_SIGNED(time_t)) |
| 34 | return time0 <= time1 ? time1 - time0 : dminus(time0 - time1); |
| 35 | |
| 36 | /* Use uintmax_t if wide enough. */ |
| 37 | if (sizeof (time_t) <= sizeof (uintmax_t)) { |
| 38 | uintmax_t t1 = time1, t0 = time0; |
| 39 | return time0 <= time1 ? t1 - t0 : dminus(t0 - t1); |
| 40 | } |
| 41 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 42 | /* |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 43 | ** Handle cases where both time1 and time0 have the same sign |
| 44 | ** (meaning that their difference cannot overflow). |
| 45 | */ |
| 46 | if ((time1 < 0) == (time0 < 0)) |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 47 | return time1 - time0; |
| 48 | |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 49 | /* |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 50 | ** The values have opposite signs and uintmax_t is too narrow. |
Elliott Hughes | ce4783c | 2013-07-12 17:31:11 -0700 | [diff] [blame] | 51 | ** This suffers from double rounding; attempt to lessen that |
| 52 | ** by using long double temporaries. |
| 53 | */ |
Elliott Hughes | 9fb22a3 | 2015-10-07 17:13:40 -0700 | [diff] [blame] | 54 | { |
| 55 | long double t1 = time1, t0 = time0; |
| 56 | return t1 - t0; |
| 57 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | } |