Files
amiga-tz/difftime.c
T

66 lines
1.7 KiB
C
Raw Normal View History

1996-06-05 08:01:54 -04:00
/*
** This file is in the public domain, so clarified as of
2004-10-25 14:59:05 -04:00
** 1996-06-05 by Arthur David Olson (arthur_david_olson@nih.gov).
1996-06-05 08:01:54 -04:00
*/
1989-02-24 18:51:47 -05:00
#ifndef lint
#ifndef NOID
static char elsieid[] = "%W%";
#endif /* !defined NOID */
#endif /* !defined lint */
/*LINTLIBRARY*/
2004-11-09 14:46:42 -05:00
#include "private.h" /* for time_t, TYPE_INTEGRAL, and TYPE_SIGNED */
1994-05-05 08:29:51 -04:00
1989-02-24 18:51:47 -05:00
double
difftime(time1, time0)
const time_t time1;
const time_t time0;
{
2004-11-01 13:18:56 -05:00
/*
** If (sizeof (double) > sizeof (time_t)) simply convert and subtract
** (assuming that the larger type has more precision).
** This is the common real-world case circa 2004.
*/
if (sizeof (double) > sizeof (time_t))
return (double) time1 - (double) time0;
2004-10-21 10:29:28 -04:00
if (!TYPE_INTEGRAL(time_t)) {
/*
** time_t is floating.
*/
2004-11-01 13:18:56 -05:00
return time1 - time0;
2004-10-26 12:24:05 -04:00
}
if (!TYPE_SIGNED(time_t)) {
2004-10-25 12:50:55 -04:00
/*
** 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)
return time1 - time0;
else return -((double) (time0 - time1));
2001-11-20 17:10:31 -05:00
}
2004-10-26 12:24:05 -04:00
/*
** time_t is integral and signed.
** Handle cases where both time1 and time0 have the same sign
** (meaning that their difference cannot overflow).
*/
2004-11-01 13:33:49 -05:00
if ((time1 < 0) == (time0 < 0))
2004-10-26 12:24:05 -04:00
return time1 - time0;
/*
** time1 and time0 have opposite signs.
** Punt if unsigned long is too narrow.
*/
if (sizeof (unsigned long) < sizeof (time_t))
return (double) time1 - (double) time0;
/*
** Stay calm...decent optimizers will eliminate the complexity below.
*/
if (time1 >= 0 /* && time0 < 0 */)
return (unsigned long) time1 +
(unsigned long) (-(time0 + 1)) + 1;
return -(double) ((unsigned long) time0 +
(unsigned long) (-(time1 + 1)) + 1);
1989-02-24 18:51:47 -05:00
}