LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 02-25-2009, 05:51 AM   #1
mayankparasher
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Rep: Reputation: 0
C code to change date time on linux


hi
i want to write code in C, which can change system date and time on linux pc.
Which lib functions can do this..
-mayank
 
Old 02-25-2009, 06:00 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
The system() library call can be used to call /bin/date.

Why do you want to write this program in C, when it would seem more appropriate to write a shell script?
 
Old 02-25-2009, 06:20 AM   #3
mayankparasher
LQ Newbie
 
Registered: Feb 2009
Posts: 4

Original Poster
Rep: Reputation: 0
this code will run on uclinux,so i need to write entire code in C only, i wanna write function to change date and time. i hope there must be some lib function which can do this task.
I've tried some thing like this, but time is not changing.
//////////////////////////////////////////////////////////

void Cmd_SD(uint8 *date_str) //*data_str will receive date like 010209
{
uint32 day, month, year;
uint8 buff[3],status;
time_t mytime,t1=0;
struct tm *tm_ptr;

strncpy(buff, date_str,2);
day = atoi(buff);
strncpy(buff, (date_str+2),2);
month = atoi(buff);
strncpy(buff, (date_str+4),2);
year = atoi(buff);
time(&mytime);
tm_ptr=0;
tm_ptr = localtime(&mytime);
tm_ptr->tm_year = year+100;
tm_ptr->tm_mon = month-1;
tm_ptr->tm_mday = day;
t1 =mktime(tm_ptr);
status = 0;
status = settimeofday(&t1);
}
//////////////////////////////////////////////////////////
????????? Now what do i do.??????
-mayank
 
Old 02-25-2009, 06:50 AM   #4
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I took your code, reviewed the man-pages, and came up with a working solution. See the code below:

Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>


void setDate(const char* dataStr)  // format like MMDDYY
{
  char buf[3] = {0};

  strncpy(buf, dataStr + 0, 2);
  unsigned short month = atoi(buf);

  strncpy(buf, dataStr + 2, 2);
  unsigned short day = atoi(buf);

  strncpy(buf, dataStr + 4, 2);
  unsigned short year = atoi(buf);

  time_t mytime = time(0);
  struct tm* tm_ptr = localtime(&mytime);

  if (tm_ptr)
  {
    tm_ptr->tm_mon  = month - 1;
    tm_ptr->tm_mday = day;
    tm_ptr->tm_year = year + (2000 - 1900);

    const struct timeval tv = {mktime(tm_ptr), 0};
    settimeofday(&tv, 0);
  }
}

int main(int argc, char** argv)
{
  if (argc < 1)
  {
    printf("enter a date using the format MMDDYY\n");
    return 1;
  }

  setDate(argv[1]);

  return 0;
}
P.S. I did not include an error checking; you may want to add that yourself.

Last edited by dwhitney67; 02-25-2009 at 06:56 AM.
 
Old 03-31-2009, 08:13 PM   #5
pwk
LQ Newbie
 
Registered: Mar 2009
Posts: 1

Rep: Reputation: 0
How could you get the date/time info from linux to use in a calendar program?
 
Old 03-31-2009, 10:04 PM   #6
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
To convert strings to time, I'd look into getdate. No need parsing it yourself.
Kevin Barry

edit:
Just saw that the format you use might not be built in; however, strptime should get the job done because you can specify a format string. As far as I know, it won't overwrite values it doesn't use; therefore, you should be able to get the current time from the system and run it through strptime, leaving only the relevant fields changed.

Last edited by ta0kira; 03-31-2009 at 10:10 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Code for changing system's local date & time g4j31a5 Programming 8 03-21-2007 05:02 AM
how to change system time? date cmd? servnov Linux - Newbie 2 10-31-2004 03:07 PM
Change system time or date or both lel800 Linux - Newbie 6 10-28-2003 07:00 PM
how to change date and time in slackware 8.1 erikm103 Linux - Newbie 4 02-25-2003 06:26 PM
how do you change the date / time? jkane Linux - General 2 01-15-2002 09:09 PM

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

All times are GMT -5. The time now is 06:43 PM.

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