zoukankan      html  css  js  c++  java
  • RTC 读写指令及测试程序

    在 Linux 系统中,指令 date 和 hwclock 都可以读写时间

    date:读写系统时间,写时间需要管理员权限

    hwclock:读写硬件时间,也就是 rtc 模块的时间,读写都必须有管理员权限

    // 读取当前系统时间
    $ date
    Thu Oct 24 03:03:13 UTC 2019
    // 普通用户设置时间失败
    $ date 102411032019.00
    date: cannot set date: Operation not permitted
    Thu Oct 24 11:03:00 UTC 2019
    // 超级用户设置时间成功,格式:月日时分年.秒
    $ sudo date 102411032019.00
    Thu Oct 24 11:03:00 UTC 2019
    // 读取当前系统时间,确认时间设置成功
    // 小时与设置的11点不同,这是由于时区造成的
    $ date
    Thu Oct 24 03:03:38 UTC 2019

    rtc 测试 demo:

    #include <stdio.h>
    #include <stdlib.h>
    #include <linux/rtc.h>
    #include <sys/ioctl.h>
    #include <sys/time.h>
    #include <sys/types.h>
    #include <fcntl.h>
    #include <unistd.h>
    #include <errno.h>
    #include <time.h>
    
    int main(int argc, char *argv[])
    {
        int fd, retval;
        struct rtc_time rtc_tm;
        time_t timep;
        struct tm *p;
    
        fd = open("/dev/rtc", O_RDONLY);
        if (fd == -1) {
            fprintf(stderr, "open /dev/rtc error
    ");
            exit(errno);
        }
    
        /* Read the RTC time/date */
        retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
        if (retval == -1) {
            perror("ioctl");
            exit(errno);
        }
    
        fprintf(stderr, "RTC date/time: %d/%d/%d %02d:%02d:%02d
    ",
            rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
            rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
        time(&timep);
        p = gmtime(&timep);
        fprintf(stderr, "OS date/time(UTC): %d/%d/%d %02d:%02d:%02d
    ",
            p->tm_mday, p->tm_mon + 1, p->tm_year + 1900,
            p->tm_hour, p->tm_min, p->tm_sec);
        p = localtime(&timep);
        fprintf(stderr, "OS date/time(Local): %d/%d/%d %02d:%02d:%02d
    ",
            p->tm_mday, p->tm_mon + 1, p->tm_year + 1900,
            p->tm_hour, p->tm_min, p->tm_sec);
        
        rtc_tm.tm_mday = 15;
        rtc_tm.tm_mon = 11;
        rtc_tm.tm_hour = 15;
        rtc_tm.tm_min = 15;
        retval = ioctl(fd, RTC_SET_TIME, &rtc_tm);
        if (retval == -1) {
            perror("ioctl");
            exit(errno);
        }
        
        /* Write the RTC time/date */
        retval = ioctl(fd, RTC_RD_TIME, &rtc_tm);
        if (retval == -1) {
            perror("ioctl");
            exit(errno);
        }
    
        fprintf(stderr, "RTC date/time: %d/%d/%d %02d:%02d:%02d
    ",
            rtc_tm.tm_mday, rtc_tm.tm_mon + 1, rtc_tm.tm_year + 1900,
            rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec);
        time(&timep);
        p = gmtime(&timep);
        fprintf(stderr, "OS date/time(UTC): %d/%d/%d %02d:%02d:%02d
    ",
            p->tm_mday, p->tm_mon + 1, p->tm_year + 1900,
            p->tm_hour, p->tm_min, p->tm_sec);
        p = localtime(&timep);
        fprintf(stderr, "OS date/time(Local): %d/%d/%d %02d:%02d:%02d
    ",
            p->tm_mday, p->tm_mon + 1, p->tm_year + 1900,
            p->tm_hour, p->tm_min, p->tm_sec);
        
        close(fd);
            
        return 0;
    }
  • 相关阅读:
    fluent/starccm/商业CFD软件中残差的概念
    windows 下用命令来操作定时任务
    selenium下打开Chrome报错解决
    TypeError: a bytes-like object is required, not 'str'
    Cannot redeclare class phpmailerException
    linux freetds无法构建错误:为--with-tdsver:8.0指定的值无效
    Apache 修改端口号
    php 二维数组按照某个键排序
    php 计算 距离
    pymysql 读取数据库没有字段
  • 原文地址:https://www.cnblogs.com/libra13179/p/14299317.html
Copyright © 2011-2022 走看看