Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

difftime returns 0....help
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


Report this thread to moderator Post Follow-up to this message
Old Post
wetherbean
04-20-05 08:59 PM


Re: difftime returns 0....help

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


Report this thread to moderator Post Follow-up to this message
Old Post
Eric Sosman
04-20-05 08:59 PM


Re: difftime returns 0....help
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...


Report this thread to moderator Post Follow-up to this message
Old Post
wetherbean
04-21-05 08:59 AM


Re: difftime returns 0....help
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.

Report this thread to moderator Post Follow-up to this message
Old Post
Wayne C. Morris
04-21-05 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Unix Programming archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 07:20 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.