difftime returns 0....help

  • Follow


Hi group..I am having a problem with difftime..I get the start time and
end time into a tm_struct and when I print out the struct it gives me
the correct times but then I do mktime to get it into seconds followed
by difftime to get the time difference and it returns 0....here is my
code:

              start_timeinfo=localtime(&start_time);
              printf("Start time %s\n",asctime(start_timeinfo));
              end_timeinfo=localtime(&end_time);
              printf("End time %s\n",asctime(end_timeinfo));
              start=mktime(start_timeinfo);
              printf("start %d\n",start);
              end=mktime(end_timeinfo);
              printf("end   %d\n",end);
              diff = difftime(end,start);

         printf ("Capture Length: %.2lf \n",diff);

the timeinfo variables are declared as tm struct *
the start_time and end time are unsigned long int
and start and end are time_t

the out put when this is run looks like this:
Start time Wed Apr  2 19:02:18 2003

End time Wed Apr  2 19:02:53 2003

start 1049328173
end 1049328173
Capture Length: 0.00

can anyone see what is going on??  Thanks

--wetherbean

0
Reply bjenkin1173 (10) 4/20/2005 5:09:44 PM


wetherbean wrote:
> Hi group..I am having a problem with difftime..I get the start time and
> end time into a tm_struct and when I print out the struct it gives me
> the correct times but then I do mktime to get it into seconds followed
> by difftime to get the time difference and it returns 0....here is my
> code:
> 
>               start_timeinfo=localtime(&start_time);
>               printf("Start time %s\n",asctime(start_timeinfo));
>               end_timeinfo=localtime(&end_time);
>               printf("End time %s\n",asctime(end_timeinfo));
>               start=mktime(start_timeinfo);
>               printf("start %d\n",start);
>               end=mktime(end_timeinfo);
>               printf("end   %d\n",end);
>               diff = difftime(end,start);
> 
>          printf ("Capture Length: %.2lf \n",diff);
> 
> the timeinfo variables are declared as tm struct *
> the start_time and end time are unsigned long int
> and start and end are time_t
> 
> the out put when this is run looks like this:
> Start time Wed Apr  2 19:02:18 2003
> 
> End time Wed Apr  2 19:02:53 2003
> 
> start 1049328173
> end 1049328173
> Capture Length: 0.00
> 
> can anyone see what is going on??  Thanks

    "7.23.3 Time conversion functions
    [...] Execution of any of the functions that return a
    pointer [...] may overwrite the information [...] from
    any previous call [...]"

    In other words, there's probably only one `struct tm'
object.  Each call to localtime() fills it in and returns
a pointer to it -- and the second call wipes out the data
filled in by the first call.  You wind up with two pointers
both pointing to the same `struct tm', containing the data
from the second localtime() call.

    Why are you working so hard, anyhow?  Why not just call
difftime(end_time, start_time) and avoid all this rummaging
around?

-- 
Eric.Sosman@sun.com

0
Reply Eric 4/20/2005 5:20:40 PM


Thanks..I thought about using time_t as ints but never the other way
around...lol...I guess I had been sitting here starring at it too
long...

0
Reply wetherbean 4/21/2005 5:41:04 AM

In article <1114062064.624200.307850@o13g2000cwo.googlegroups.com>,
 "wetherbean" <bjenkin1@gmail.com> wrote:

> Thanks..I thought about using time_t as ints but never the other way
> around...lol...I guess I had been sitting here starring at it too
> long...

It's not really a case of using an int as time_t.  The parameter to 
localtime is supposed to be a (time_t *), not an (unsigned long int *).

time_t is usually defined to be a long int (either signed or unsigned), so 
you can usually get away with using long instead of time_t.  But it's best 
to use the correct data type, just to be safe.
0
Reply Wayne 4/21/2005 5:08:31 PM

3 Replies
243 Views

(page loaded in 0.161 seconds)

Similiar Articles:




7/25/2012 8:15:28 PM


Reply: