Home > Archive > Unix Programming > April 2005 > difftime returns 0....help
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
difftime returns 0....help
|
|
| wetherbean 2005-04-20, 3:59 pm |
| 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
| |
| Eric Sosman 2005-04-20, 3:59 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
| |
| wetherbean 2005-04-21, 3:59 am |
| 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...
| |
| Wayne C. Morris 2005-04-21, 3:59 pm |
| 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.
|
|
|
|
|