14

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 time.

Linux systems typically sync the hardware clock with the system time at shutdown and at periodic intervals. However, if the machine gets power-cycled before one of these automatic syncs, the time will be incorrect when the machine restarts.

How do you ensure that the hardware clock gets updated when you set the system time?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302

5 Answers5

18

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

#include <linux/rtc.h>
#include <sys/ioctl.h>


    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);
flederwiesel
  • 447
  • 1
  • 9
  • 17
Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • 1
    Root not needed, only CAP_SYS_TIME capability according to rtc(4). – Erikw Nov 12 '14 at 07:11
  • Hello, i have 3 questions regarding trhe answer. 1. Why the own rtc_time and not the one from rtc.h 2. O_RDONLY ? is that not only for read? 3. if i try the code I get from ioctl -1 and errno 22. What does that mean? – Offler Jun 16 '21 at 14:10
  • @Offler This answer was not meant as a copy and paste ready to use solution. I included the struct to show how it worked. For a real example view the source code to the `hwclock` program. – Zan Lynx Jun 16 '21 at 15:14
8

After calling stime(), do this:

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

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

Kristopher Johnson
  • 81,409
  • 55
  • 245
  • 302
2

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.

Martin York
  • 257,169
  • 86
  • 333
  • 562
  • Most of those variations, from my experience, are due to low voltage on the motheboard battery. I would suggest checking it before setting up an innecesary service if its not mission critical. – Manuel Ferreria Oct 22 '08 at 18:05
2

I would start by reading the source code of hwclock.

iny
  • 7,339
  • 3
  • 31
  • 36
-2

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
Werner
  • 14,324
  • 7
  • 55
  • 77