1 /* difftime( time_t, time_t )
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
11 double difftime( time_t time1, time_t time0 )
13 /* If we want to avoid rounding errors and overflows, we need to be
14 careful with the exact type of time_t being unknown to us.
15 The code below is based on tzcode's difftime.c, which is in the
16 public domain, so clarified as of 1996-06-05 by Arthur David Olson.
19 /* If double is large enough, simply covert and substract
20 (assuming that the larger type has more precision).
22 if ( sizeof( time_t ) < sizeof( double ) )
24 return (double)time1 - (double)time0;
27 /* The difference of two unsigned values cannot overflow if the
28 minuend is greater or equal to the subtrahend.
30 if ( ! _PDCLIB_TYPE_SIGNED( time_t ) )
32 return ( time1 >= time0 ) ? (double)( time1 - time0 ) : -(double)( time0 - time1 );
35 /* Use uintmax_t if wide enough. */
36 if ( sizeof( time_t ) <= sizeof( _PDCLIB_uintmax_t ) )
38 _PDCLIB_uintmax_t t1 = time1, t0 = time0;
39 return ( time1 >= time0 ) ? t1 - t0 : -(double)( t0 - t1 );
42 /* If both times have the same sign, their difference cannot overflow. */
43 if ( ( time1 < 0 ) == ( time0 < 0 ) )
48 /* The times have opposite signs, and uintmax_t is too narrow.
49 This suffers from double rounding; attempt to lessen that
50 by using long double temporaries.
52 long double t1 = time1, t0 = time0;
60 #include "_PDCLIB_test.h"
64 TESTCASE( NO_TESTDRIVER );