about difftime()

  • Thread starter Emmanuel Delahaye
  • Start date
E

Emmanuel Delahaye

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.

For this reason, I guess, that this code of mine, supposed to give an
example about 'how to use time functions correctly' is not portable, is
it ?

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

int main (void)
{
time_t now = time (NULL);
struct tm tm_now = *localtime (&now);
char s[64];

strftime (s, sizeof s, "%d/%m/%Y", &tm_now);
printf ("Today is : %s\n", s);


/* next Christmas */
{
struct tm tm_xmas =
{0};

tm_xmas.tm_year = tm_now.tm_year;
tm_xmas.tm_mon = 12 - 1;
tm_xmas.tm_mday = 25;

/* ajustment */
{
time_t xmas = mktime (&tm_xmas);

strftime (s, sizeof s, "%d/%m/%Y", &tm_xmas);
printf ("next Christmas is : %s\n", s);

{
time_t diff = difftime (xmas, now);
struct tm tm_diff = *gmtime (&diff);

printf ("Only %d days remaining before Christmas\n",
tm_diff.tm_yday);
}
}
}

return 0;
}

Today is : 07/01/2006
next Christmas is : 25/12/2006
Only 351 days remaining before Christmas
 
R

Richard Heathfield

Emmanuel Delahaye said:
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.

You can use a correctly-initialised struct tm to store your number of
seconds, provided it fits into an int - if not, try a bit of intelligent
scaling, e.g. if you know it'll fit into a year but not a day, you can do
something like: tm_yday = d / 86400; tm_sec = (d / 86400 - tm_yday) * 86400

Then shove the whole mess through mktime to normalise it and hand you a
time_t on a plate.
 
E

Eric Sosman

Emmanuel said:
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?
 
E

Emmanuel Delahaye

Eric Sosman a écrit :
However, since you've got both today's and Christmas'
dates in struct tm form, why not just subract the tm_yday
elements?

Yes, sounds obvious once you mentioned it ! Anyway, thanks for your
answers Eric and Richard, and Happy New Year.
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Emmanuel said:
Eric Sosman a écrit :



Yes, sounds obvious once you mentioned it ! Anyway, thanks for your
answers Eric and Richard, and Happy New Year.

Wouldn't that work only within the same year?
Bjørn
 
E

Eric Sosman

Bjørn Augestad said:
Wouldn't that work only within the same year?

Yes. Emmanuel's code constructs the date of Christmas
by plugging December 25 into the struct tm for the current
time, so "same year" is certain.
 
E

Emmanuel Delahaye

Bjørn Augestad a écrit :
Wouldn't that work only within the same year?

Thinking more about it, yes, it could be a problem between in the 26 to
31 December period. But at this time of the year, a few people are
concerned by the question !
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top