Emmanuel Delahaye wrote:
> Hi,
>
> Is there a portable way to convert the value returned by difftime (a
> number of seconds of type double) in time_t, which, AFAIK, is not a
> number of seconds.
The only thing I can think of is to round or truncate the
difftime() value to get an integral number of seconds, and use
that to populate a struct tm. Some care is required in case
INT_MAX is small; something like
long seconds = round(difftime(xmas, now));
struct tm tm = { 0 };
tm.tm_mday = seconds / (60 * 60 * 24L);
seconds %= 60 * 60 * 24L;
tm.tm_hour = seconds / (60 * 60);
seconds %= 60 * 60;
tm.tm_sec = seconds; /* < INT_MAX */
gmtime (&tm);
might do the trick. It's a little sleazy about leap seconds,
but I think that'll be true of any method that tries to use
an "absolute" time to represent an interval.
Another, simpler method:
printf ("%.0f days to go\n",
difftime(xmas, now) / (60 * 60 * 24.0));
Again, this ignores leap seconds.
However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?
--
Eric Sosman
lid