import tput from NetBSD
[minix3.git] / lib / libc / time / difftime.c
blob0849bc856764bf6ebfacd247e85bf245a29a846c
1 /* $NetBSD: difftime.c,v 1.11 2012/03/20 16:39:08 matt Exp $ */
3 /*
4 ** This file is in the public domain, so clarified as of
5 ** 1996-06-05 by Arthur David Olson.
6 */
8 #include <sys/cdefs.h>
9 #if defined(LIBC_SCCS) && !defined(lint)
10 #if 0
11 static char elsieid[] = "@(#)difftime.c 8.1";
12 #else
13 __RCSID("$NetBSD: difftime.c,v 1.11 2012/03/20 16:39:08 matt Exp $");
14 #endif
15 #endif /* LIBC_SCCS and not lint */
17 /*LINTLIBRARY*/
19 #include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
21 double
22 difftime(const time_t time1, const time_t time0)
25 ** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
26 ** (assuming that the larger type has more precision).
27 ** This is the common real-world case circa 2004.
29 /* LINTED constant */
30 if (sizeof (double) > sizeof (time_t))
31 return (double) time1 - (double) time0;
32 /* LINTED constant */
33 if (!TYPE_INTEGRAL(time_t)) {
35 ** time_t is floating.
37 return time1 - time0;
39 /* LINTED constant */
40 if (!TYPE_SIGNED(time_t)) {
42 ** time_t is integral and unsigned.
43 ** The difference of two unsigned values can't overflow
44 ** if the minuend is greater than or equal to the subtrahend.
46 if (time1 >= time0)
47 return time1 - time0;
48 else return -((double) (time0 - time1));
51 ** time_t is integral and signed.
52 ** Handle cases where both time1 and time0 have the same sign
53 ** (meaning that their difference cannot overflow).
55 if ((time1 < 0) == (time0 < 0))
56 return time1 - time0;
58 ** time1 and time0 have opposite signs.
59 ** Punt if unsigned long is too narrow.
61 /* CONSTCOND */
62 if (sizeof (unsigned long) < sizeof (time_t))
63 return (double) time1 - (double) time0;
65 ** Stay calm...decent optimizers will eliminate the complexity below.
67 if (time1 >= 0 /* && time0 < 0 */)
68 return (unsigned long) time1 +
69 (unsigned long) (-(time0 + 1)) + 1;
70 return -(double) ((unsigned long) time0 +
71 (unsigned long) (-(time1 + 1)) + 1);
74 #if defined(__minix) && defined(__weak_alias)
75 __weak_alias(difftime, __difftime50)
76 #endif