Suresh wrote:
hello,
i am new to c and how do i use time function to get time.
#include <stdio.h>
#include <time.h>
#include <string.h>
#define TMBUFSIZ 120 /* only 26 needed for localtime, ctime,
etc */
int main(void)
{
time_t now;
struct tm tyme;
char timebuf[TMBUFSIZ] = "";
now = time(0); /* get the time. A simple line of
'time(&now);' works too. */
/* the short way to show the time */
printf("The time is now (from ctime) %s", ctime(&now));
/* a longer way that sets the struct tm tyme for further use */
tyme = *localtime(&now); /* set up the broken-down time struct */
strcpy(timebuf, asctime(&tyme)); /* We could just printf the
asctime() result as with
ctime above */
printf("The longer way: %s", timebuf);
/* another way to use the struct tm tyme */
strftime(timebuf, TMBUFSIZ, "This is %A, %e %B %Y, %r", &tyme);
printf("Another form: %s\n", timebuf);
return 0;
}
The time is now (from ctime) Sun Apr 25 00:13:38 2004
The longer way: Sun Apr 25 00:13:38 2004
Another form: This is Sunday, 25 April 2004, 12:13:38 AM