Set a clock
#include <time.h>
int clock_settime( clockid_t clock_id,
const struct timespec * tp );
libc
The clock_settime() function sets the clock specified by clock_id, from the buffer pointed to by tp.
The tp parameter points to a structure containing at least the following members:
![]() |
Be careful if you set the date during the period that a time zone is switching to daylight saving time (DST). When a time zone changes to DST, the local time goes back one hour (for example, 2:00 a.m. becomes 1:00 a.m.). The local time during this hour is ambiguous (e.g. 1:14 a.m. occurs twice in the morning that the time zone switches to DST). To avoid problems, use UTC time to set the date in this period. |
/* 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" );
return EXIT_FAILURE;
}
stime.tv_sec += (60*60)*24L; /* Add one day */
stime.tv_nsec = 0;
if( clock_settime( CLOCK_REALTIME, &stime) == -1 ) {
perror( "setclock" );
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
POSIX 1003.1 (Realtime Extensions)
| Safety: | |
|---|---|
| Cancellation point | No |
| Interrupt handler | No |
| Signal handler | Yes |
| Thread | Yes |
clock_getres(), clock_gettime(), errno