tags:

views:

51

answers:

1

I can get the system time using struct tm and time(),localtime(),asctime().but i need help about how i can set time of system using c program.

+1  A: 

if you don't want to execute a shell command you can (as you mentioned) use the settimeofday, i would start by reading the MAN page, or looking for some examples

here's an example:

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

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

    now.tv_sec=866208142;
    now.tv_usec=290944;

    rc=settimeofday(&now, NULL);
    if(rc==0) {
        printf("settimeofday() successful.\n");
    }
    else {
        printf("settimeofday() failed, "
        "errno = %d\n",errno);
        return -1;
    }

    return 0;
}

Shamelessly ripped From IBMs documentation, the struct timeval struct holds the number of seconds (as a long) plus the number of milliseconds (as a long) from 1 January 1970, 00:00:00 UTC (Unix Epoch time). So you will need to calculate these numbers in order to set the time. you can use these helper functions, to better handle dealing with the timeval struct.

luke
Thanks again luke...Sorry,bcoz i forget the basic.....look at the MAN pages.
neo730
often these days i just use google to search man pages (unless I'm right there in the terminal), usually the "I' Feeling Lucky" search nails it every time if you just enter man 'query'. If you are using firefox or chrome you can just type it into the address bar.
luke