LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 2337|回复: 3

怎样读取CMOS里面的时间

[复制链接]
发表于 2005-9-9 00:15:55 | 显示全部楼层 |阅读模式
是否有系统调用可以用?
发表于 2005-9-9 16:43:01 | 显示全部楼层
time_t now = time(NULL)?
这个不行吗> 有时会不一致,但情况很少吧.
回复 支持 反对

使用道具 举报

发表于 2005-9-12 16:40:12 | 显示全部楼层
好象可以用汇编+C得到
回复 支持 反对

使用道具 举报

发表于 2005-9-13 09:25:42 | 显示全部楼层
需要操作RTC设备的吧。

  1. #include <stdio.h>
  2. #include <linux/rtc.h>
  3. #include <sys/ioctl.h>
  4. #include <sys/time.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <unistd.h>
  8. #include <errno.h>

  9. int main(void)
  10. {

  11.     int i, fd, retval, irqcount = 0;
  12.     unsigned long tmp, data;
  13.     struct rtc_time rtc_tm;

  14.     fd = open ("/dev/rtc", O_RDONLY);

  15.     if (fd ==  -1) {
  16.         perror("/dev/rtc");
  17.         exit(errno);
  18.     }

  19.     fprintf(stderr, "\n\t\t\tRTC Driver Test Example.\n\n");
  20.     /* Read the RTC time/date */
  21.     retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
  22.     if (retval == -1) {
  23.         perror("ioctl");
  24.         exit(errno);
  25.     }

  26.     fprintf(stderr, "\n\nCurrent RTC date/time is %d-%d-%d, %02d:%02d:%    02d.\n",        rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
  27.         rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  28.     close(fd);
  29.     return 0;
  30. }

复制代码


内核的时间不一定总是与RTC时间同步,所以使用RTC还是有风险的,注意
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表