gettimeofday(2)
NAME
gettimeofday − get the date and time
SYNOPSIS
#include <sys/time.h>
int gettimeofday(struct timeval *tp, void *tzp);
DESCRIPTION
The gettimeofday() function obtains the current time, expressed as seconds and microseconds since 00:00 Coordinated Universal Time (UTC), January 1, 1970, and stores it in the timeval structure pointed to by tp. The resolution of the system clock is unspecified.
If tzp is not a null pointer, the behaviour is unspecified.
RETURN VALUE
The gettimeofday() function returns 0 and no value is reserved to indicate an error.
ERRORS
No errors are defined.
SEE ALSO
ctime(), ftime(), <sys/time.h>.
CHANGE HISTORY
First released in Issue 4, Version 2.
HP-UX EXTENSIONS
SYNOPSIS
#include <time.h>
int gettimeofday(struct timeval *tp,
struct timezone *tzp);
DESCRIPTION
The structures pointed to by tp and tzp are defined in <time.h> as:
struct timeval {
unsigned long tv_sec; /* seconds since Jan. 1, 1970 */
long tv_usec; /* and microseconds */
};
struct timezone {
int tz_minuteswest; /* of UTC */
int tz_dsttime; /* type of DST correction to apply */
};
The timezone structure indicates the local time zone (measured in minutes of time westward from UTC), and a flag that, if nonzero, indicates that Daylight Savings Time applies locally during the appropriate part of the year. Programs should use this time zone information only in the absence of the TZ environment variable.
Security Restrictions
Only a user with appropriate privileges can set the time of day.
RETURN VALUE
gettimeofday() return the following values:
0 Successful completion.
-1 Failure. errno is set to indicate the error.
ERRORS
gettimeofday() fails, errno is set to the following value.
[EFAULT] An argument address referenced invalid memory. The reliable detection of this error is implementation dependent.
EXAMPLES
The following example calls gettimeofday() twice. It then computes the lapsed time between the calls in seconds and microseconds and stores the result in a timeval structure:
struct timeval first,
second,
lapsed;
struct timezone tzp;
gettimeofday (&first, &tzp);
/* lapsed time */
gettimeofday (&second, &tzp);
if (first.tv_usec > second.tv_usec) {
second.tv_usec += 1000000;
second.tv_sec--;
}
lapsed.tv_usec = second.tv_usec - first.tv_usec;
lapsed.tv_sec = second.tv_sec - first.tv_sec;
WARNINGS
The microsecond value usually has a granularity much greater than one due to the resolution of the system clock. Relying on any granularity (particularly of one) will render code nonportable.
AUTHOR
gettimeofday() was developed by the University of California, Berkeley, and SecureWare Inc.
SEE ALSO
date(1), stime(2), time(2), ctime(3C).
Hewlett-Packard Company — HP-UX Release 10.20: July 1996