Mail Archives: djgpp/1999/01/10/17:45:33
There are some problems with your code (I don't understand what you mean by
"prompts you again for the
time", though, because nothing in your program evokes any prompt).
1. The value in `now' is the time value at the time when you called time().
This value doesn't change by itself, so if you want a new time value you
need to repeat the line ``time(&now);''.
2. printf():ing the value of a time_t using only the `d' specifier isn't
portable. `d' specifies a plain integer value, which is possibly of shorter
length than time_t (though not in DJGPP). To be safe, you need to cast the
value either to int or to long int (and in that case use a %ld flag and
specifier).
3. The value created by time() isn't really useful to display time, since
it's simply a very large integer number. If you want to print it, you
should first convert it to a string using for instance asctime() or
strftime().
Example code:
#include <stdio.h>
#include <time.h>
#include <dos.h>
int main()
{
time_t now;
time(&now);
printf("The current time is %s", asctime(localtime(&now)));
delay(2000);
time(&now);
printf("The current time is %s", asctime(localtime(&now)));
return 0;
}
However, what you were interested in was the difference between to times.
This code is probably closer to what you were aiming for:
#include <stdio.h>
#include <time.h>
#include <dos.h>
int main()
{
time_t t0, t1;
time(&t0);
delay(2000);
time(&t1);
printf("Time elapsed is %.0f seconds\n", difftime(t1, t0));
return 0;
}
In this program, I record and preserve starting time in `t0', and ending
time in `t1'. difftime() gives the difference between these two times as a
double value (it's better to use difftime() than straight subtraction,
because the underlying representation of time_t might change in a way that
makes the result of a subtraction undefined).
NB: the C Standard doesn't actually require an implementation of the time
functions to behave in a manner that's useful to a program of this kind (but
I believe DJGPP does); e.g. time() is allowed to bail out with a return
value of -1 if calendar time can't be determined, and there's nothing to
prevent an implementation from having time() returning the same value on
each call during the lifetime of the program.
DooMWiz
<7F82A0A45979CCE2 DOT 62B466D22539138C DOT F9F3FFF8903A0300 AT library-proxy DOT airnews DOT ne
t>...
> Hi. I want to be able to tell how long a certain file, etc. has been
open
>or how long a player has been playing, etc. by reading the current time at
>startup, then reading the time later on when they quit. Then just compare
>the difference to find how long they were using it. I've tried using
time_t
>and the time() function, but I'm having a bit of trouble. I tried using
>this simple program:
>
>int main()
>{
> time_t now;
> time(&now);
> printf("Time is %d\n", now);
> delay(2000);
> printf("Time is %d\n", now);
> return 0;
>}
>
>But the problem is that after the first time, it prompts you again for the
>time. I really don't know what I should do. Thanks in advance.
>
>
>
- Raw text -