[comp.lang.c] Time Info

molnar@neis.UUCP (Christopher Molnar) (05/24/91)

I am writing a C routine that needs to compare a start time and
the current time and have the result in minute.

Something like: Time-Present minus Time-In = Time Used (minutes)

Can any-one suggest where to find this in the Xenix reffereance manuals
or suggest a way I can accomplish this?

Thanks in advance for your help!

Chris Molnar
uunet!crdgw1!sixhub!neis!molnar     molnar@neis.uucp

mike@bria.UUCP (mike.stefanik) (05/26/91)

In an article, molnar@neis.UUCP (Christopher Molnar) writes:
>I am writing a C routine that needs to compare a start time and
>the current time and have the result in minute.

#include <time.h>
#include <sys/types.h>

time_t
deltat(since)
time_t since;
{
time_t now;

	time(&now);
	return(now - since);
}

-- 
Michael Stefanik, MGI Inc, Los Angeles | Opinions stated are never realistic
Title of the week: Systems Engineer    | UUCP: ...!uunet!bria!mike
-------------------------------------------------------------------------------
If MS-DOS didn't exist, who would UNIX programmers have to make fun of?

wirzeniu@klaava.Helsinki.FI (Lars Wirzenius) (05/26/91)

In article <39@neis.UUCP> molnar@neis.UUCP (Christopher Molnar) writes:
>I am writing a C routine that needs to compare a start time and
>the current time and have the result in minute.
>
>Can any-one suggest where to find this in the Xenix reffereance manuals
>or suggest a way I can accomplish this?

The standard library function difftime returns the difference in seconds
between two times (represented as time_t's, i.e.  whatever time()
returns).  This is easy to convert to minutes:

	time_t then, now;
	double secs, mins;			/* difftime returns
						 * double
						 */

	time(&then);
	.....
	time(&now);

	secs = difftime(then, now);
	mins = secs / 60.0; 			/* you could do some
						 * rounding here, if you
						 * prefer
						 */

Oops. I just checked my manuals for Xenix, and it seems that it
doesn't have difftime. :-(

In that case one needs to take advantage of the fact that time_t on SCO
Xenix (and most of the Unix world) is a long that tells the seconds
since the beginning of the Era (January 1, 1970, at 00:00:00 GMT), so
the difftime can be replaced by

	secs = (double) (now - then);		/* of course, you could
						 * use long for secs now,
						 * but that would make it
						 * more difficult to use
						 * difftime in the future.
						 */

and the rest is identical.
-- 
Lars Wirzenius     wirzeniu@cc.helsinki.fi

neufeld@aurora.physics.utoronto.ca (Christopher Neufeld) (05/26/91)

In article <268@bria.UUCP> uunet!bria!mike writes:
>In an article, molnar@neis.UUCP (Christopher Molnar) writes:
>>I am writing a C routine that needs to compare a start time and
>>the current time and have the result in minute.
>
>#include <time.h>
>#include <sys/types.h>
>
>time_t
>deltat(since)
>time_t since;
>{
>time_t now;
>
>	time(&now);
>	return(now - since);
>}
>
   I would rather return the value:  difftime(now, since) / 60.0
On my compiler, 'now-since' doesn't mean minutes or seconds, or
anything else obvious.
   You can also write the function as:
return(difftime(time(NULL),since)/60.0);

which is now so short that it might as well be a #define macro.

#define DELTAT(tnought) (difftime(time(NULL),tnought)/60.0)


>Michael Stefanik
>UUCP: ...!uunet!bria!mike


-- 
 Christopher Neufeld....Just a graduate student  | There no place like $FC58
 neufeld@aurora.physics.utoronto.ca    Ad astra! | They're $FF69-ing my
 cneufeld@{pnet91,pro-cco}.cts.com               | every word! Send for a
 "Don't edit reality for the sake of simplicity" | free $A56E.