Code Comments
Programming Forum and web based access to our favorite programming groups.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
Post Follow-up to this message
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
Post Follow-up to this messageThanks..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...
Post Follow-up to this messageIn 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.
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.