How Do You Programmatically Set the Hardware Clock on Linux?

前端 未结 5 1379
小蘑菇
小蘑菇 2020-12-08 22:50

Linux provides the stime(2) call to set the system time. However, while this will update the system\'s time, it does not set the BIOS hardware clock to match the new system

5条回答
  •  长情又很酷
    2020-12-08 23:36

    Check out the rtc man-page for details, but if you are logged in as root, something like this:

    #include 
    #include 
    
    
        struct       rtc_time {
            int         tm_sec;      
            int         tm_min;      
            int         tm_hour;     
            int         tm_mday;     
            int         tm_mon;      
            int         tm_year;     
            int         tm_wday; /* unused */
            int         tm_yday; /* unused */
            int         tm_isdst;/* unused */
        };
    
    int fd;
    struct rtc_time rt;
    /* set your values here */
    fd = open("/dev/rtc", O_RDONLY);
    ioctl(fd, RTC_SET_TIME, &rt);
    close(fd);
    

提交回复
热议问题