获取linux系统RTC设备的时钟

由于linux和windows的不同,这个结构体不能使用 库文件中的tm结构体

代码
#include <sys/ioctl.h>

struct rtc_time {
 
int tm_sec;
 
int tm_min;
 
int tm_hour;
 
int tm_mday;
 
int tm_mon;
 
int tm_year;
 
int tm_wday;
 
int tm_yday;
 
int tm_isdst;
};
#define RTC_MAGIC 'p'
#define RTC_RD_TIME  _IOR(RTC_MAGIC, 0x09, struct rtc_time) /* Read RTC time. */
#define RTC_SET_TIME _IOW(RTC_MAGIC, 0x0a, struct rtc_time) /* Set RTC time. */
void rtc_test(void)
{
 
int fd;
 
struct rtc_time tm;
 fd 
= open("/dev/rtc0",O_RDONLY); 
 
if (fd < 0) {
  
return;
 }
 
if (0 != ioctl(fd,RTC_RD_TIME,&tm)){
  close(fd);
  
return;
 }
 close(fd);

 printf(
"%d-%d:%d-%d:%d:%d\n",
  tm.tm_year
+1900,
  tm.tm_mon
+1,
  tm.tm_mday,
  tm.tm_hour,
  tm.tm_min,
  tm.tm_sec);
}
原文地址:https://www.cnblogs.com/kakaliush/p/1688955.html