Use the clock_settime() function to modify the clock time.
Important: The clock time can be modified only by a process having the WriteDeviceData capability.
The following example code demonstrates how the current clock time can be moved forward by a minute.
The following example code performs the following tasks:
Gets the clock id of the system clock and stores it in ClockId.
Gets the current time associated with ClockId using clock_gettime() and stores it in CurrTime.
Increments current time by a minute.
Sets the current time as CurrTime using clock_settime().
#include <time.h> #include <stdio.h> #include<errno.h> #include<string.h> int main() { int Ret; clockid_t ClockId; //Stores the clock id of the system clock struct timespec CurrTime; //Stores the current time memset(&CurrTime, 0, sizeof (struct timespec)); //Will return the supported clock id only for the pid_t of 0. Ret = clock_getcpuclockid(0,&ClockId); if (Ret == 0) { //get the current time of ClockId. if(clock_gettime(ClockId, &CurrTime) == 0) { CurrTime.tv_sec+=60; //forward the time by one minute //set the new time for ClockId. Ret = clock_settime(ClockId, &CurrTime); if (Ret != 0) { printf("clock_settime () failed with %d\n", errno); } else { printf("clock_settime() passed\n"); } } else { printf("clock_gettime() failed with %d\n", errno); } } else { printf("clock_getcpuclockid () failed with %d\n", errno); } return Ret; }
The output of the above program is:
clock_settime() passed