time functions

Scott J Brickner sjb at piobe.austin.ibm.com
Thu Jun 13 07:59:53 AEST 1991


In article <6583 at graphite14.UUCP>, johnsonl at 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



More information about the Comp.lang.c mailing list