linux设置时间API

嵌入式Linux 设置时间和日期 API ,是busybox中提取的源码;

Linux设置时间和日期步骤是:

1. 设置系统时间和日期;

2. 将系统的时间和日期,同步到硬件中;


[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #include <stdio.h>  
  2. #include <fcntl.h>  
  3. #include <sys/types.h>  
  4. #include <sys/stat.h>  
  5. #include <sys/ioctl.h>  
  6. #include <time.h>  
  7. #include <linux/rtc.h>  
  8. #include <linux/capability.h>   
  9.   
  10.   
  11.   
  12. int  SetSysDateAndTime(const char *time_str);  
  13. void SetHWClockFromSysClock(int utc);  
  14.   
  15. static int  rtc_xopen(const char **default_rtc, int flags);  
  16. static void write_rtc(time_t t, int utc);  
  17.   
  18.   
  19. static const char *rtcname;  
  20.   
  21.   
  22.   
  23. int main(void)  
  24. {  
  25.     const char time_str[] = "1989-11-22 11:22:33";  
  26.     SetSysDateAndTime(time_str);  
  27.     SetHWClockFromSysClock(0);  
  28.       
  29.     system("reboot");  
  30.       
  31.     return 0;  
  32. }  
  33.   
  34.   
  35. int SetSysDateAndTime(const char *time_str)  
  36. {  
  37.     struct tm           time_tm;  
  38.     struct timeval  time_tv;  
  39.     time_t                  timep;  
  40.         int                         ret;  
  41.           
  42.         if(time_str == NULL)  
  43.         {  
  44.                 fprintf(stderr, "time string args invalid!\n");  
  45.                 return -1;  
  46.         }  
  47.       
  48.     sscanf(time_str, "%d-%d-%d %d:%d:%d", &time_tm.tm_year, &time_tm.tm_mon, &time_tm.tm_mday, &time_tm.tm_hour, &time_tm.tm_min, &time_tm.tm_sec);  
  49.     time_tm.tm_year     -= 1900;  
  50.     time_tm.tm_mon      -= 1;  
  51.     time_tm.tm_wday     = 0;  
  52.     time_tm.tm_yday     = 0;  
  53.     time_tm.tm_isdst    = 0;  
  54.   
  55.     timep = mktime(&time_tm);  
  56.     time_tv.tv_sec  = timep;  
  57.     time_tv.tv_usec = 0;  
  58.   
  59.     ret = settimeofday(&time_tv, NULL);  
  60.     if(ret != 0)  
  61.     {  
  62.         fprintf(stderr, "settimeofday failed\n");  
  63.         return -2;  
  64.     }  
  65.       
  66.     return 0;  
  67. }  
  68.   
  69.   
  70. void SetHWClockFromSysClock(int utc)  
  71. {  
  72.     struct timeval tv;  
  73.   
  74.     gettimeofday(&tv, NULL);  
  75.     //if (gettimeofday(&tv, NULL))  
  76.     //  bb_perror_msg_and_die("gettimeofday() failed");  
  77.     write_rtc(tv.tv_sec, utc);  
  78. }  
  79.   
  80.   
  81.   
  82. static int  rtc_xopen(const char **default_rtc, int flags)  
  83. {  
  84.     int rtc;  
  85.   
  86.     if (!*default_rtc) {  
  87.         *default_rtc = "/dev/rtc";  
  88.         rtc = open(*default_rtc, flags);  
  89.         if (rtc >= 0)  
  90.             return rtc;  
  91.         *default_rtc = "/dev/rtc0";  
  92.         rtc = open(*default_rtc, flags);  
  93.         if (rtc >= 0)  
  94.             return rtc;  
  95.         *default_rtc = "/dev/misc/rtc";  
  96.     }  
  97.   
  98.     return open(*default_rtc, flags);  
  99. }  
  100.   
  101. static void write_rtc(time_t t, int utc)  
  102. {  
  103. #define RTC_SET_TIME    _IOW('p', 0x0a, struct rtc_time) /* Set RTC time    */  
  104.   
  105.     struct tm tm;  
  106.     int rtc = rtc_xopen(&rtcname, O_WRONLY);  
  107.   
  108.     tm = *(utc ? gmtime(&t) : localtime(&t));  
  109.     tm.tm_isdst = 0;  
  110.   
  111.     ioctl(rtc, RTC_SET_TIME, &tm);  
  112.   
  113.     close(rtc);  
  114. }  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值