Quote:
Originally Posted by sreejeshs
Hi,
I am trying to set time using settimeofday in linux. But it sets local time. i.e works like SetLocalTime in windows. But I want to set system time(like SetSystemTime in windows).
|
In Linux/UNIX, "local time" is not distinguished from system time. The current time of day is the same for all running processes, though different processes can see that time in different time zones if desired. If you change it, it's changed for all processes. For this reason, you must be privileged (probably root) to change it.
But if by "local time" you mean as opposed to UTC (aka GMT), I can show you how to change the system time either to a specific local time (in this case, July 4, 1976 in the local time zone, which in my case is Pacific Time) or a specific UTC time (in this case, July 4, 1976 UTC).
A minor complication is that currently my local time zone is PST (Pacific Standard Time). But when changing to the desired time, my local time zone would be PDT (Pacific Daylight Time), because July 1976 was in the summer. The time library is smart enough to figure this out.
Here are two programs. The first program (when run as root) sets the desired time as expressed in the local time zone, and gives this output:
Code:
before changing date:
Tue Jan 12 05:02:40 PST 2010
Tue Jan 12 13:02:40 UTC 2010
after changing date:
Sun Jul 4 00:00:00 PDT 1976
Sun Jul 4 07:00:00 UTC 1976
The second program (when run as root) sets the desired time as UTC, and gives this output:
Code:
before changing date:
Tue Jan 12 05:03:29 PST 2010
Tue Jan 12 13:03:29 UTC 2010
after changing date:
Sat Jul 3 17:00:00 PDT 1976
Sun Jul 4 00:00:00 UTC 1976
Ok, now for the source code, which I've presented as shell scripts which you should run as root. Thing One:
Code:
echo before changing date:; date; date --utc
cat > 1.c <<EOD; gcc -Wall -Werror 1.c -o 1; ./1; echo after changing date:; date; date --utc
#include <errno.h>
#include <stdio.h>
#include <time.h> /* for mktime() */
#include <sys/time.h> /* for settimeofday() */
int main(void)
{
int result;
int saved_error;
time_t new_time;
struct tm lassie;
struct timeval garfield;
lassie.tm_sec =0;
lassie.tm_min =0;
lassie.tm_hour=0;
lassie.tm_mday=4;
lassie.tm_mon =6; /* July */
lassie.tm_year=1976-1900;
new_time=mktime(&lassie);
garfield.tv_sec =new_time;
garfield.tv_usec=0;
result=settimeofday(&garfield,NULL);
if(result<0)
{
saved_error=errno;
if(saved_error==EPERM)
{
fprintf(stderr,"You must run this as root.\n");
return 1;
}
}
return 0;
} /* main() */
EOD
Here is Thing Two:
Code:
echo before changing date:; date; date --utc
cat > 2.c <<EOD; gcc -Wall -Werror 2.c -o 2; ./2; echo after changing date:; date; date --utc
#include <errno.h>
#include <stdio.h>
#include <stdlib.h> /* for setenv() */
#include <time.h> /* for mktime() */
#include <sys/time.h> /* for settimeofday() */
int main(void)
{
int result;
int saved_error;
time_t new_time;
struct tm lassie;
struct timeval garfield;
lassie.tm_sec =0;
lassie.tm_min =0;
lassie.tm_hour=0;
lassie.tm_mday=4;
lassie.tm_mon =6; /* July */
lassie.tm_year=1976-1900;
result=setenv("TZ","UTC",1);
if(result<0)
{
fprintf(stderr,"setenv() returned an error.\n");
return 1;
}
new_time=mktime(&lassie);
garfield.tv_sec =new_time;
garfield.tv_usec=0;
result=settimeofday(&garfield,NULL);
if(result<0)
{
saved_error=errno;
if(saved_error==EPERM)
{
fprintf(stderr,"You must run this as root.\n");
return 1;
}
else
{
fprintf(stderr,"settimeofday() returned an error.\n");
return 1;
}
}
return 0;
} /* main() */
EOD
Hope this helps.