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