[comp.lang.c] time functions

TLIMONCE%DREW.BITNET@CUNYVM.CUNY.EDU (04/07/88)

One thing that's not too well defined (pre-dpANSI) is the time functions.
I need to find a way to tell how long a procedure took to execute (I'm
doing time comparisons for a Csci class).  Because of my situation
I need the number of [any unit of time]s that my process used... not
how much time has passed.

I am on a VAX running VMS using VAX-C (the equivalent for Eunice will
be good enough).

AdvaTHANKSnce

Tom Limoncelli
tlimonce@drew.bitnet

nevin1@ihlpf.ATT.COM (00704a-Liber) (04/08/88)

In article <12851@brl-adm.ARPA> TLIMONCE%DREW.BITNET@CUNYVM.CUNY.EDU writes:
>One thing that's not too well defined (pre-dpANSI) is the time functions.
>I need to find a way to tell how long a procedure took to execute (I'm
>doing time comparisons for a Csci class).  Because of my situation
>I need the number of [any unit of time]s that my process used... not
>how much time has passed.

The reason that this isn't very well-defined for C is because it is not a
part of the C language.  The timings that you want are a function of the
operating system and/or hardware that you are using, and may possibly be
available via operating system calls.  You should cross-post your
request to comp.os.vms for more information.
-- 
 _ __			NEVIN J. LIBER	..!ihnp4!ihlpf!nevin1	(312) 510-6194
' )  )				"The secret compartment of my ring I fill
 /  / _ , __o  ____		 with an Underdog super-energy pill."
/  (_</_\/ <__/ / <_	These are solely MY opinions, not AT&T's, blah blah blah

johnsonl@motcid.UUCP (Lisa A. Johnson) (06/12/91)

I'm trying to write a program that finds the local time using the
functions in time.h.  I don't care about the date, I just want the
time, but I can't figure out how to do it.  Can anyone out there
help?  Which functions do I have to use, and what kind of variables
do I need?

Thanks in advance.

Lisa

wirzeniu@klaava.Helsinki.FI (Lars Wirzenius) (06/12/91)

In article <6583@graphite14.UUCP> johnsonl@motcid.UUCP (Lisa A. Johnson) writes:
>I'm trying to write a program that finds the local time using the
>functions in time.h.  I don't care about the date, I just want the
>time, but I can't figure out how to do it.  Can anyone out there
>help?  Which functions do I have to use, and what kind of variables
>do I need?

The first thing you need to know is that there are two ways to represent
the time in the standard C library. The first one uses a type called
type_t (often long, but don't count on it; however, you may have to use
long on some old Unix systems). This is a typedef for some integer type,
and holds the time encoded in some suitable way.

You can get the current date and time as a time_t object with the
function time, for example:

	time_t now;

	time(&now);

time_t alone is more or less useless, it cannot be used for nearly
anything. For further processing it needs to be converted to the other
kind of representation, a struct tm. The conversion is done with the
function localtime, for example:

	time_t now;
	struct tm *now_tm;

	time(&now);
	now_tm = localtime(&now);

A struct tm has (at least) the following fields:

	tm_year		year
	tm_mon		month
	tm_mday		day of the month
	tm_wday		day of the week
	tm_hour		hour of the day
	tm_min		minutes past full hour
	tm_sec		seconds past full minute

The last three are what you are searching for, I think.

Note that the localtime function returns a pointer to an internal struct
tm, and that the next call to localtime will overwrite this buffer. You
need to copy it to a safe place if you are going to need the data later.

There are also a number of other functions that may come in handy, these
include asctime, ctime, gmtime, and mktime. They should be covered in
any decent manual.
-- 
Lars Wirzenius     wirzeniu@cc.helsinki.fi

sjb@piobe.austin.ibm.com (Scott J Brickner) (06/13/91)

In article <6583@graphite14.UUCP>, johnsonl@motcid.UUCP (Lisa A.
Johnson) writes:

> I'm trying to write a program that finds the local time using the
> functions in time.h.  I don't care about the date, I just want the
> time, but I can't figure out how to do it.  Can anyone out there
> help?  Which functions do I have to use, and what kind of variables
> do I need?

Try using time() and localtime(), thusly:

#include <time.h>

main()
{
    time_t t;
    struct tm *tm;

    time( &t);  /* t now contains seconds since 00:00 Jan 1, 1970 on UNIX */
    tm = localtime( &t);
    printf( "%02d:%02d:%02d\n", tm->tm_hour, tm->tm_min, tm->tm_sec);
}

"t" may have seconds (or some other unit) since some other epoch date on
non-UNIX systems, but localtime() should compensate for this.

Hope it helps...

Scott J Brickner