免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3124 | 回复: 0
打印 上一主题 下一主题

读取RTC时间 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-11-18 14:32 |只看该作者 |倒序浏览
本帖最后由 seaquester 于 2010-04-20 08:58 编辑

读取RTC时间

冷胜魁(Seaquester)
lengshengkui@gmail.com
2009-11-18

一段简单的代码演示如何使用RTC driver提供的接口读取RTC 时间,并与系统时间做比较。


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

  11. int main(void)
  12. {
  13.         int fd, retval;
  14.         struct rtc_time rtc_tm;
  15.         time_t timep;
  16.         struct tm *p;
  17.         fd = open("/dev/rtc", O_RDONLY);
  18.         if (fd == -1) {
  19.                 perror("/dev/rtc");
  20.                 exit(errno);
  21.         }
  22.         /* Read the RTC time/date */
  23.         retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
  24.         if (retval == -1) {
  25.                 perror("ioctl");
  26.                 exit(errno);
  27.         }
  28.         close(fd);
  29.         fprintf(stderr, "RTC date/time: %d/%d/%d %02d:%02d:%02d\n",
  30.                 rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
  31.                 rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
  32.         time(&timep);
  33.         p = gmtime(&timep);
  34.         fprintf(stderr, "OS date/time(UTC): %d/%d/%d %02d:%02d:%02d\n",
  35.                 p->tm_mday, p->tm_mon + 1, p->tm_year + 1900,
  36.                 p->tm_hour, p->tm_min, p->tm_sec);
  37.         
  38.         p = localtime(&timep);
  39.         fprintf(stderr, "OS date/time(Local): %d/%d/%d %02d:%02d:%02d\n",
  40.                 p->tm_mday, p->tm_mon + 1, p->tm_year + 1900,
  41.                 p->tm_hour, p->tm_min, p->tm_sec);
  42.    
  43.         return 0;
  44. }
复制代码



本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/73823/showart_2097552.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP