LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-12-2010, 12:45 AM   #1
sreejeshs
LQ Newbie
 
Registered: Jan 2010
Posts: 5

Rep: Reputation: 0
Setting time in LInux


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). I could'nt find no other api in linux. What should i do? I had tried with mktime/gmtime apis.

sreejesh
 
Old 01-12-2010, 01:02 AM   #2
dracuss
Member
 
Registered: May 2006
Location: Chisinau, Moldova
Distribution: Gentoo, Debian sid
Posts: 151

Rep: Reputation: 29
Try using the "date" command. it's "date -s <time>"
 
Old 01-12-2010, 03:12 AM   #3
H_TeXMeX_H
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
pls state what language this should be in (C/C++ ?)
 
Old 01-12-2010, 07:20 AM   #4
wje_lq
Member
 
Registered: Sep 2007
Location: Mariposa
Distribution: FreeBSD,Debian wheezy
Posts: 811

Rep: Reputation: 179Reputation: 179
Quote:
Originally Posted by sreejeshs View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems setting time and time zone in RH Linux am_mitra Linux - Newbie 1 12-03-2008 08:09 PM
LXer: Setting time the right way, the Linux way LXer Syndicated Linux News 0 10-03-2007 10:30 PM
Linux Time setting? zbinwu Linux - Newbie 6 09-04-2005 05:09 PM
Hell of a time setting up linux router knappster Linux - Networking 6 10-17-2004 06:45 PM
Setting up a Linux/Redhat system 1st time! andyc1000 Linux - Newbie 3 09-01-2003 01:46 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration