gettimeofday()--Get Current UTC Time


  Syntax
 #include <sys/time.h>

 int gettimeofday (struct timeval *tp,
                   struct timezone *tzp);

  Service Program Name:QWCTZUTC

  Default Public Authority: *USE

  Threadsafe: Yes

The gettimeofday() function retrieves the current Coordinated Universal Time (UTC) and places it in the timeval structure pointed to by tp. If tzp is not NULL, the time zone information is returned in the time zone structure pointed to by tzp.


Parameters

tp
(Output) A pointer to a timeval structure that contains the time in seconds and microseconds since 1 January 1970, 00:00:00 UTC (epoch-1970).

tzp
(Output) A pointer to a time zone structure that contains the local time zone (measured in minutes west of Greenwich) and a flag that, if nonzero, indicates Daylight Saving Time applies locally during the appropriate part of the year.

Authorities and Locks

None.


Return Value



Error Conditions

If gettimeofday() is not successful, errno usually indicates one of the following errors. Under some conditions, errno could indicate an error other than those listed here.



Error Messages

None.


Usage Notes


Related Information


Example

The following example gets the current UTC time.

Note: By using the code examples, you agree to the terms of the Code license and disclaimer information.

#include <sys/time.h>
#include <stdio.h>
#include <errno.h>

int main(int argc, char *argv[])
{
    struct timeval now;
    int rc;

    rc=gettimeofday(&now, NULL);
    if(rc==0) {
        printf("gettimeofday() successful.\n");
        printf("time = %u.%06u\n",
                now.tv_sec, now.tv_usec);
    }
    else {
        printf("gettimeofday() failed, errno = %d\n",
                errno);
        return -1;
    }

    return 0;
}
Example Output:
gettimeofday() successful.
time = 866208142.290944


API introduced: V4R2

[ Back to top | UNIX-Type APIs | APIs by category ]