On Fri, 31 Aug 2007 07:48:53 -0000, alcool <fa***********@ gmail.com>
wrote:
>
You are probably thinking about struct tm; time_t is
not a struct. Store the values for one time in a struct tm
object[*] and use mktime() to convert them to a time_t. Then
do the same with the other, getting another time_t. Then
use difftime() to find the number of seconds between the two
times.
I have try:
------------
void cfrDate(char h[], char min[], char y[],char mo[], char d[]){
struct tm *newtime, *oldtime;
time_t result;
time_t long_time;
double elapsed_time;
Aside: The name long_time could be misleading, since time_t isn't
necessarily /*signed*/ long, or even unsigned long, although those are
common. It also isn't a good variable name as it says nothing whatever
about the _purpose_ for which the variable is used. In this routine I
would probably call it current_time or time_now or just now. result is
a little better, but still not very specific; I would probably call it
other_time or time_then or then. The choice of names is important to
the human(s) reading your code -- possibly including yourself, some
time in the future -- but make no difference to the compiler, as long
as they are legal and unique (which these are).
>
time( &long_time );
newtime = localtime( &long_time );
oldtime = localtime( &long_time );
Note that localtime() (or gmtime())) returns a pointer to static
memory, not a unique object or value. If after this you used both
oldtime->foo and newtime->bar you would have problems. You don't in
fact do that, so this time you're OK, but skating on thin ice.
>
newtime->tm_hour = (int)h;
newtime->tm_min = (int)min;
newtime->tm_year = (int)y - 1900;
newtime->tm_mon = (int)mo - 1;
newtime->tm_mday = (int)d;
As already noted these are your real problem. Also remember Eric's
advice to set tm_isdst in the struct tm you will pass to mktime().
result = mktime(newtime) ;
if ( (result = mktime(newtime) ) == (time_t)-1){
There's no reason to call mktime() twice with the same argument. Or
more precisely, with pointers to the same* data, whether or not
located in the same place. (* They won't actually be identical if the
first call normalized it, but then they will be equivalent.)
result = mktime(newtime) ;
if( result == (time_t)-1 ) ...
is one common style, and
if( (result = mktime(newtime) ) == (time_t)-1 ) ...
is another common style.
Both are legal. Some people prefer one or the other across the board,
and some choose depending on the particular situation. At the level of
learning you display in your post, you might want to just stick with
one for now, while you focus on more important points.
- formerly david.thompson1 || achar(64) || worldnet.att.ne t