Thursday, August 5, 2010

Time Functions

In this chapter we will look at how we can access the clock time with UNIX system calls. There are many more time functions than we consider here - see man pages and standard library function listings for full details. In this chapter we concentrate on applications of timing functions in C
Uses of time functions include:
  • telling the time.
  • timing programs and functions.
  • setting number seeds.

Basic time functions

Some of thge basic time functions are prototypes as follows:
time_t time(time_t *tloc) -- returns the time since 00:00:00 GMT, Jan. 1, 1970, measured in seconds.

If tloc is not NULL, the return value is also stored in the location to which tloc points.

time() returns the value of time on success.

On failure, it returns (time_t) -1. time_t is typedefed to a long (int) in and header files.

int ftime(struct timeb *tp) -- fills in a structure pointed to by tp, as defined in :



   struct timeb
   { time_t time;
unsigned short millitm;
short timezone;
short dstflag;
};


The structure contains the time since the epoch in seconds, up to 1000 milliseconds of more precise interval, the local time zone (measured in minutes of time westward from Greenwich), and a flag that, if nonzero, indicates that Day light Saving time applies locally during the appropriate part of the year.

On success, ftime() returns no useful value. On failure, it returns -1. 

Two other functions defined etc. in
#include  

char *ctime(time_t *clock),
char *asctime(struct tm *tm)

ctime() converts a long integer, pointed to by clock, to a 26-character string of the form produced by asctime(). It first breaks down clock to a tm structure by calling localtime(), and then calls asctime() to convert that tm structure to a string.

asctime() converts a time value contained in a tm structure to a 26-character string of the form:

   Sun Sep 16 01:03:52 1973

asctime() returns a pointer to the string.

No comments:

Post a Comment