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 |
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? |
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 |
I took your code, reviewed the man-pages, and came up with a working solution. See the code below:
Code:
#include <stdio.h> |
How could you get the date/time info from linux to use in a calendar program?
|
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. |