delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/11/25/00:05:26

Date: Mon, 24 Nov 1997 21:03:10 -0800 (PST)
Message-Id: <199711250503.VAA01601@adit.ap.net>
Mime-Version: 1.0
To: Anton Helm <tony AT nt DOT tuwien DOT ac DOT at>, djgpp AT delorie DOT com
From: Nate Eldredge <eldredge AT ap DOT net>
Subject: Re: strange behavior of difftime()

At 12:07  11/25/1997 +0100, Anton Helm wrote:
[snipped]
>>From the above quote I would assume that there is no restriction on time0
and time1,
>especially the standard doesn't assume that time1 > time0.
>
>Why does difftime() from the DJGPP library produce erronous results if
time0 > time1 ??????
>
>difftime() should return a negative result, but actually it returns
>
>(double)(UINT_MAX - d)
>
>where d is the absolute value of the difference of time0 and time1.
Probably because `time_t' is an unsigned integer type. An integer
subtraction is done, and the result is less than zero. But due to the
workings of two's complement arithmetic, when considered as an unsigned
value, this will be a very large number. IMHO, this is not really a problem,
since most people know which way time goes. :) But if you want a fix, here's
a replacement `difftime.c' which should do the job (the patch was larger
than the new file)
--cut--
#include <time.h>

double
difftime(time_t time1, time_t time0)
{
  return (double)time1 - (double)time0;
}
--cut--

Nate Eldredge
eldredge AT ap DOT net



- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019