LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   C code to change date time on linux (https://www.linuxquestions.org/questions/programming-9/c-code-to-change-date-time-on-linux-707384/)

mayankparasher 02-25-2009 05:51 AM

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

dwhitney67 02-25-2009 06:00 AM

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?

mayankparasher 02-25-2009 06:20 AM

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

dwhitney67 02-25-2009 06:50 AM

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.

pwk 03-31-2009 08:13 PM

How could you get the date/time info from linux to use in a calendar program?

ta0kira 03-31-2009 10:04 PM

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.


All times are GMT -5. The time now is 02:16 AM.