about difftime()

  • Follow


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
-- 
A+

Emmanuel Delahaye
0
Reply emdel (952) 1/7/2006 7:37:53 PM

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.

-- 
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
0
Reply invalid171 (6558) 1/7/2006 8:01:51 PM


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
esosman@acm-dot-org.invalid
0
Reply esosman (1335) 1/7/2006 8:07:35 PM

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.

-- 
A+

Emmanuel Delahaye
0
Reply emdel (952) 1/7/2006 8:19:14 PM

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

Wouldn't that work only within the same year?
Bj�rn
0
Reply boa11 (125) 1/7/2006 8:40:58 PM

Bj�rn Augestad wrote:
> Emmanuel Delahaye wrote:
> 
>> 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.
>>
> 
> 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.

-- 
Eric Sosman
esosman@acm-dot-org.invalid
0
Reply esosman (1335) 1/7/2006 9:40:12 PM

Bj�rn Augestad a �crit :
>> 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?

> 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 !

-- 
A+

Emmanuel Delahaye
0
Reply emdel (952) 1/7/2006 9:42:16 PM

6 Replies
28 Views

(page loaded in 0.126 seconds)

Similiar Articles:




7/16/2012 12:51:22 AM


Reply: