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