[Previous]
[Contents]
[Next]

clock_settime()

set a clock

Synopsis:

#include <time.h>
int clock_settime( clockid_t clock_id,
                   struct timespec *tp );

Description:

The clock_settime() function sets the clock specified by clock_id, from the buffer pointed to by tp. The only supported clock ID is CLOCK_REALTIME, which maintains the system time.

The tp parameter points to a structure containing at least the following members:

time_t tv_sec
The number of seconds since 1970.
time_t tv_nsec
The number of nanoseconds in the current second. This value increases by some multiple of nanoseconds, based on the system clock's resolution.

Returns:

0
Success
-1
An error occurred. errno is set to indicate the error.

Errors:

EINVAL
The clock_id isn't CLOCK_REALTIME.
EPERM
You don't have sufficient privilege to change the time.

Examples:

/*  This program sets the clock forward 1 day. */

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <time.h>

int main( void )
  {
    struct timespec stime;

    if( clock_gettime( CLOCK_REALTIME, &stime) == -1 ) {
       perror( "getclock" );
       exit( EXIT_FAILURE );
    }

    stime.tv_sec += (60*60)*24L;  /* Add one day */
    stime.tv_nsec = 0;
    if( clock_settime( CLOCK_REALTIME, &stime) == -1 ) {
       perror( "setclock" );
       exit( EXIT_FAILURE );
    }
    return( EXIT_SUCCESS );
  }

Classification:

POSIX 1003.4

Safety:
Interrupt handler No
Signal handler Yes, but modifies errno
Thread Yes

See also:

clock_getres(), clock_gettime(), clock_setres(), errno, qnx_getclock(), qnx_setclock(), ticksize utility


[Previous]
[Contents]
[Next]