Within a program, we may be interested in two kinds of time:
? Real time: This is the time as measured either from some standard point (calendar time) or from some fixed point (typically the start) in the life of a process (elapsed or wall clock time). Obtaining the calendar time is useful to programs that, for example, timestamp database records or files. Measuring elapsed time is useful for a program that takes periodic actions or makes regular
measurements from some external input device.
? Process time: This is the amount of CPU time used by a process. Measuring process time is useful for checking or optimizing the performance of a program or algorithm.
The gettimeofday() system call returns the calendar time in the buffer pointed to by tv.
#include <sys/time.h>
int gettimeofday(struct timeval *tv, struct timezone *tz);
Returns 0 on success, or –1 on error
The tv argument is a pointer to a structure of the following form:
struct timeval {
time_t tv_sec; /* Seconds since 00:00:00, 1 Jan 1970 UTC */
suseconds_t tv_usec; /* Additional microseconds (long int) */
};
The time() system call returns the number of seconds since the Epoch (i.e., the same value that gettimeofday() returns in the tv_sec field of its tv argument).
#include <time.h>
time_t time(time_t *timep);
Returns number of seconds since the Epoch,or (time_t) –1 on error
The ctime() function provides a simple method of converting a time_t value into
printable form.
#include <time.h>
char *ctime(const time_t *timep);
Returns pointer to statically allocated string terminated
by newline and \0 on success, or NULL on error
The gmtime() and localtime() functions convert a time_t value into a so-called brokendown
time. The broken-down time is placed in a statically allocated structure whose
address is returned as the function result.
#include <time.h>
struct tm *gmtime(const time_t *timep);
struct tm *localtime(const time_t *timep);
Both return a pointer to a statically allocated broken-down
time structure on success, or NULL on error
struct tm {
int tm_sec; /* Seconds (0-60) */
int tm_min; /* Minutes (0-59) */
int tm_hour; /* Hours (0-23) */
int tm_mday; /* Day of the month (1-31) */
int tm_mon; /* Month (0-11) */
int tm_year; /* Year since 1900 */
int tm_wday; /* Day of the week (Sunday = 0)*/
int tm_yday; /* Day in the year (0-365; 1 Jan = 0)*/
int tm_isdst; /* Daylight saving time flag
> 0: DST is in effect;
= 0: DST is not effect;
< 0: DST information not available */
};
The mktime() function translates a broken-down time, expressed as local time,
into a time_t value, which is returned as the function result. The caller supplies the
broken-down time in a tm structure pointed to by timeptr. During this translation,
the tm_wday and tm_yday fields of the input tm structure are ignored.
#include <time.h>
time_t mktime(struct tm *timeptr);
Returns seconds since the Epoch corresponding to timeptr
on success, or (time_t) –1 on error
#include <locale.h>
#include <time.h>
#include <sys/time.h>
#include "tlpi_hdr.h"
#define SECONDS_IN_TROPICAL_YEAR (365.24219 * 24 * 60 * 60)
int
main(int argc, char *argv[])
{
time_t t;
struct tm *gmp, *locp;
struct tm gm, loc;
struct timeval tv;
/* Retrieve time, convert and display it in various forms */
t = time(NULL);
printf("Seconds since the Epoch (1 Jan 1970): %ld", (long) t);
printf(" (about %6.3f years)\n", t / SECONDS_IN_TROPICAL_YEAR);
if (gettimeofday(&tv, NULL) == -1)
errExit("gettimeofday");
printf(" gettimeofday() returned %ld secs, %ld microsecs\n",
(long) tv.tv_sec, (long) tv.tv_usec);
gmp = gmtime(&t);
if (gmp == NULL)
errExit("gmtime");
gm = *gmp; /* Save local copy, since *gmp may be modified
by asctime() or gmtime() */
printf("Broken down by gmtime():\n");
printf(" year=%d mon=%d mday=%d hour=%d min=%d sec=%d ", gm.tm_year,
gm.tm_mon, gm.tm_mday, gm.tm_hour, gm.tm_min, gm.tm_sec);
printf("wday=%d yday=%d isdst=%d\n", gm.tm_wday, gm.tm_yday, gm.tm_isdst);
/* The TZ environment variable will affect localtime().
Try, for example:
TZ=Pacific/Auckland calendar_time
*/
locp = localtime(&t);
if (locp == NULL)
errExit("localtime");
loc = *locp; /* Save local copy */
printf("Broken down by localtime():\n");
printf(" year=%d mon=%d mday=%d hour=%d min=%d sec=%d ",
loc.tm_year, loc.tm_mon, loc.tm_mday,
loc.tm_hour, loc.tm_min, loc.tm_sec);
printf("wday=%d yday=%d isdst=%d\n\n",
loc.tm_wday, loc.tm_yday, loc.tm_isdst);
printf("asctime() formats the gmtime() value as: %s", asctime(&gm));
printf("ctime() formats the time() value as: %s", ctime(&t));
printf("mktime() of gmtime() value: %ld secs\n", (long) mktime(&gm));
printf("mktime() of localtime() value: %ld secs\n", (long) mktime(&loc));
exit(EXIT_SUCCESS);
}
char *
currTime(const char *format)
{
static char buf[BUF_SIZE]; /* Nonreentrant */
time_t t;
size_t s;
struct tm *tm;
t = time(NULL);
tm = localtime(&t);
if (tm == NULL)
return NULL;
s = strftime(buf, BUF_SIZE, (format != NULL) ? format : "%c", tm);
return (s == 0) ? NULL : buf;
}