#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <linux/rtc.h>
#include <sys/time.h>
#include <sys/ioctl.h>
int main(int argc ,char *argv[])
{
if(argc!=2){
printf("usage: Please Input time ,Example \"2016-10-14 20:30:30\" \n");
}
int fd;
struct rtc_time tm;
struct rtc_time rtc_time;
//char *pt="2016-10-14 20:30:30";
fd= open("/dev/rtc0", O_RDWR);
if(!fd){
perror("Opening rtc0");
exit(EXIT_FAILURE);
}
sscanf(argv[1], "%d-%d-%d %d:%d:%d", &tm.tm_year, &tm.tm_mon, &tm.tm_mday,&tm.tm_hour, &tm.tm_min, &tm.tm_sec);
rtc_time.tm_year=tm.tm_year-1900;
rtc_time.tm_mon=tm.tm_mon-1;
rtc_time.tm_mday=tm.tm_mday;
rtc_time.tm_hour=tm.tm_hour;
rtc_time.tm_min=tm.tm_min;
rtc_time.tm_sec=tm.tm_sec;
ioctl(fd,RTC_SET_TIME,&rtc_time);
printf("\n\nCurrent RTC data/time is %d-%d-%d, %02d:%02d:%02d.\n", rtc_time.tm_mday, rtc_time.tm_mon+1,rtc_time.tm_year+1900, rtc_time.tm_hour, rtc_time.tm_min, rtc_time.tm_sec);
close(fd);
exit(EXIT_SUCCESS);
}