How Do You Programmatically Set the Hardware Clock on Linux?

半腔热情 提交于 2019-11-28 07:01:40

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

    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 */
    };

struct rtc_time rt;
/* set your values here */
fd = open("/dev/rtc", O_RDONLY);
ioctl(fd, RTC_SET_TIME, &rt);
close(fd);
Kristopher Johnson

After calling stime(), do this:

system("/sbin/hwclock --systohc");

See the hwclock(8) man page for more information.

I would start by reading the source code of hwclock.

I would also like to point out the hardware clock is only accurate to a point (I have seen systems that will loose/gain a couple of seconds a day).

Have you considered setting up the network time daemon to sync your clock against a time server?

Admittedly this syncing does not happen until after the daemon starts so keeping your hardware clock in sync also helps between the power up and the point the time daemon syncs against the time server.

brian carr

If the text editor is different than kubuntu (kate is the default in editor), use your own with the sudo command in terminal.

  1. run terminal
  2. copy and paste this command sudo kate /etc/default/rcS press Enter
  3. enter user password (your login password) press Enter
  4. text editor will open on the desktop
  5. change the line UTC=yes to UTC=no
  6. and click save (at top of text editor tab bar)
  7. reboot
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!