Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > about difftime()

Reply
Thread Tools

about difftime()

 
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
 
 
 
Richard Heathfield
Guest
Posts: n/a
 
      01-07-2006
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)
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
=?ISO-8859-1?Q?Bj=F8rn_Augestad?=
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      01-07-2006
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
lid
 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      01-07-2006
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off




Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57