Home | History | Annotate | Line # | Download | only in time
      1  1.24  christos /*	$NetBSD: difftime.c,v 1.24 2024/01/20 14:52:49 christos Exp $	*/
      2  1.19  christos 
      3  1.19  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.24  christos __RCSID("$NetBSD: difftime.c,v 1.24 2024/01/20 14:52:49 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.23  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.24  christos 		/*NOTREACHED*/
     49  1.22  christos 		return time0 <= time1 ? time1 - time0 :
     50  1.22  christos 		    dminus((double)(time0 - time1));
     51  1.16  christos 
     52  1.16  christos 	/* Use uintmax_t if wide enough.  */
     53  1.17  christos 	/*CONSTCOND*/
     54  1.23  christos 	if (sizeof(time_t) <= sizeof(uintmax_t)) {
     55  1.16  christos 		uintmax_t t1 = time1, t0 = time0;
     56  1.22  christos 		return time0 <= time1 ? t1 - t0 : dminus((double)(t0 - t1));
     57   1.8    kleink 	}
     58  1.16  christos 
     59   1.1       jtc 	/*
     60  1.10   mlelstv 	** Handle cases where both time1 and time0 have the same sign
     61  1.10   mlelstv 	** (meaning that their difference cannot overflow).
     62   1.1       jtc 	*/
     63  1.10   mlelstv 	if ((time1 < 0) == (time0 < 0))
     64  1.10   mlelstv 		return time1 - time0;
     65  1.16  christos 
     66   1.1       jtc 	/*
     67  1.16  christos 	** The values have opposite signs and uintmax_t is too narrow.
     68  1.14  christos 	** This suffers from double rounding; attempt to lessen that
     69  1.14  christos 	** by using long double temporaries.
     70   1.1       jtc 	*/
     71  1.16  christos 	{
     72  1.16  christos 		long double t1 = time1, t0 = time0;
     73  1.16  christos 		return t1 - t0;
     74  1.16  christos 	}
     75   1.1       jtc }
     76