2013-07-18 00:13:04 +04:00
|
|
|
/* $NetBSD: difftime.c,v 1.14 2013/07/17 20:13:04 christos Exp $ */
|
1996-09-11 02:04:29 +04:00
|
|
|
|
|
|
|
/*
|
|
|
|
** This file is in the public domain, so clarified as of
|
2010-01-01 01:49:15 +03:00
|
|
|
** 1996-06-05 by Arthur David Olson.
|
1996-09-11 02:04:29 +04:00
|
|
|
*/
|
1995-03-10 02:41:11 +03:00
|
|
|
|
1997-07-14 00:25:30 +04:00
|
|
|
#include <sys/cdefs.h>
|
2000-09-14 02:32:25 +04:00
|
|
|
#if defined(LIBC_SCCS) && !defined(lint)
|
1997-07-14 00:25:30 +04:00
|
|
|
#if 0
|
2010-01-01 01:49:15 +03:00
|
|
|
static char elsieid[] = "@(#)difftime.c 8.1";
|
1997-07-14 00:25:30 +04:00
|
|
|
#else
|
2013-07-18 00:13:04 +04:00
|
|
|
__RCSID("$NetBSD: difftime.c,v 1.14 2013/07/17 20:13:04 christos Exp $");
|
1997-07-14 00:25:30 +04:00
|
|
|
#endif
|
2000-09-14 02:32:25 +04:00
|
|
|
#endif /* LIBC_SCCS and not lint */
|
|
|
|
|
1995-03-10 02:21:48 +03:00
|
|
|
/*LINTLIBRARY*/
|
|
|
|
|
2010-01-01 01:49:15 +03:00
|
|
|
#include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
|
1995-03-10 02:21:48 +03:00
|
|
|
|
2013-07-18 00:13:04 +04:00
|
|
|
double ATTRIBUTE_CONST
|
2012-03-20 20:38:44 +04:00
|
|
|
difftime(const time_t time1, const time_t time0)
|
1995-03-10 02:21:48 +03:00
|
|
|
{
|
2010-01-01 01:49:15 +03:00
|
|
|
/*
|
|
|
|
** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
|
|
|
|
** (assuming that the larger type has more precision).
|
|
|
|
*/
|
2012-10-27 03:23:23 +04:00
|
|
|
/*CONSTCOND*/
|
2010-01-01 01:49:15 +03:00
|
|
|
if (sizeof (double) > sizeof (time_t))
|
|
|
|
return (double) time1 - (double) time0;
|
2012-10-27 03:23:23 +04:00
|
|
|
/*LINTED const not */
|
2010-01-01 01:49:15 +03:00
|
|
|
if (!TYPE_INTEGRAL(time_t)) {
|
|
|
|
/*
|
|
|
|
** time_t is floating.
|
|
|
|
*/
|
|
|
|
return time1 - time0;
|
|
|
|
}
|
2012-10-27 03:23:23 +04:00
|
|
|
/*LINTED const not */
|
2010-01-01 01:49:15 +03:00
|
|
|
if (!TYPE_SIGNED(time_t)) {
|
|
|
|
/*
|
|
|
|
** time_t is integral and unsigned.
|
|
|
|
** The difference of two unsigned values can't overflow
|
|
|
|
** if the minuend is greater than or equal to the subtrahend.
|
|
|
|
*/
|
|
|
|
if (time1 >= time0)
|
2013-07-18 00:13:04 +04:00
|
|
|
return time1 - time0;
|
|
|
|
else return -(double) (time0 - time1);
|
2002-01-29 15:40:33 +03:00
|
|
|
}
|
1995-03-10 02:21:48 +03:00
|
|
|
/*
|
2010-01-01 01:49:15 +03:00
|
|
|
** time_t is integral and signed.
|
|
|
|
** Handle cases where both time1 and time0 have the same sign
|
|
|
|
** (meaning that their difference cannot overflow).
|
1995-03-10 02:21:48 +03:00
|
|
|
*/
|
2010-01-01 01:49:15 +03:00
|
|
|
if ((time1 < 0) == (time0 < 0))
|
|
|
|
return time1 - time0;
|
1995-03-10 02:21:48 +03:00
|
|
|
/*
|
2010-01-01 01:49:15 +03:00
|
|
|
** time1 and time0 have opposite signs.
|
2013-07-18 00:13:04 +04:00
|
|
|
** Punt if uintmax_t is too narrow.
|
|
|
|
** This suffers from double rounding; attempt to lessen that
|
|
|
|
** by using long double temporaries.
|
1995-03-10 02:21:48 +03:00
|
|
|
*/
|
2012-10-26 22:30:11 +04:00
|
|
|
/* CONSTCOND */
|
2013-07-18 00:13:04 +04:00
|
|
|
if (sizeof (uintmax_t) < sizeof (time_t))
|
2010-01-01 01:49:15 +03:00
|
|
|
return (double) time1 - (double) time0;
|
1995-03-10 02:21:48 +03:00
|
|
|
/*
|
2010-01-01 01:49:15 +03:00
|
|
|
** Stay calm...decent optimizers will eliminate the complexity below.
|
1995-03-10 02:21:48 +03:00
|
|
|
*/
|
2010-01-01 01:49:15 +03:00
|
|
|
if (time1 >= 0 /* && time0 < 0 */)
|
2013-07-18 00:13:04 +04:00
|
|
|
return (uintmax_t) time1 + (uintmax_t) (-(time0 + 1)) + 1;
|
|
|
|
return -(double) ((uintmax_t) time0 + (uintmax_t) (-(time1 + 1)) + 1);
|
1995-03-10 02:21:48 +03:00
|
|
|
}
|