#include <linux/rtc.h>
#include <sys/ioctl.h>
void ReadRTC()
{
int fdrtc = open("/dev/rtc",O_RDONLY);
if (fdrtc == -1){
perror("/dev/rtc");
return;
}
struct rtc_time rtc_tm;
int retval = ioctl(fdrtc,RTC_RD_TIME,&rtc_tm);
if (retval == -1){
perror("ioctl");
close(fdrtc);
return;
}
char cTime[100] = {0};
sprintf(cTime,"now time is :%d-%d-%d %02d:%02d:%02d/n",
rtc_tm.tm_year + 1900,rtc_tm.tm_mon + 1,rtc_tm.tm_mday,rtc_tm.tm_hour,rtc_tm.tm_min,rtc_tm.tm_sec);
printf(cTime);
close(fdrtc);
}
void SetRTC()
{
int fdrtc = open("/dev/rtc",O_RDONLY);
if (fdrtc == -1){
perror("/dev/rtc");
return;
}
struct rtc_time rtc_tm;
int retval = ioctl(fdrtc,RTC_RD_TIME,&rtc_tm);
if (retval == -1){
perror("ioctl");
close(fdrtc);
return;
}
rtc_tm.tm_year = 111;
rtc_tm.tm_mon = 0;
rtc_tm.tm_mday = 20;
rtc_tm.tm_hour = 19;
int nInput = 0;
printf("input -1,to stop set time/n");
scanf("%d",&nInput);
if (nInput == -1){
return;
}
rtc_tm.tm_min = nInput;
scanf("%d",&nInput);
if (nInput == -1){
return;
}
rtc_tm.tm_sec = nInput;
//set time
retval = ioctl(fdrtc,RTC_SET_TIME,&rtc_tm);
if (retval == -1){
perror("ioctl");
close(fdrtc);
return;
}
close(fdrtc);
}