Keith Marshall - 2013-12-30

On reflection, is it really necessary to even rely on MSVCRT.DLL for something as trivial as difftime(), never mind probe it? I think not; the following is (mostly) independent of MSVCRT.DLL:

typedef __int32 __time32_t;
typedef __int64 __time64_t;

#if MSVCRT_VERSION < 700 || defined _USE_32BIT_TIME_T
typedef __time32_t time_t;
#else
typedef __time64_t time_t;
#endif

static __inline__
double _difftime32( __time32_t __t1, __time32_t __t0 )
{ return (double)((__time64_t)(__t1) - (__time64_t)(__t0)); }

static __inline__
double _difftime64( __time64_t __t1, __time64_t __t0 )
{ return (double)((long double)(__t1) - (long double)(__t0)); }

#if MSVCRT_VERSION < 800 || defined _USE_32BIT_TIME_T
static __inline__
double difftime( time_t __t1, time_t __t0 )
{ return _difftime32( (__time32_t)(__t1), (__time32_t)(__t0) ); }

#else
static __inline__
double difftime( time_t __t1, time_t __t0 )
{ return _difftime64( (__time64_t)(__t1), (__time64_t)(__t0) ); }
#endif

Note that, while the above provides an implementation of difftime() which should work, regardless of the MSVCRT.DLL version present on the run time host, it does highlight the anomaly which is responsible the the present broken implementation: the current time.h header makes time_t equivalent to __time64_t for all MSVCRT_VERSION, (and 700 is the default), unless _USE_32BIT_TIME_T is defined, (which is just plain wrong for some -- indeed perhaps all -- of the MSVCRT.DLL versions still in use, without header file shenanigans to alter the underlying behaviour), but there is no working difftime() implementation specified in the (default) interval MSVCRT_VERSION < 800.

 
Last edit: Keith Marshall 2013-12-30