www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1999/01/10/17:45:33

From: "Yyrkoon" <yyrkoon AT imrryr DOT mel>
Newsgroups: comp.os.msdos.djgpp
References: <7F82A0A45979CCE2 DOT 62B466D22539138C DOT F9F3FFF8903A0300 AT library-proxy DOT airnews DOT net>
Subject: Re: Time_t stuff
Lines: 98
X-Newsreader: Microsoft Outlook Express 4.72.3155.0
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0
Message-ID: <WP9m2.26986$9S3.68301@nntpserver.swip.net>
Date: Sun, 10 Jan 1999 23:25:18 +0100
NNTP-Posting-Host: 130.244.153.102
X-Complaints-To: news-abuse AT swip DOT net
X-Trace: nntpserver.swip.net 916007286 130.244.153.102 (Sun, 10 Jan 1999 23:28:06 MET DST)
NNTP-Posting-Date: Sun, 10 Jan 1999 23:28:06 MET DST
Organization: A Customer of Tele2
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp
Reply-To: djgpp AT delorie DOT com

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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019