Home | History | Annotate | Line # | Download | only in time
difftime.c revision 1.18.4.1
      1 /*	$NetBSD: difftime.c,v 1.18.4.1 2019/06/10 22:05:22 christos Exp $	*/
      2 
      3 /* Return the difference between two timestamps.  */
      4 
      5 /*
      6 ** This file is in the public domain, so clarified as of
      7 ** 1996-06-05 by Arthur David Olson.
      8 */
      9 
     10 #include <sys/cdefs.h>
     11 #if defined(LIBC_SCCS) && !defined(lint)
     12 #if 0
     13 static char	elsieid[] = "@(#)difftime.c	8.1";
     14 #else
     15 __RCSID("$NetBSD: difftime.c,v 1.18.4.1 2019/06/10 22:05:22 christos Exp $");
     16 #endif
     17 #endif /* LIBC_SCCS and not lint */
     18 
     19 /*LINTLIBRARY*/
     20 
     21 #include "private.h"	/* for time_t and TYPE_SIGNED */
     22 
     23 /* Return -X as a double.  Using this avoids casting to 'double'.  */
     24 static double
     25 dminus(double x)
     26 {
     27 	return -x;
     28 }
     29 
     30 double
     31 difftime(time_t time1, time_t time0)
     32 {
     33 	/*
     34 	** If double is large enough, simply convert and subtract
     35 	** (assuming that the larger type has more precision).
     36 	*/
     37 	/*CONSTCOND*/
     38 	if (sizeof (time_t) < sizeof (double)) {
     39 		double t1 = time1, t0 = time0;
     40 		return t1 - t0;
     41  	}
     42 
     43 	/*
     44 	** The difference of two unsigned values can't overflow
     45 	** if the minuend is greater than or equal to the subtrahend.
     46 	*/
     47 	if (!TYPE_SIGNED(time_t))
     48 		return time0 <= time1 ? time1 - time0 :
     49 		    dminus((double)(time0 - time1));
     50 
     51 	/* Use uintmax_t if wide enough.  */
     52 	/*CONSTCOND*/
     53 	if (sizeof (time_t) <= sizeof (uintmax_t)) {
     54 		uintmax_t t1 = time1, t0 = time0;
     55 		return time0 <= time1 ? t1 - t0 : dminus((double)(t0 - t1));
     56 	}
     57 
     58 	/*
     59 	** Handle cases where both time1 and time0 have the same sign
     60 	** (meaning that their difference cannot overflow).
     61 	*/
     62 	if ((time1 < 0) == (time0 < 0))
     63 		return time1 - time0;
     64 
     65 	/*
     66 	** The values have opposite signs and uintmax_t is too narrow.
     67 	** This suffers from double rounding; attempt to lessen that
     68 	** by using long double temporaries.
     69 	*/
     70 	{
     71 		long double t1 = time1, t0 = time0;
     72 		return t1 - t0;
     73 	}
     74 }
     75